package com.cool.store.service.impl;


import com.cool.store.constants.CommonConstants;
import com.cool.store.context.CurrentUserHolder;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.HyOpenAreaInfoDAO;
import com.cool.store.dao.LineInfoDAO;
import com.cool.store.dao.PreFryQualificationApplyDAO;
import com.cool.store.dao.ShopInfoDAO;
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.dto.pre.fry.ApplyManagementQueryDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.FileTypeEnum;
import com.cool.store.enums.ImportStatusEnum;
import com.cool.store.enums.point.PointStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.ImportTaskMapper;
import com.cool.store.mapper.PointInfoMapper;
import com.cool.store.request.*;
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.easyExcel.EasyExcelUtil;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.utils.poi.StringUtils;
import com.cool.store.vo.Preparation.PreparationScheduleVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: WangShuo
 * @Date: 2024/10/24/下午5:03
 * @Version 1.0
 * @注释:
 */
@Service
public class ExportServiceImpl implements ExportService {

    private static final Logger log = LoggerFactory.getLogger(ExportServiceImpl.class);
    @Resource
    private ExportRealizeService exportRealizeService;
    @Resource
    private PointInfoMapper pointInfoMapper;
    @Resource
    private ImportTaskMapper importTaskMapper;
    @Autowired
    private LineInfoDAO lineInfoDAO;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    UserAuthMappingService userAuthMappingService;
    @Resource
    private RegionService regionService;
    @Resource
    HyOpenAreaInfoDAO hyOpenAreaInfoDAO;
    @Resource
    private EasyExcelUtil easyExcelUtil;
    @Value("${mybatis.configuration.variables.enterpriseId}")
    private String eid;
    @Autowired
    private ShopInfoDAO shopInfoDAO;
    @Autowired
    private ShopService shopService;
    @Resource
    private FranchiseReportService franchiseReportService;
    @Resource
    private PreFryQualificationApplyDAO preFryQualificationApplyDAO;



