package com.pine.admin.modules.base.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.pine.admin.modules.base.service.ExcelPoiService;
import com.pine.admin.modules.business.dao.OrderDao;
import com.pine.admin.modules.business.entity.Order;
import com.pine.admin.modules.business.service.OrderService;
import com.pine.admin.shiro.ShiroUtils;
import com.pine.common.excelutil.ExcelPoiUtil;
import com.pine.common.exception.ApiException;
import com.pine.common.utils.DateTimeTool;
import com.pine.common.utils.HttpGetRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 用户信息
 *
 * @author Pine
 * @email 771190883@qq.com
 * @date 2019-03-31 21:13:10
 */
@Service
@Slf4j
public class ExcelPoiServiceImpl implements ExcelPoiService {


    /**
     * 订单状态map
     */
    private static final Map<String, String> statusMap = new HashMap<>();

    static {
        statusMap.put("1", "待付款");
        statusMap.put("2", "待发货");
        statusMap.put("3", "待收货");
        statusMap.put("4", "已完成");
        statusMap.put("5", "已取消");
        statusMap.put("6", "退款通过");
        statusMap.put("7", "退货通过");
    }

    /**
     * 来源
     */
    private static final Map<String, String> sourceMap = new HashMap<>();

    static {
        sourceMap.put("1", "PC");
        sourceMap.put("2", "H5");
        sourceMap.put("3", "APP");
        ;
    }

    @Autowired
    private OrderService orderService;

    @Override
    public void exampleExport(HttpServletRequest request, HttpServletResponse response, Integer productId) {
        try {
            List<Object> exchangeCodes = new ArrayList<>();
            if (!exchangeCodes.isEmpty()) {
                String excelName = "示例" + DateTimeTool.dateTimeToStr(new Date());
                LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
                fieldMap.put("name", "姓名");
                fieldMap.put("age", "年龄");
                ExcelPoiUtil.export(excelName, exchangeCodes, fieldMap, response);
            }
        } catch (Exception e) {
            log.error("exampleExport", e);

            throw new ApiException(e.getMessage());
        }
    }

    @Override
    public void orderExcelExport(HttpServletRequest request, HttpServletResponse response, Order order) {
        Map<Long, String> dealerNameMap = new HashMap<>();
        if (!ShiroUtils.getShiroUserInfo().getName().contains("商城管理员")) {
            order.setDealerId(ShiroUtils.getShiroUserInfo().getDealerID());
        }
        List<Order> allList = orderService.getAllList(order);
        allList.stream().forEach(o -> {
            orderService.FullOrderInfo(o);
            o.setCreateString(DateTimeTool.dateTimeToYearMMddhhmmss(o.getCreateTime()));
            o.setStatus(statusMap.get(o.getStatus()));
            o.setSource(sourceMap.get(o.getSource()));
            o.setShopName(o.getOrderSkus().get(0).getSkuName());
            o.setSkuNo(o.getOrderSkus().get(0).getSkuNo());
            o.setSkuSpecs(o.getOrderSkus().get(0).getSkuSpecs());
            try {
                String s1 = dealerNameMap.get(o.getDealerId());
                if (!StringUtils.isEmpty(s1)) {
                    o.setDealerInfo(String.valueOf(s1));
                } else {
                    String getUrl  = HttpGetRequestUtil.doGet("https://dealer.jetour.com.cn/api-dealer/dealer-info/front/searchDealerById/" + o.getDealerId());
                    JSONObject jsonObject = JSONObject.parseObject(getUrl);
                    Object data = jsonObject.get("data");
                    Object dealerName = ((JSONObject) data).getInnerMap().get("dealerName");
                    if (!ObjectUtils.isEmpty(dealerName)) {
                        dealerNameMap.put(o.getDealerId(), String.valueOf(dealerName));
                        o.setDealerInfo(String.valueOf(dealerName));
                    } else {
                        o.setDealerInfo("@#" + o.getDealerInfo());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        });
        try {
            if (!allList.isEmpty()) {
                String excelName = "商城订单-"+System.currentTimeMillis();
                LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
//                fieldMap.put("orderCode", "订单号");
                fieldMap.put("billNo", "银联单号");
                fieldMap.put("customerName", "注册账号");
                fieldMap.put("receiptName", "购车人");
                fieldMap.put("receiptMobile", "购车人电话");
                fieldMap.put("orderAttr.receiptAddress", "收货地址");
                fieldMap.put("orderAttr.receiptDetailAddress", "详细地址");
                fieldMap.put("shopName", "商品名称");
                fieldMap.put("skuNo", "商品编号");
                fieldMap.put("skuSpecs", "商品规格");
                fieldMap.put("dealerInfo", "经销商名称");
                fieldMap.put("createString", "下单时间");
                fieldMap.put("status", "订单状态");
                fieldMap.put("originalPrice", "实付金额");
                fieldMap.put("source", "支付方式");
                ExcelPoiUtil.export(excelName, allList, fieldMap, response);
            }
        } catch (Exception e) {

            throw new ApiException(e.getMessage());
        }
    }


}
