package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.dao.*;

import com.cool.store.dto.*;
import com.cool.store.dto.Preparation.PreparationDTO;
import com.cool.store.dto.Preparation.ScheduleDTO;

import com.cool.store.dto.pre.fry.ApplyManagementDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.*;
import com.cool.store.enums.point.*;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.*;
import com.cool.store.request.FranchiseReportRequest;
import com.cool.store.request.InitiatingRequest;
import com.cool.store.response.BranchShopResponse;
import com.cool.store.response.FranchiseReportResponse;
import com.cool.store.response.ShopReportResponse;
import com.cool.store.service.*;
import com.cool.store.utils.JSONUtils;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.StringUtil;
import com.cool.store.utils.easyExcel.EasyExcelUtil;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.utils.poi.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.MessageFormat;
import java.text.ParseException;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.cool.store.utils.poi.DateUtils.*;

/**
 * @Author: WangShuo
 * @Date: 2024/10/29/下午1:58
 * @Version 1.0
 * @注释:
 */
@Service
@Slf4j
public class ExportRealizeServiceImpl implements ExportRealizeService {

    @Resource
    private ShopAccountDAO shopaccountDAO;
    @Resource
    private WarehouseInfoMapper warehouseInfoMapper;
    @Resource
    private OrderSysInfoDAO orderSysInfoDAO;
    @Resource
    private BuildInformationDAO buildInformationDAO;
    @Resource
    private PointInfoDAO pointInfoDAO;
    @Resource
    private FranchiseFeeMapper franchiseFeeMapper;
    @Resource
    private ImportTaskMapper importTaskMapper;
    @Autowired
    private LineInfoDAO lineInfoDAO;
    @Autowired
    private HyPartnerUserChannelDAO hyPartnerUserChannelDAO;
    @Resource
    EnterpriseUserDAO enterpriseUserDAO;
    @Resource
    SignFranchiseMapper signFranchiseMapper;
    @Resource
    UserAuthMappingService userAuthMappingService;
    @Resource
    private RegionService regionService;
    @Resource
    HyOpenAreaInfoDAO hyOpenAreaInfoDAO;
    @Resource
    DeskService deskService;
    @Resource
    private EasyExcelUtil easyExcelUtil;
    @Value("${mybatis.configuration.variables.enterpriseId}")
    private String eid;
    @Resource
    private IntentAgreementMapper intentAgreementMapper;
    @Resource
    private ShopInfoDAO shopInfoDAO;
    @Autowired
    private ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    private QualificationsInfoDAO qualificationsInfoDAO;
    @Resource
    private LinePayDAO linePayDAO;
    @Resource
    RedisUtilPool redisUtilPool;

    @Autowired
    private RegionDao regionDao;

    @Resource
    private InvoicingDAO invoicingDAO;