    @Override
    public Long branchShopList(BranchShopRequest request, LoginUserInfo user) {
        request.setPageSize(CommonConstants.MAX_EXPORT_SIZE + 100);
        request.setPageNum(CommonConstants.ONE);
        PageInfo<BranchShopResponse> branchShopList = shopService.getBranchShopList(request, user.getUserId());
        long total = branchShopList.getTotal();
        List<BranchShopResponse> exportList = new ArrayList<>();
        if (total == CommonConstants.ZERO) {
            return CommonConstants.ZERO_LONG;
        } else if (total > CommonConstants.MAX_EXPORT_SIZE) {
            exportList.addAll(branchShopList.getList().subList(0, CommonConstants.MAX_EXPORT_SIZE));
        } else {
            exportList.addAll(branchShopList.getList());
        }
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setStatus(ImportStatusEnum.Ongoing.getCode());
        importTaskDO.setFileName(FileTypeEnum.BRANCH_SHOP_LIST.getDesc() + DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, new Date()));
        importTaskDO.setIsImport(Boolean.FALSE);
        importTaskDO.setFileType(FileTypeEnum.BRANCH_SHOP_LIST.getFileType());
        importTaskDO.setCreateUserId(user.getUserId());
        importTaskDO.setCreateTime(new Date().getTime());
        importTaskDO.setCreateName(user.getName());
        importTaskMapper.insert(eid, importTaskDO);
        exportRealizeService.branchShopList(branchShopList.getList(), importTaskDO);
        return total;
    }

    @Override
    public Long exportProgressBase(BranchShopRequest request, LoginUserInfo user, FileTypeEnum fileTypeEnum) {
        request.setPageSize(CommonConstants.FIVE_SECONDS+10);
        request.setPageNum(CommonConstants.ONE);
        PageInfo<BranchShopResponse> branchShopList = shopService.getBranchShopList(request, user.getUserId());
        long total = branchShopList.getTotal();
        if (total == CommonConstants.ZERO) {
            return CommonConstants.ZERO_LONG;
        } else if (total > CommonConstants.FIVE_SECONDS) {
            throw new ServiceException(ErrorCodeEnum.EXPORT_LIMIT_5000);
        }
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setStatus(ImportStatusEnum.Ongoing.getCode());
        importTaskDO.setFileName(fileTypeEnum.getDesc() + DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, new Date()));
        importTaskDO.setIsImport(Boolean.FALSE);
        importTaskDO.setFileType(fileTypeEnum.getFileType());
        importTaskDO.setCreateUserId(user.getUserId());
        importTaskDO.setCreateTime(new Date().getTime());
        importTaskDO.setCreateName(user.getName());
        importTaskMapper.insert(eid, importTaskDO);
        switch (fileTypeEnum) {
            case EXPORT_FINANCE:
                exportRealizeService.exportFinance(branchShopList.getList(), importTaskDO);
                break;
            case EXPORT_JOIN_SHOP:
                exportRealizeService.exportJoinShop(branchShopList.getList(), importTaskDO);
                break;
            case EXPORT_PROGRESS:
                exportRealizeService.exportProgress(branchShopList.getList(), importTaskDO);
                break;
            default:
                log.error("导出类型错误");
                break;
        }
        return total;
    }

    @Override
    public Long exportZxjpOpenShopReport(FranchiseReportRequest request, LoginUserInfo user) {
        List<FranchiseReportResponse> franchiseReport = franchiseReportService.zxjpOpenShopReport(request);
        if (CollectionUtils.isEmpty(franchiseReport)){
            log.error("导出数据为空");
            return 0L;
        }
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setStatus(ImportStatusEnum.Ongoing.getCode());
        importTaskDO.setFileName(FileTypeEnum.ZXJP_OPEN_SHOP_REPORT.getDesc() + DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, new Date()));
        importTaskDO.setIsImport(Boolean.FALSE);
        importTaskDO.setFileType(FileTypeEnum.ZXJP_OPEN_SHOP_REPORT.getFileType());
        importTaskDO.setCreateUserId(user.getUserId());
        importTaskDO.setCreateTime(new Date().getTime());
        importTaskDO.setCreateName(user.getName());
        importTaskMapper.insert(eid, importTaskDO);
        exportRealizeService.exportZxjpOpenShopReport(franchiseReport, importTaskDO,request);
        return (long) franchiseReport.size();
    }

    @Override
    public Long exportZxjpOpenShopReportPage(FranchiseReportRequest request, LoginUserInfo user) {
        request.setPageSize(CommonConstants.INT_MAX_VALUE);
        request.setPageNum(CommonConstants.ONE);
        PageInfo<ShopReportResponse> pageInfo = franchiseReportService.zxjpOpenShopReportPage(request);
            List<ShopReportResponse> list = pageInfo.getList();
        if (CollectionUtils.isEmpty(list)){
            log.error("导出数据为空");
            return 0L;
        }
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setStatus(ImportStatusEnum.Ongoing.getCode());
        importTaskDO.setFileName(FileTypeEnum.SELLING_DETAILS_REPORT.getDesc() + DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, new Date()));
        importTaskDO.setIsImport(Boolean.FALSE);
        importTaskDO.setFileType(FileTypeEnum.SELLING_DETAILS_REPORT.getFileType());
        importTaskDO.setCreateUserId(user.getUserId());
        importTaskDO.setCreateTime(new Date().getTime());
        importTaskDO.setCreateName(user.getName());
        importTaskMapper.insert(eid, importTaskDO);
        exportRealizeService.exportZxjpOpenShopReportPage(list, importTaskDO,request);
        return (long) list.size();
    }

    @Override
    public Long exportPreFryQualificationApply(ApplyManagementQueryDTO request, LoginUserInfo user) {
        List<ApplyManagementDTO> list = preFryQualificationApplyDAO.selectApplyManagementList(request);
        if (CollectionUtils.isEmpty(list)){
            log.error("导出数据为空");
            return 0L;
        }
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setStatus(ImportStatusEnum.Ongoing.getCode());
        importTaskDO.setFileName(FileTypeEnum.PRE_FRY_QUALIFICATION_APPLY_EXPORT.getDesc() + DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1, new Date()));
        importTaskDO.setIsImport(Boolean.FALSE);
        importTaskDO.setFileType(FileTypeEnum.PRE_FRY_QUALIFICATION_APPLY_EXPORT.getFileType());
        importTaskDO.setCreateUserId(user.getUserId());
        importTaskDO.setCreateTime(new Date().getTime());
        importTaskDO.setCreateName(user.getName());
        importTaskMapper.insert(eid, importTaskDO);
        exportRealizeService.exportPreFryQualificationApply(list, importTaskDO);
        return (long) list.size();
    }


}
