package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSONObject;
import com.bweb.config.SysConst;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighTySalesman;
import com.hai.enum_type.UserObjectTypeEnum;
import com.hai.model.ResponseData;
import com.hai.model.TyOrderModel;
import com.hai.model.UserInfoModel;
import com.hai.service.BsOrganizationService;
import com.hai.service.HighTyAgentOilStationService;
import com.hai.service.HighTySalesmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping(value = "/tyAgentOilStation")
@Api(value = "订单接口")
public class HighTyAgentOilStationController {

    private static Logger log = LoggerFactory.getLogger(HighTyAgentOilStationController.class);

    @Resource
    private HighTyAgentOilStationService highTyAgentOilStationService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/assignOrg", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分配机构")
    public ResponseData assignOrg(@RequestBody JSONObject body) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            if (userModel.getBsCompany() == null || !userModel.getSecUser().getAdminFlag().equals(1)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            if (body.getLong("orgId") == null
                    || body.getJSONArray("storeList") == null
                    || body.getJSONArray("storeList").size() == 0
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Long> storeList = new ArrayList<>();
            for (Object obj : body.getJSONArray("storeList")) {
                JSONObject objData = (JSONObject)obj;
                if (objData.getLong("storeId") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                storeList.add(objData.getLong("storeId"));
            }
            highTyAgentOilStationService.assignOrg(body.getLong("orgId"), storeList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighTyAgentController --> assignOrg() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/unbindOrg", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "解绑机构")
    public ResponseData unbindOrg(@RequestBody JSONObject body) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            if (userModel.getBsCompany() == null || !userModel.getSecUser().getAdminFlag().equals(1)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            if (body.getJSONArray("oilStationList") == null
                    || body.getJSONArray("oilStationList").size() == 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Long> oilStationList = new ArrayList<>();
            for (Object obj : body.getJSONArray("oilStationList")) {
                JSONObject objData = (JSONObject)obj;
                if (objData.getLong("oilStationId") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                oilStationList.add(objData.getLong("oilStationId"));
            }
            highTyAgentOilStationService.unbindOrg(oilStationList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighTyAgentController --> assignOrg() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/assignAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分配代理商")
    public ResponseData assignAgent(@RequestBody JSONObject body) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            if (body.getLong("agentId") == null
                    || body.getJSONArray("oilStationList") == null
                    || body.getJSONArray("oilStationList").size() == 0
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Long> storeList = new ArrayList<>();
            for (Object obj : body.getJSONArray("oilStationList")) {
                JSONObject objData = (JSONObject)obj;
                if (objData.getLong("oilStationId") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                storeList.add(objData.getLong("oilStationId"));
            }
            highTyAgentOilStationService.assignAgent(body.getLong("agentId"), storeList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighTyAgentController --> assignOrg() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/unbindAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "解绑代理商")
    public ResponseData unbindAgent(@RequestBody JSONObject body) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            if (userModel.getBsCompany() == null && !userModel.getSecUser().getAdminFlag().equals(1)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            if (body.getJSONArray("oilStationList") == null
                    || body.getJSONArray("oilStationList").size() == 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Long> oilStationList = new ArrayList<>();
            for (Object obj : body.getJSONArray("oilStationList")) {
                JSONObject objData = (JSONObject)obj;
                if (objData.getLong("oilStationId") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                oilStationList.add(objData.getLong("oilStationId"));
            }
            highTyAgentOilStationService.unbindAgent(oilStationList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighTyAgentController --> assignOrg() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/assignSalesman", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分配业务员")
    public ResponseData assignSalesman(@RequestBody JSONObject body) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            if (!userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type6.getType())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            if (body.getLong("salesmanId") == null
                    || body.getJSONArray("oilStationList") == null
                    || body.getJSONArray("oilStationList").size() == 0
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Long> storeList = new ArrayList<>();
            for (Object obj : body.getJSONArray("oilStationList")) {
                JSONObject objData = (JSONObject)obj;
                if (objData.getLong("oilStationId") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                storeList.add(objData.getLong("oilStationId"));
            }
            highTyAgentOilStationService.assignSalesman(body.getLong("salesmanId"), storeList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighTyAgentController --> assignOrg() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/unbindSalesman", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "解绑业务员")
    public ResponseData unbindSalesman(@RequestBody JSONObject body) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            if (userModel.getBsCompany() != null && !userModel.getSecUser().getAdminFlag().equals(1)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            if (body.getJSONArray("oilStationList") == null
                    || body.getJSONArray("oilStationList").size() == 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Long> oilStationList = new ArrayList<>();
            for (Object obj : body.getJSONArray("oilStationList")) {
                JSONObject objData = (JSONObject)obj;
                if (objData.getLong("oilStationId") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                oilStationList.add(objData.getLong("oilStationId"));
            }
            highTyAgentOilStationService.unbindSalesman(oilStationList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighTyAgentController --> assignOrg() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getOilStationList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询加油站列表")
    public ResponseData getOilStationList(@RequestParam(name = "regionName", required = false) String regionName,
                                          @RequestParam(name = "storeName", required = false) String storeName,
                                          @RequestParam(name = "isAssignOrgId", required = false) Boolean isAssignOrgId,
                                          @RequestParam(name = "organizationName", required = false) String organizationName,
                                          @RequestParam(name = "isAssignTyAgent", required = false) Boolean isAssignTyAgent,
                                          @RequestParam(name = "tyAgentName", required = false) String tyAgentName,
                                          @RequestParam(name = "isAssignTySalesman", required = false) Boolean isAssignTySalesman,
                                          @RequestParam(name = "tySalesmanName", required = false) String tySalesmanName,
                                          @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                          @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            Map<String, Object> param = new HashMap<>();
            param.put("regionName", regionName);
            param.put("storeName", storeName);

            if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type0.getType())) {
                param.put("isAssignOrgId", isAssignOrgId);
                param.put("organizationName", organizationName);
                param.put("isAssignTyAgent", isAssignTyAgent);
                param.put("tyAgentName", tyAgentName);
                param.put("isAssignTySalesman", isAssignTySalesman);
                param.put("tySalesmanName", tySalesmanName);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type1.getType()) && userModel.getSecUser().getAdminFlag().equals(1)) {
                param.put("regionId", userModel.getBsCompany().getRegionId());
                param.put("isAssignOrgId", isAssignOrgId);
                param.put("organizationName", organizationName);
                param.put("isAssignTyAgent", isAssignTyAgent);
                param.put("tyAgentName", tyAgentName);
                param.put("isAssignTySalesman", isAssignTySalesman);
                param.put("tySalesmanName", tySalesmanName);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type1.getType()) && userModel.getSecUser().getAdminFlag().equals(0)) {
                param.put("organizationId", userModel.getBsOrganization().getId());
                param.put("isAssignTyAgent", isAssignTyAgent);
                param.put("tyAgentName", tyAgentName);
                param.put("isAssignTySalesman", isAssignTySalesman);
                param.put("tySalesmanName", tySalesmanName);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type6.getType())) {
                param.put("tyAgentId", userModel.getHighTyAgent().getId());
                param.put("isAssignTySalesman", isAssignTySalesman);
                param.put("tySalesmanName", tySalesmanName);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type7.getType())) {
                param.put("tySalesmanId", userModel.getHighTySalesman().getId());
            }

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highTyAgentOilStationService.getOilStationList(param)));

        } catch (Exception e) {
            log.error("HighTyAgentController --> getOilStationList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/getOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单列表")
    public ResponseData getOrderList(@RequestParam(name = "orderNo", required = false) String orderNo,
                                     @RequestParam(name = "goodsName", required = false) String goodsName,
                                     @RequestParam(name = "salesmanName", required = false) String salesmanName,
                                     @RequestParam(name = "salesmanId", required = false) Long salesmanId,
                                     @RequestParam(name = "agentName", required = false) String agentName,
                                     @RequestParam(name = "agentId", required = false) Long agentId,
                                     @RequestParam(name = "orgName", required = false) String orgName,
                                     @RequestParam(name = "orgId", required = false) Long orgId,
                                     @RequestParam(name = "memPhone", required = false) String memPhone,
                                     @RequestParam(name = "payTimeS", required = false) Long payTimeS,
                                     @RequestParam(name = "payTimeE", required = false) Long payTimeE,
                                     @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                     @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                     @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            Map<String, Object> param = new HashMap<>();
            param.put("orderNo", orderNo);
            param.put("goodsName", goodsName);
            param.put("memPhone", memPhone);
            param.put("orderStatus", orderStatus);
            if (payTimeS != null) {
                param.put("payTimeS", DateUtil.format(new Date(payTimeS), "yyyy-MM-dd HH:mm:ss"));
            }

            if (payTimeE != null) {
                param.put("payTimeE", DateUtil.format(new Date(payTimeE), "yyyy-MM-dd HH:mm:ss"));
            }

            if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) {
                param.put("companyId", userModel.getBsCompany().getId());
                param.put("orgName", orgName);
                param.put("orgId", orgId);
                param.put("agentName", agentName);
                param.put("agentId", agentId);
                param.put("salesmanName", salesmanName);
                param.put("salesmanId", salesmanId);

            } else if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(0)) {
                param.put("orgId", userModel.getBsOrganization().getId());
                param.put("agentName", agentName);
                param.put("agentId", agentId);
                param.put("salesmanName", salesmanName);
                param.put("salesmanId", salesmanId);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type6.getType())) {
                param.put("agentId", userModel.getHighTyAgent().getId());
                param.put("salesmanName", salesmanName);
                param.put("salesmanId", salesmanId);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type7.getType())) {
                param.put("salesmanId", userModel.getHighTySalesman().getId());
            }

            PageHelper.startPage(pageNum,pageSize);

            PageInfo<TyOrderModel> pageInfo = new PageInfo<>(highTyAgentOilStationService.getOrderList(param));
            for (TyOrderModel orderModel : pageInfo.getList()) {
                orderModel.setOilLiters(orderModel.getTotalPrice().divide(orderModel.getGasPriceGun(), 2, BigDecimal.ROUND_HALF_DOWN));
            }
            return ResponseMsgUtil.success(pageInfo);

        } catch (Exception e) {
            log.error("HighTyAgentController --> getAgentList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/excelOrder", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出订单")
    public ResponseData excelOrder(@RequestParam(name = "orderNo", required = false) String orderNo,
                                   @RequestParam(name = "goodsName", required = false) String goodsName,
                                   @RequestParam(name = "salesmanName", required = false) String salesmanName,
                                   @RequestParam(name = "salesmanId", required = false) Long salesmanId,
                                   @RequestParam(name = "agentName", required = false) String agentName,
                                   @RequestParam(name = "agentId", required = false) Long agentId,
                                   @RequestParam(name = "orgName", required = false) String orgName,
                                   @RequestParam(name = "orgId", required = false) Long orgId,
                                   @RequestParam(name = "memPhone", required = false) String memPhone,
                                   @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                   @RequestParam(name = "payTimeS", required = false) Long payTimeS,
                                   @RequestParam(name = "payTimeE", required = false) Long payTimeE) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            Map<String, Object> param = new HashMap<>();
            param.put("orderNo", orderNo);
            param.put("goodsName", goodsName);
            param.put("memPhone", memPhone);
            param.put("orderStatus", orderStatus);
            if (payTimeS != null) {
                param.put("payTimeS", DateUtil.format(new Date(payTimeS), "yyyy-MM-dd HH:mm:ss"));
            }

            if (payTimeE != null) {
                param.put("payTimeE", DateUtil.format(new Date(payTimeE), "yyyy-MM-dd HH:mm:ss"));
            }

            if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) {
                param.put("companyId", userModel.getBsCompany().getId());
                param.put("orgName", orgName);
                param.put("orgId", orgId);
                param.put("agentName", agentName);
                param.put("agentId", agentId);
                param.put("salesmanName", salesmanName);
                param.put("salesmanId", salesmanId);

            } else if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(0)) {
                param.put("orgId", userModel.getBsOrganization().getId());
                param.put("agentName", agentName);
                param.put("agentId", agentId);
                param.put("salesmanName", salesmanName);
                param.put("salesmanId", salesmanId);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type6.getType())) {
                param.put("agentId", userModel.getHighTyAgent().getId());
                param.put("salesmanName", salesmanName);
                param.put("salesmanId", salesmanId);

            } else if (userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type7.getType())) {
                param.put("salesmanId", userModel.getHighTySalesman().getId());
            }

            // 订单列表
            List<TyOrderModel> orderList = highTyAgentOilStationService.getOrderList(param);

            List<String> headList = new ArrayList<>();
            if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) {
                headList.add("代理商公司");
            }
            if (userModel.getBsCompany() != null) {
                headList.add("代理商");
            }
            if ((userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) || userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type6.getType())) {
                headList.add("业务员");
            }
            if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) {
                headList.add("用户手机号");
            }
            headList.add("加油站");
            headList.add("平台订单号");
            headList.add("团油订单号");
            headList.add("支付类型");
            headList.add("加油金额");
            headList.add("加油升数");
            headList.add("优惠金额");
            headList.add("实付金额");
            headList.add("油号");
            headList.add("枪号");
            headList.add("平台枪价");
            headList.add("团油枪价");
            headList.add("创建时间");
            headList.add("支付时间");
            headList.add("订单状态");
            headList.add("退款时间");

            List<List<Object>> dataList = new ArrayList<>();
            List<Object> data;

            for (TyOrderModel orderModel : orderList) {
                data = new ArrayList<>();
                if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) {
                    data.add(orderModel.getOrgName());
                }
                if (userModel.getBsCompany() != null) {
                    data.add(orderModel.getAgentName());
                }
                if ((userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) || userModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type6.getType())) {
                    data.add(orderModel.getSalesmanName());
                }
                if (userModel.getBsCompany() != null && userModel.getSecUser().getAdminFlag().equals(1)) {
                    data.add(orderModel.getMemPhone());
                }
                data.add(orderModel.getGoodsName());
                data.add(orderModel.getOrderNo());
                data.add(orderModel.getGasOrderNo());
                data.add(orderModel.getPayType());
                data.add(orderModel.getTotalPrice());
                data.add(orderModel.getTotalPrice().divide(orderModel.getGasPriceGun(), 2, BigDecimal.ROUND_HALF_DOWN));
                data.add(orderModel.getDeductionPrice());
                data.add(orderModel.getPayRealPrice());
                data.add(orderModel.getGasOilNo());
                data.add(orderModel.getGasGunNo());
                data.add(orderModel.getPlatformPrice());
                data.add(orderModel.getGasPriceGun());
                data.add(orderModel.getCreateTime());
                data.add(orderModel.getPayTime());

                // 订单状态：1 待支付  2 已支付  3.已完成  4. 已退款  5.已取消   6.退款中   7.拒绝退款
                if (orderModel.getOrderStatus().equals(3)) {
                    data.add("已完成");
                } else if (orderModel.getOrderStatus().equals(4)) {
                    data.add("已退款");
                } else if (orderModel.getOrderStatus().equals(6)) {
                    data.add("退款中");
                } else if (orderModel.getOrderStatus().equals(7)) {
                    data.add("退款驳回");
                }
                dataList.add(data);
                data.add(orderModel.getRefundTime());
            }

            String fileUrl = SysConst.getSysConfig().getFileUrl() + "/temporary/";
            String pathName = System.currentTimeMillis()+".xlsx";
            File file = new File(fileUrl);
            if (!file.exists()) {
                file.mkdirs();
            }
            EasyExcel.write(fileUrl+pathName).head(generationHead(headList)).sheet("加油订单").doWrite(dataList);

            return ResponseMsgUtil.success(pathName);

        } catch (Exception e) {
            log.error("HighTyAgentController --> getAgentList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    /**
     * 生成头部
     * @param headList
     * @return
     */
    private List<List<String>> generationHead(List<String> headList) {
        List<List<String>> list = new ArrayList<>();
        List<String> head;
        for (String headStr : headList) {
            head = new ArrayList<>();
            head.add(headStr);
            list.add(head);
        }
        return list;
    }
}