    @Override
    @Async
    public void branchShopList(List<BranchShopResponse> list, ImportTaskDO importTaskDO) {
        Boolean flag = Boolean.TRUE;
        String url = "";
        try {
            List<ExportBranchShopDTO> exportList = new ArrayList<>();
            List<Long> shopIds = list.stream().map(BranchShopResponse::getShopId).collect(Collectors.toList());
            List<SignFranchiseDO> signFranchises = signFranchiseMapper.selectByShopIds(shopIds);
            List<FranchiseFeeDTO> franchiseFees = franchiseFeeMapper.getPayTimeByShopIds(shopIds);
            List<ShopStageInfoDO> franchiseFeeStages = shopStageInfoDAO.getSubStageList(shopIds, ShopSubStageEnum.SHOP_STAGE_7.getShopSubStage());
            Map<Long, Integer> franchiseFeeStageMap = franchiseFeeStages.stream().filter(o -> o.getShopId() != null).collect(Collectors.toMap(ShopStageInfoDO::getShopId, ShopStageInfoDO::getShopSubStageStatus));
            List<ShopStageInfoDO> signFranchisesStages = shopStageInfoDAO.getSubStageList(shopIds, ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage());
            Map<Long, Integer> signFranchisesStageMap = signFranchisesStages.stream().filter(o -> o.getShopId() != null).collect(Collectors.toMap(ShopStageInfoDO::getShopId, ShopStageInfoDO::getShopSubStageStatus));
            Map<Long, List<Date>> payTimeMap = new HashMap<>();
            for (FranchiseFeeDTO dto : franchiseFees) {
                List<Date> payTimeList = JsonToDate(dto.getCombinedField());
                if (dto.getPayTime() != null) {
                    payTimeList.add(dto.getPayTime());
                }
                Collections.sort(payTimeList);
                payTimeMap.put(dto.getShopId(), payTimeList);
            }

            Map<Long, FranchiseFeeDTO> franchiseFeeDTOMap = franchiseFees.stream().filter(o -> o.getShopId() != null)
                    .collect(Collectors.toMap(FranchiseFeeDTO::getShopId, Function.identity()));
            Map<Long, SignFranchiseDO> signFranchiseMap = new HashMap<>();
            if (signFranchises != null) {
                signFranchiseMap = signFranchises.stream().collect(Collectors.toMap(SignFranchiseDO::getShopId, Function.identity()));
            }
            List<InvoicingDO> invoicingDOList = invoicingDAO.listByShopIds(shopIds);
            Map<Long, InvoicingDO> InvoicingMap = new HashMap<>();
            if (invoicingDOList != null) {
                InvoicingMap = invoicingDOList.stream().collect(Collectors.toMap(InvoicingDO::getShopId, Function.identity()));
            }
            for (BranchShopResponse response : list) {
                ExportBranchShopDTO dto = new ExportBranchShopDTO();
                dto.setShopAddress(response.getShopAddress());
                FranchiseFeeDTO franchiseFeeDTO = franchiseFeeDTOMap.get(response.getShopId());
                if (ObjectUtils.isNotEmpty(franchiseFeeDTO)) {
                    dto.setFirstYearStartTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, franchiseFeeDTO.getFirstYearStartTime()));
                    dto.setFirstYearEndTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, franchiseFeeDTO.getFirstYearEndTime()));
                    String total = (new BigDecimal(franchiseFeeDTO.getPerformanceBond()).add(new BigDecimal(franchiseFeeDTO.getFirstYearFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getFirstYearManagementFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getYearFranchiseFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getLoanMargin())).toString());
                    dto.setTotalAmountContributions(total);
                    dto.setFranchiseFeeBill(franchiseFeeDTO.getYearFranchiseFee());
                    dto.setLoanMargin(franchiseFeeDTO.getLoanMargin());
                    dto.setPerformanceBondBill(franchiseFeeDTO.getPerformanceBond());
                    dto.setFirstYearManagementFeeBill(franchiseFeeDTO.getFirstYearManagementFee());
                    dto.setFirstYearBrandingFeeBill(franchiseFeeDTO.getFirstYearFee());
                    dto.setPayUserName(franchiseFeeDTO.getPayUserName());
                    dto.setDiscountReason(franchiseFeeDTO.getDiscountReason());
                }
                dto.setUserName(response.getUsername());
                dto.setMobile(response.getMobile());
                dto.setShopName(response.getShopName());
                dto.setShopCode(response.getShopCode());
                dto.setRegionName(response.getRegionName());
                dto.setJoinMode(JoinModeEnum.getByCode(response.getJoinMode()));
                dto.setFranchiseBrand(FranchiseBrandEnum.getDescByCode(response.getFranchiseBrand()));
                dto.setInvestmentManager(response.getInvestmentManagerName());
                SignFranchiseDO signFranchiseDO = signFranchiseMap.get(response.getShopId());
                if (signFranchiseDO != null) {
                    dto.setContractAmount(signFranchiseDO.getContractAmount());
                    dto.setContractCode(signFranchiseDO.getContractCode());
                    dto.setContractStartTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, signFranchiseDO.getContractStartTime()));
                    dto.setContractEndTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, signFranchiseDO.getContractEndTime()));
                    dto.setSignTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, signFranchiseDO.getCreateTime()));
                    dto.setPartnershipSignatoryFirst(signFranchiseDO.getPartnershipSignatoryFirst());
                    dto.setPartnershipSignatorySecond(signFranchiseDO.getPartnershipSignatorySecond());
                }
                List<Date> payTime = payTimeMap.getOrDefault(response.getShopId(), new ArrayList<>());
                for (int i = 0; i < payTime.size() && i <= 3; i++) {
                    switch (i) {
                        case 0:
                            dto.setFirstPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                        case 1:
                            dto.setSecondPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                        case 2:
                            dto.setThirdPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                        case 3:
                            dto.setFourthPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                    }
                }
                InvoicingDO invoicingDO = InvoicingMap.get(response.getShopId());
                if (invoicingDO != null) {
                    dto.setInvoicingTime(invoicingDO.getInvoiceDate());
                    dto.setFirstYearManagementFeeTax(toString(invoicingDO.getManagementFeeTax()));
                    dto.setFirstYearManagementFee(toString(invoicingDO.getManagementFee()));
                    dto.setFirstYearBrandingFeeTax(toString(invoicingDO.getBrandUsageFeeTax()));
                    dto.setFirstYearBrandingFee(toString(invoicingDO.getBrandUsageFee()));
                    dto.setFranchiseFeeTax(toString(invoicingDO.getFranchiseFeeTax()));
                    dto.setFranchiseFee(toString(invoicingDO.getFranchiseFee()));
                    dto.setDesignFeeTax(toString(invoicingDO.getDesignServiceFeeTax()));
                    dto.setDesignFee(toString(invoicingDO.getDesignServiceFee()));
                }
                Integer franchiseFeeStageStatus = franchiseFeeStageMap.getOrDefault(response.getShopId(), 0);
                switch (franchiseFeeStageStatus) {
                    case -100:
                        dto.setFranchiseFeeStageStatus("未开始");
                        break;
                    case 700:
                        dto.setFranchiseFeeStageStatus("待提交账单");
                        break;
                    case 710:
                        dto.setFranchiseFeeStageStatus("待缴费");
                        break;
                    case 720:
                        dto.setFranchiseFeeStageStatus("待审核");
                        break;
                    case 730:
                        dto.setFranchiseFeeStageStatus("已缴费");
                        break;
                    case 740:
                        dto.setFranchiseFeeStageStatus("审核未通过");
                        break;
                    default:
                        dto.setFranchiseFeeStageStatus("");
                }
                Integer signFranchisesStageStatus = signFranchisesStageMap.getOrDefault(response.getShopId(), 0);
                switch (signFranchisesStageStatus) {
                    case -100:
                        dto.setSignFranchiseStageStatus("未开始");
                        break;
                    case 800:
                        dto.setSignFranchiseStageStatus("待提交");
                        break;
                    case 840:
                        dto.setSignFranchiseStageStatus("已签约");
                        break;
                    default:
                        dto.setSignFranchiseStageStatus("");
                }
                dto.setCurrentProgress(response.getCompletionColumn() + "/" + response.getTotalColumn());
                dto.setShopStatus(response.getShopStatus());
                dto.setPlanOpenTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, response.getPlanOpenTime()));
                dto.setOpenDuration(response.getDays());
                dto.setCurrency("元");
                exportList.add(dto);
            }
            url = easyExcelUtil.exportExcel(ExportBranchShopDTO.class, exportList, null, FileTypeEnum.BRANCH_SHOP_LIST.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()), FileTypeEnum.BRANCH_SHOP_LIST.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()));
        } catch (Throwable e) {
            flag = Boolean.FALSE;
            log.error("fileUpload upload err, originFileName={}", FileTypeEnum.BRANCH_SHOP_LIST.getDesc(), e);
            throw new ServiceException(ErrorCodeEnum.INTERNAL_SERVER_ERROR);
        } finally {
            if (flag) {
                importTaskDO.setStatus(ImportStatusEnum.success.getCode());
                importTaskDO.setFileUrl(url);
            } else {
                importTaskDO.setStatus(ImportStatusEnum.fail.getCode());
            }
            importTaskMapper.update(eid, importTaskDO);
        }

    }

    @Override
    @Async
    public void exportFinance(List<BranchShopResponse> list, ImportTaskDO importTaskDO) {
        Boolean flag = Boolean.TRUE;
        String url = "";
        List<ExportFinanceDTO> exportList = new ArrayList<>();
        try {
            List<Long> shopIds = list.stream().map(BranchShopResponse::getShopId).collect(Collectors.toList());
            List<SignFranchiseDO> signFranchises = signFranchiseMapper.selectByShopIds(shopIds);
            List<FranchiseFeeDTO> franchiseFees = franchiseFeeMapper.getPayTimeByShopIds(shopIds);
            List<ShopStageInfoDO> franchiseFeeStages = shopStageInfoDAO.getSubStageList(shopIds, ShopSubStageEnum.SHOP_STAGE_7.getShopSubStage());
            Map<Long, Integer> franchiseFeeStageMap = franchiseFeeStages.stream().filter(o -> o.getShopId() != null).collect(Collectors.toMap(ShopStageInfoDO::getShopId, ShopStageInfoDO::getShopSubStageStatus));
            List<ShopStageInfoDO> signFranchisesStages = shopStageInfoDAO.getSubStageList(shopIds, ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage());
            Map<Long, Integer> signFranchisesStageMap = signFranchisesStages.stream().filter(o -> o.getShopId() != null).collect(Collectors.toMap(ShopStageInfoDO::getShopId, ShopStageInfoDO::getShopSubStageStatus));
            List<ShopStageInfoDO> buildStages = shopStageInfoDAO.getSubStageList(shopIds, ShopSubStageEnum.SHOP_STAGE_15.getShopSubStage());
            Map<Long, Integer> buildStageMap = buildStages.stream().collect(Collectors.toMap(ShopStageInfoDO::getShopId, ShopStageInfoDO::getShopSubStageStatus));
            Map<Long, List<Date>> payTimeMap = new HashMap<>();
            for (FranchiseFeeDTO dto : franchiseFees) {
                List<Date> payTimeList = JsonToDate(dto.getCombinedField());
                if (dto.getPayTime() != null) {
                    payTimeList.add(dto.getPayTime());
                }
                Collections.sort(payTimeList);
                payTimeMap.put(dto.getShopId(), payTimeList);
            }
            Map<Long, FranchiseFeeDTO> franchiseFeeDTOMap = franchiseFees.stream().filter(o -> o.getShopId() != null)
                    .collect(Collectors.toMap(FranchiseFeeDTO::getShopId, Function.identity()));
            Map<Long, SignFranchiseDO> signFranchiseMap = new HashMap<>();
            if (signFranchises != null) {
                signFranchiseMap = signFranchises.stream().collect(Collectors.toMap(SignFranchiseDO::getShopId, Function.identity()));
            }
            for (BranchShopResponse response : list) {
                ExportFinanceDTO dto = new ExportFinanceDTO();
                dto.setShopAddress(response.getShopAddress());
                dto.setUsername(response.getUsername());
                dto.setMobile(response.getMobile());

                FranchiseFeeDTO franchiseFeeDTO = franchiseFeeDTOMap.get(response.getShopId());
                if (ObjectUtils.isNotEmpty(franchiseFeeDTO)) {
                    dto.setFirstYearStartTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, franchiseFeeDTO.getFirstYearStartTime()));
                    dto.setFirstYearEndTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, franchiseFeeDTO.getFirstYearEndTime()));
                    String total = (new BigDecimal(franchiseFeeDTO.getPerformanceBond()).add(new BigDecimal(franchiseFeeDTO.getFirstYearFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getFirstYearManagementFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getYearFranchiseFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getLoanMargin())).toString());
                    dto.setTotalAmountContributions(total);
                    dto.setFranchiseFee(franchiseFeeDTO.getYearFranchiseFee());
                    dto.setLoanMargin(franchiseFeeDTO.getLoanMargin());
                    dto.setPerformanceBond(franchiseFeeDTO.getPerformanceBond());
                    dto.setFirstYearManagementFee(franchiseFeeDTO.getFirstYearManagementFee());
                    dto.setFirstYearFee(franchiseFeeDTO.getFirstYearFee());
                    dto.setPayUserName(franchiseFeeDTO.getPayUserName());
                    dto.setDiscountReason(franchiseFeeDTO.getDiscountReason());
                }
                dto.setShopName(response.getShopName());
                dto.setShopCode(response.getShopCode());
                dto.setRegionName(response.getRegionName());
                dto.setJoinMode(JoinModeEnum.getByCode(response.getJoinMode()));
                dto.setFranchiseBrand(FranchiseBrandEnum.getDescByCode(response.getFranchiseBrand()));
                dto.setInvestmentManagerName(response.getInvestmentManagerName());
                SignFranchiseDO signFranchiseDO = signFranchiseMap.get(response.getShopId());
                if (signFranchiseDO != null) {
                    dto.setContractAmount(signFranchiseDO.getContractAmount());
                    dto.setContractCode(signFranchiseDO.getContractCode());
                    dto.setSignTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, signFranchiseDO.getCreateTime()));
                    dto.setPartnershipSignatoryFirst(signFranchiseDO.getPartnershipSignatoryFirst());
                    dto.setPartnershipSignatorySecond(signFranchiseDO.getPartnershipSignatorySecond());
                    dto.setSignType(SignTypeEnum.getDescByCode(signFranchiseDO.getSignType()));
                }
                List<Date> payTime = payTimeMap.getOrDefault(response.getShopId(), new ArrayList<>());
                for (int i = 0; i < payTime.size() && i <= 3; i++) {
                    switch (i) {
                        case 0:
                            dto.setFirstPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                        case 1:
                            dto.setSecondPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                        case 2:
                            dto.setThirdPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                        case 3:
                            dto.setFourthPayTime(DateUtils.parseDateToStr(SPECIAL_DATE_START_1, payTime.get(i)));
                            break;
                    }
                }
                Integer franchiseFeeStageStatus = franchiseFeeStageMap.getOrDefault(response.getShopId(), 0);
                ShopSubStageStatusEnum franchiseFeeStageStatusEnum = ShopSubStageStatusEnum.getShopSubStageStatusEnum(franchiseFeeStageStatus);
                dto.setFranchiseFeeStageStatus(franchiseFeeStageStatusEnum.getShopSubStageStatusName());
                Integer signFranchisesStageStatus = signFranchisesStageMap.getOrDefault(response.getShopId(), 0);
                ShopSubStageStatusEnum signFranchisesStageStatusEnum = ShopSubStageStatusEnum.getShopSubStageStatusEnum(signFranchisesStageStatus);
                dto.setSignFranchiseStageStatus(signFranchisesStageStatusEnum.getShopSubStageStatusName());
                Integer buildStage = buildStageMap.get(response.getShopId());
                dto.setBuildInformationStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(buildStage).getShopSubStageStatusName());
                dto.setCurrentProgress(response.getCompletionColumn() + "/" + response.getTotalColumn());
                dto.setShopStatus(response.getShopStatus());
                exportList.add(dto);
            }
            url = easyExcelUtil.exportExcel(ExportFinanceDTO.class, exportList, null,
                    FileTypeEnum.EXPORT_FINANCE.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    FileTypeEnum.EXPORT_FINANCE.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()));
        } catch (Throwable e) {
            flag = Boolean.FALSE;
            log.error("fileUpload upload err, originFileName={}", FileTypeEnum.EXPORT_FINANCE.getDesc(), e);
            throw new ServiceException(ErrorCodeEnum.INTERNAL_SERVER_ERROR);
        } finally {
            if (flag) {
                importTaskDO.setStatus(ImportStatusEnum.success.getCode());
                importTaskDO.setFileUrl(url);
            } else {
                importTaskDO.setStatus(ImportStatusEnum.fail.getCode());
            }
            importTaskMapper.update(eid, importTaskDO);
        }
    }

    @Override
    @Async
    public void exportJoinShop(List<BranchShopResponse> list, ImportTaskDO importTaskDO) {
        Boolean flag = Boolean.TRUE;
        String url = "";
        List<ExportJoinShopDTO> exportList = new ArrayList<>();
        try {
            List<WarehouseInfoDO> warehouseInfoMapperAll = warehouseInfoMapper.getAll();
            Map<String, String> warehouseInfoMap = warehouseInfoMapperAll.stream().collect(Collectors.toMap(WarehouseInfoDO::getWarehouseCode, WarehouseInfoDO::getWarehouseName));
            Set<Long> pointIds = list.stream().map(BranchShopResponse::getPointId).collect(Collectors.toSet());
            List<PointInfoDTO> pointInfoDOS = pointInfoDAO.getPointAndDetailByIds(new ArrayList<>(pointIds));
            Map<Long, PointInfoDTO> pointInfoDOMap = pointInfoDOS.stream().collect(Collectors.toMap(PointInfoDTO::getPointId, dto -> dto));
            List<Long> shopIds = list.stream().map(BranchShopResponse::getShopId).collect(Collectors.toList());
            List<SignFranchiseDO> signFranchises = signFranchiseMapper.selectByShopIds(shopIds);
            List<FranchiseFeeDTO> franchiseFees = franchiseFeeMapper.getPayTimeByShopIds(shopIds);
            List<ShopStageInfoDO> buildStages = shopStageInfoDAO.getSubStageList(shopIds, ShopSubStageEnum.SHOP_STAGE_15.getShopSubStage());
            Map<Long, ShopStageInfoDO> buildStageMap = buildStages.stream().collect(Collectors.toMap(ShopStageInfoDO::getShopId, Function.identity()));
            Map<Long, FranchiseFeeDTO> franchiseFeeDTOMap = franchiseFees.stream().filter(o -> o.getShopId() != null)
                    .collect(Collectors.toMap(FranchiseFeeDTO::getShopId, Function.identity()));
            Set<Long> lineIds = list.stream().map(BranchShopResponse::getLineId).collect(Collectors.toSet());
            List<LineInfoDO> lineInfoDOS = lineInfoDAO.getByLineIds(new ArrayList<>(lineIds));
            Map<Long, LineInfoDO> lineMap = lineInfoDOS.stream().collect(Collectors.toMap(LineInfoDO::getId, Function.identity()));
            List<QualificationsInfoDO> qualificationsInfoDOList = qualificationsInfoDAO.getByLineIds(new ArrayList<>(lineIds));
            Map<Long, QualificationsInfoDO> qualificationsInfoDOMap = qualificationsInfoDOList.stream().collect(Collectors.toMap(QualificationsInfoDO::getLineId, Function.identity()));
            Map<Long, SignFranchiseDO> signFranchiseMap = new HashMap<>();
            if (signFranchises != null) {
                signFranchiseMap = signFranchises.stream().collect(Collectors.toMap(SignFranchiseDO::getShopId, Function.identity()));
            }
            List<OrderSysInfoDO> orderSysInfoDOList = orderSysInfoDAO.getSpecificByShopIdList(shopIds);
            Map<Long, OrderSysInfoDO> orderSysInfoDOMap = orderSysInfoDOList.stream().collect(Collectors.toMap(OrderSysInfoDO::getShopId, Function.identity()));


            for (BranchShopResponse response : list) {
                ExportJoinShopDTO dto = new ExportJoinShopDTO();
                OrderSysInfoDO orderSysInfoDO = orderSysInfoDOMap.get(response.getShopId());
                if (orderSysInfoDO != null) {
                    dto.setDeclareGoodsType(DeclareGoodsTypeEnum.getNameByCode(orderSysInfoDO.getDeclareGoodsType()));
                    dto.setDeclareGoodsLogisticsWarehouseName(warehouseInfoMap.getOrDefault(orderSysInfoDO.getDeclareGoodsLogisticsWarehouse(), ""));
                    if (StringUtils.isNotBlank(orderSysInfoDO.getDeclareGoodsDate())
                            && !DeclareGoodsTypeEnum.SELF_PICKUP.getCode().equals(orderSysInfoDO.getDeclareGoodsType())) {
                        List<DeclareGoodsDateDTO> declareGoodsDates = JSONUtils.parseToListOrMap(orderSysInfoDO.getDeclareGoodsDate(), new TypeReference<List<DeclareGoodsDateDTO>>() {
                        });
                        if (CollectionUtils.isNotEmpty(declareGoodsDates)) {
                            StringBuilder declareGoodsTime = new StringBuilder();
                            StringBuilder deliverGoodsTime = new StringBuilder();
                            for (DeclareGoodsDateDTO declareGoodsDateDTO : declareGoodsDates) {
                                declareGoodsTime.append("报货日期:").append(declareGoodsDateDTO.getDeclareGoods()).append(",");
                                deliverGoodsTime.append("送货日期:").append(declareGoodsDateDTO.getDeliverGoods()).append(",");
                            }
                            dto.setYlsDeclareGoodsTime(declareGoodsTime.toString());
                            dto.setWarehouseDeliveryTime(deliverGoodsTime.toString());
                        }
                    }
                    dto.setReceivingFirmName(orderSysInfoDO.getReceivingFirmName());
                    dto.setReceivingMsBankAccount(orderSysInfoDO.getReceivingMsBankAccount());
                }
                PointInfoDTO pointInfoDTO = pointInfoDOMap.get(response.getPointId());
                if (pointInfoDTO != null) {
                    dto.setProvince(pointInfoDTO.getProvince());
                    dto.setCity(pointInfoDTO.getCity());
                    dto.setDistrict(pointInfoDTO.getDistrict());
                    dto.setArea(pointInfoDTO.getPointArea());
                    dto.setMonthRent(pointInfoDTO.getMonthRent());
                }

                dto.setUsername(response.getUsername());
                dto.setMobile(response.getMobile());
                dto.setShopAddress(response.getShopAddress());
                FranchiseFeeDTO franchiseFeeDTO = franchiseFeeDTOMap.get(response.getShopId());
                dto.setStoreType(StoreTypeEnum.getMessage(response.getStoreType()));
                if (ObjectUtils.isNotEmpty(franchiseFeeDTO)) {
                    String total = (new BigDecimal(franchiseFeeDTO.getPerformanceBond()).add(new BigDecimal(franchiseFeeDTO.getFirstYearFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getFirstYearManagementFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getYearFranchiseFee()))
                            .add(new BigDecimal(franchiseFeeDTO.getLoanMargin())).toString());
                    dto.setTotalAmountContributions(total);
                    dto.setFranchiseFee(franchiseFeeDTO.getYearFranchiseFee());
                    dto.setLoanMargin(franchiseFeeDTO.getLoanMargin());
                    dto.setPerformanceBond(franchiseFeeDTO.getPerformanceBond());
                    dto.setFirstYearManagementFee(franchiseFeeDTO.getFirstYearManagementFee());
                    dto.setFirstYearFee(franchiseFeeDTO.getFirstYearFee());
                    dto.setDiscountReason(franchiseFeeDTO.getDiscountReason());
                }
                dto.setShopName(response.getShopName());
                dto.setShopCode(response.getShopCode());
                dto.setRegionName(response.getRegionName());
                dto.setJoinMode(JoinModeEnum.getByCode(response.getJoinMode()));
                dto.setInvestmentManagerName(response.getInvestmentManagerName());
                SignFranchiseDO signFranchiseDO = signFranchiseMap.get(response.getShopId());
                if (signFranchiseDO != null) {
                    dto.setContractCode(signFranchiseDO.getContractCode());
                    dto.setPartnershipSignatorySecond(signFranchiseDO.getPartnershipSignatorySecond());
                    dto.setPartnershipSignatorySecondMobile(signFranchiseDO.getPartnershipSignatorySecondMobile());
                    dto.setPartnershipSignatorySecondIdCard(signFranchiseDO.getPartnershipSignatorySecondIdNumber());
                    dto.setSignType(SignTypeEnum.getDescByCode(signFranchiseDO.getSignType()));
                    dto.setProtectiveDistance(signFranchiseDO.getProtectiveDistance());
                    dto.setPartnershipSignatoryFirstWhichStore(signFranchiseDO.getPartnershipSignatoryFirstWhichStore());
                    dto.setContractSignTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, signFranchiseDO.getCreateTime()));
                    dto.setContractStartTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, signFranchiseDO.getContractStartTime()));
                    dto.setContractEndTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, signFranchiseDO.getContractEndTime()));
                }
                ShopStageInfoDO buildStageInfoDO = buildStageMap.get(response.getShopId());
                if (buildStageInfoDO != null) {
                    Integer buildStage = buildStageInfoDO.getShopSubStageStatus();
                    dto.setBuildInformationStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(buildStage).getShopSubStageStatusName());
                    if (buildStageInfoDO.getIsTerminated()) {
                        dto.setBuildInformationEndTime(buildStageInfoDO.getActualCompleteTime());
                    }
                }
                LineInfoDO lineInfoDO = lineMap.getOrDefault(response.getLineId(), new LineInfoDO());
                dto.setPartnershipSignatoryFirst(lineInfoDO.getUsername());
                dto.setPartnershipSignatoryFirstMobile(lineInfoDO.getMobile());
                QualificationsInfoDO qualificationsInfoDO = qualificationsInfoDOMap.getOrDefault(response.getLineId(), new QualificationsInfoDO());
                dto.setPartnershipSignatoryFirstIdCard(qualificationsInfoDO.getIdCardNo());
                exportList.add(dto);
            }
            url = easyExcelUtil.exportExcel(ExportJoinShopDTO.class, exportList, null,
                    FileTypeEnum.EXPORT_JOIN_SHOP.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    FileTypeEnum.EXPORT_JOIN_SHOP.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()));
        } catch (Throwable e) {
            flag = Boolean.FALSE;
            log.error("fileUpload upload err, originFileName={}", FileTypeEnum.EXPORT_JOIN_SHOP.getDesc(), e);
            throw new ServiceException(ErrorCodeEnum.INTERNAL_SERVER_ERROR);
        } finally {
            if (flag) {
                importTaskDO.setStatus(ImportStatusEnum.success.getCode());
                importTaskDO.setFileUrl(url);
            } else {
                importTaskDO.setStatus(ImportStatusEnum.fail.getCode());
            }
            importTaskMapper.update(eid, importTaskDO);
        }
    }

    @Override
    @Async
    public void exportProgress(List<BranchShopResponse> list, ImportTaskDO importTaskDO) {
        Boolean flag = Boolean.TRUE;
        String url = "";
        List<ExportProgressDTO> exportList = new ArrayList<>();
        try {
            List<Long> shopIds = list.stream().map(BranchShopResponse::getShopId).collect(Collectors.toList());
            List<BuildInformationDO> buildInformationDOList = buildInformationDAO.getSpecificByShopIdList(shopIds);
            Map<Long, String> cShopNameMap = new HashMap<>();
            for (BuildInformationDO buildInformationDO : buildInformationDOList) {
                String cShopName = buildInformationDO.getCShopName();
                if (StringUtils.isBlank(cShopName)) {
                    cShopName = "";
                }
                cShopNameMap.put(buildInformationDO.getShopId(), cShopName);
            }
            List<SignFranchiseDO> signFranchises = signFranchiseMapper.selectByShopIds(shopIds);
            Map<Long, SignFranchiseDO> signFranchiseMap = new HashMap<>();
            if (signFranchises != null) {
                signFranchiseMap = signFranchises.stream().collect(Collectors.toMap(SignFranchiseDO::getShopId, Function.identity()));
            }
            List<ShopAccountDO> shopAccountList = shopaccountDAO.getSpecificByShopIds(shopIds);
            Map<Long, List<ShopAccountDO>> shopAccountMap = shopAccountList.stream().collect(Collectors.groupingBy(ShopAccountDO::getShopId, Collectors.toList()));
            List<ShopStageInfoDO> shopSubStages = shopStageInfoDAO.getShopSubStages(shopIds);
            Map<Long, List<ShopStageInfoDO>> shopSubStageMap = shopSubStages.stream().collect(Collectors.groupingBy(ShopStageInfoDO::getShopId, Collectors.toList()));
            for (BranchShopResponse response : list) {
                ExportProgressDTO dto = new ExportProgressDTO();
                SignFranchiseDO signFranchiseDO = signFranchiseMap.get(response.getShopId());
                if (signFranchiseDO != null) {
                    dto.setSignType(SignTypeEnum.getDescByCode(signFranchiseDO.getSignType()));
                }
                dto.setStoreType(StoreTypeEnum.getMessage(response.getStoreType()));
                dto.setUsername(response.getUsername());
                dto.setMobile(response.getMobile());
                dto.setShopName(response.getShopName());
                dto.setShopCode(response.getShopCode());
                dto.setRegionName(response.getRegionName());
                dto.setJoinMode(JoinModeEnum.getByCode(response.getJoinMode()));
                dto.setFranchiseBrand(FranchiseBrandEnum.getDescByCode(response.getFranchiseBrand()));
                dto.setInvestmentManagerName(response.getInvestmentManagerName());
                dto.setShopStatus(response.getShopStatus());
                List<ShopStageInfoDO> shopStageInfos = shopSubStageMap.get(response.getShopId());
                if (CollectionUtils.isNotEmpty(shopStageInfos)) {
                    Map<Integer, ShopStageInfoDO> map = shopStageInfos.stream().collect(Collectors.toMap(ShopStageInfoDO::getShopSubStage, Function.identity()));
                    dto.setAddressStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_1.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setLeaseStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_2.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setJoinFeeStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_7.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setJoinContractStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setBusinessLicenseStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_3.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setFoodLicenseStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_4.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    ShopStageInfoDO buildStage = map.get(ShopSubStageEnum.SHOP_STAGE_15.getShopSubStage());
                    if (buildStage != null) {
                        dto.setBuildStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(buildStage.getShopSubStageStatus()).getShopSubStageStatusName());
                        if (buildStage.getIsTerminated()) {
                            dto.setBuildEndTime(buildStage.getActualCompleteTime());
                        }
                    }
                    dto.setDesignStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_9.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setConstructionStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_11.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setDecorationStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_12.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setTrainStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_5.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setOpenPlanStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_14.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setYztStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_24.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                    dto.setXfStatus(ShopSubStageStatusEnum.getShopSubStageStatusEnum(map.getOrDefault(ShopSubStageEnum.SHOP_STAGE_23.getShopSubStage(), new ShopStageInfoDO()).getShopSubStageStatus()).getShopSubStageStatusName());
                }
                List<ShopAccountDO> shopAccountDOS = shopAccountMap.getOrDefault(response.getShopId(), new ArrayList<>());
                if (CollectionUtils.isNotEmpty(shopAccountDOS)) {
                    Map<String, ShopAccountDO> accountDOMap = shopAccountDOS.stream().collect(Collectors.toMap(ShopAccountDO::getSystemName, Function.identity()));
                    dto.setPosStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.HUOMA.getSystemName(), new ShopAccountDO()).getStatus()));
                    dto.setYlsStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.YLS.getSystemName(), new ShopAccountDO()).getStatus()));
                    dto.setXzgStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.XZG.getSystemName(), new ShopAccountDO()).getStatus()));
                    dto.setMtgStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.MTTG.getSystemName(), new ShopAccountDO()).getStatus()));
                    dto.setDygStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.DY.getSystemName(), new ShopAccountDO()).getStatus()));
                    dto.setKsgStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.KS.getSystemName(), new ShopAccountDO()).getStatus()));
                    dto.setElmStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.ELM.getSystemName(), new ShopAccountDO()).getStatus()));
                    dto.setMtwStatus(OpenStatusEnum.getByCode(accountDOMap.getOrDefault(ShopAccountEnum.MT.getSystemName(), new ShopAccountDO()).getStatus()));
                }
                dto.setMiniProgramsShopName(cShopNameMap.getOrDefault(response.getShopId(), ""));
                dto.setCurrentProgress(response.getCompletionColumn() + "/" + response.getTotalColumn());
                exportList.add(dto);
            }
            url = easyExcelUtil.exportExcel(ExportProgressDTO.class, exportList, null,
                    FileTypeEnum.EXPORT_PROGRESS.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    FileTypeEnum.EXPORT_PROGRESS.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()));
        } catch (Throwable e) {
            flag = Boolean.FALSE;
            log.error("fileUpload upload err, originFileName={}", FileTypeEnum.EXPORT_PROGRESS.getDesc(), e);
            throw new ServiceException(ErrorCodeEnum.INTERNAL_SERVER_ERROR);
        } finally {
            if (flag) {
                importTaskDO.setStatus(ImportStatusEnum.success.getCode());
                importTaskDO.setFileUrl(url);
            } else {
                importTaskDO.setStatus(ImportStatusEnum.fail.getCode());
            }
            importTaskMapper.update(eid, importTaskDO);
        }
    }

    @Override
    @Async
    public void exportZxjpOpenShopReport(List<FranchiseReportResponse> list, ImportTaskDO importTaskDO, FranchiseReportRequest request) {
        String url = "";
        try {
            FranchiseReportResponse total = new FranchiseReportResponse();
            total.setYesterdayDate(list.get(0).getYesterdayDate());
            total.setJoinGroupName("总计");
            for (FranchiseReportResponse response : list){
                response.setTotal(response.getFranchiseFeeTotal().add(response.getManagementBrandingDesignFeeTotal()).add(response.getLoanMarginTotal()));
                total.setTotal(total.getTotal().add(response.getTotal()));
                total.setStoreNum(total.getStoreNum() + response.getStoreNum());
                total.setNormalStoreNum(total.getNormalStoreNum() + response.getNormalStoreNum());
                total.setRestaurantStoreNum(total.getRestaurantStoreNum() + response.getRestaurantStoreNum());
                total.setNoShowStoreNum(total.getNoShowStoreNum() + response.getNoShowStoreNum());
                total.setFranchiseFeeTotal(total.getFranchiseFeeTotal().add(response.getFranchiseFeeTotal()));
                total.setFirstYearBrandingFeeTotal(total.getFirstYearBrandingFeeTotal().add(response.getFirstYearBrandingFeeTotal()));
                total.setFirstYearManagementFeeTotal(total.getFirstYearManagementFeeTotal().add(response.getFirstYearManagementFeeTotal()));
                total.setPerformanceBondTotal(total.getPerformanceBondTotal().add(response.getPerformanceBondTotal()));
                total.setManagementBrandingDesignFeeTotal(total.getManagementBrandingDesignFeeTotal().add(response.getManagementBrandingDesignFeeTotal()));
                total.setLoanMarginTotal(total.getLoanMarginTotal().add(response.getLoanMarginTotal()));
                total.setYesterdayStoreNum(total.getYesterdayStoreNum() + response.getYesterdayStoreNum());
            }
            list.add(total);
            url = easyExcelUtil.exportExcelSpecial(FranchiseReportResponse.class, list, null,
                    FileTypeEnum.ZXJP_OPEN_SHOP_REPORT.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    FileTypeEnum.ZXJP_OPEN_SHOP_REPORT.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    "建店资料完成日期：",request.getBuildStartTime(),request.getBuildEndTime());
        } catch (Throwable e) {
            log.error("fileUpload upload err, originFileName={}", FileTypeEnum.ZXJP_OPEN_SHOP_REPORT.getDesc(), e);
            throw new ServiceException(ErrorCodeEnum.INTERNAL_SERVER_ERROR);
        } finally {
            importTaskDO.setStatus(ImportStatusEnum.success.getCode());
            importTaskDO.setFileUrl(url);
            importTaskMapper.update(eid, importTaskDO);
        }
    }

    @Override
    @Async
    public void exportZxjpOpenShopReportPage(List<ShopReportResponse> list, ImportTaskDO importTaskDO, FranchiseReportRequest request) {
        String url = "";
        try {
            ShopReportResponse total = new ShopReportResponse();
            total.setBuildTime("总计");
            for (ShopReportResponse response : list){
                total.setTotal(total.getTotal().add(response.getTotal()));
                total.setFranchiseFee(total.getFranchiseFee().add(response.getFranchiseFee()));
                total.setFirstYearBrandingFee(total.getFirstYearBrandingFee().add(response.getFirstYearBrandingFee()));
                total.setFirstYearManagementFee(total.getFirstYearManagementFee().add(response.getFirstYearManagementFee()));
                total.setPerformanceBond(total.getPerformanceBond().add(response.getPerformanceBond()));
                total.setManagementBrandingDesignFeeTotal(total.getManagementBrandingDesignFeeTotal().add(response.getManagementBrandingDesignFeeTotal()));
                total.setLoanMargin(total.getLoanMargin().add(response.getLoanMargin()));
            }
            list.add(total);
            url = easyExcelUtil.exportExcelSpecial(ShopReportResponse.class, list, null,
                    FileTypeEnum.SELLING_DETAILS_REPORT.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    FileTypeEnum.SELLING_DETAILS_REPORT.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    "建店资料完成日期：",request.getBuildStartTime(),request.getBuildEndTime());
        } catch (Throwable e) {
            log.error("fileUpload upload err, originFileName={}", FileTypeEnum.SELLING_DETAILS_REPORT.getDesc(), e);
            throw new ServiceException(ErrorCodeEnum.INTERNAL_SERVER_ERROR);
        } finally {
            importTaskDO.setStatus(ImportStatusEnum.success.getCode());
            importTaskDO.setFileUrl(url);
            importTaskMapper.update(eid, importTaskDO);
        }
    }

    @Override
    @Async
    public void exportPreFryQualificationApply(List<ApplyManagementDTO> list, ImportTaskDO importTaskDO) {
        String url = "";
        try {
            for (ApplyManagementDTO dto : list){
              dto.setApplyTypeName(PreFryApplyTypeEnum.getDescriptionByCode(dto.getApplyType()));
              dto.setCreateTimeStr(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START,dto.getCreateTime()));
              switch (dto.getAuditStatus()){
                  case 0:
                      dto.setAuditStatusName("审批中");
                      break;
                  case 1:
                      dto.setAuditStatusName("审核通过");
                      break;
                  case 2:
                      dto.setAuditStatusName("审核不通过");
                      break;
              }
            }
            url = easyExcelUtil.exportExcel(ApplyManagementDTO.class, list, null,
                    FileTypeEnum.PRE_FRY_QUALIFICATION_APPLY_EXPORT.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()),
                    FileTypeEnum.PRE_FRY_QUALIFICATION_APPLY_EXPORT.getDesc() + DateUtils.parseDateToStr(SPECIAL_DATE_START_1, new Date()));
        } catch (Throwable e) {
            log.error("fileUpload upload err, originFileName={}", FileTypeEnum.PRE_FRY_QUALIFICATION_APPLY_EXPORT.getDesc(), e);
            throw new ServiceException(ErrorCodeEnum.INTERNAL_SERVER_ERROR);
        } finally {
            importTaskDO.setStatus(ImportStatusEnum.success.getCode());
            importTaskDO.setFileUrl(url);
            importTaskMapper.update(eid, importTaskDO);
        }
    }

    private String toString(Object o) {
        if (o == null) {
            return "";
        }
        return o.toString();
    }

    private List<Date> JsonToDate(String json) {
        if (StringUtils.isBlank(json)) {
            return new ArrayList<>();
        }
        ObjectMapper mapper = new ObjectMapper();
        // 解析JSON字符串为JsonNode对象
        JsonNode jsonNode = null;
        try {
            jsonNode = mapper.readTree(json);
            List<Date> payTimeList = new ArrayList<>();
            // 遍历数组节点
            for (JsonNode node : jsonNode) {
                long payTime = node.get("payTime").asLong();
                // 将时间戳转换为Date对象
                Date date = new Date(payTime);
                payTimeList.add(date);
            }
            return payTimeList;
        } catch (Exception e) {
            log.info("解析加盟费缴纳时间json失败");
            throw new RuntimeException(e);
        }
    }
}

