package com.cool.store.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cool.store.constants.CommonConstants;
import com.cool.store.dao.ShopInfoDAO;
import com.cool.store.dao.ShopStageInfoDAO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.entity.ShopStageInfoDO;
import com.cool.store.enums.point.ShopStageEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.service.DataHandleService;
import com.cool.store.utils.poi.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.unit.DataUnit;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2024/10/10 16:24
 * @Version 1.0
 */
@Service
public class DataHandleServiceImpl implements DataHandleService {
    @Resource
    ShopInfoDAO shopInfoDAO;
    @Resource
    ShopStageInfoDAO shopStageInfoDAO;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean dataHandleService(Long shopId) {
        //查询所有的门店线索数据（如果指定shopId 查指定门店的数据）
        List<ShopInfoDO> shopInfoDOS = shopInfoDAO.selectByIdOrSelectAll(shopId);
        for (ShopInfoDO x : shopInfoDOS) {
            List<ShopStageInfoDO> shopStageInfo = shopStageInfoDAO.getShopStageInfo(x.getId(), null);
            if (CollectionUtils.isEmpty(shopStageInfo)) {
                continue;
            }
            Map<Integer, ShopStageInfoDO> infoDOMap = ListUtils.emptyIfNull(shopStageInfo).stream().collect(Collectors.toMap(k -> k.getShopSubStage(), v -> v, (k1, k2) -> k1));

            //判断加盟合同是否完成
            ShopStageInfoDO shopStageInfoDO8 = infoDOMap.get(ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage());
            //大于800完成
            Boolean flag = shopStageInfoDO8.getShopSubStageStatus()>800;


            //先初始化新增的阶段
            initShopStageInfo(x.getLineId(),x.getId(),flag);

            //老阶段改造-证照
            ShopStageInfoDO shopStageInfoDO = infoDOMap.get(ShopSubStageEnum.SHOP_STAGE_4.getShopSubStage());
            this.licenseHandle(shopStageInfoDO);

            //老阶段改造-施工
            ShopStageInfoDO shopStageInfoDO12 = infoDOMap.get(ShopSubStageEnum.SHOP_STAGE_12.getShopSubStage());
            Integer b = 1200;
            //如果是待预约 直接换成待验收
            if (b.equals(shopStageInfoDO12.getShopSubStageStatus())){
                ShopSubStageStatusEnum shopSubStageStatus121 = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_121;
                shopStageInfoDO12.setShopSubStageStatus(shopSubStageStatus121.getShopSubStageStatus());
                shopStageInfoDO12.setRemark(shopSubStageStatus121.getShopSubStageName() + CommonConstants.PATH_BAR +shopSubStageStatus121.getShopSubStageStatusName());
                shopStageInfoDAO.updateByPrimaryKeySelective(shopStageInfoDO12);
            }

        }
        return Boolean.TRUE;
    }


    public void licenseHandle(ShopStageInfoDO shopStageInfoDO){
        if (Objects.isNull(shopStageInfoDO)){
            return;
        }
        String planCompleteTime = shopStageInfoDO.getPlanCompleteTime();
        LocalDate plusFDays = DateUtils.getPlusFDays(DateUtils.strToDate(shopStageInfoDO.getPlanCompleteTime(), DateUtils.YYYY_MM_DD), 32);
        if (ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00.getShopSubStageStatus().equals(shopStageInfoDO.getShopSubStageStatus())){
            //未完成 拆分数据 老的证照办理变为食品许可证
            shopStageInfoDO.setRemark(ShopSubStageEnum.SHOP_STAGE_4.getShopSubStageName() + CommonConstants.PATH_BAR +ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00.getShopSubStageStatusName());
            shopStageInfoDO.setPlanCompleteTime(plusFDays.toString());
            shopStageInfoDAO.updateByPrimaryKeySelective(shopStageInfoDO);
            //新增营业执照证照办理
            shopStageInfoDO.setId(null);
            ShopSubStageStatusEnum shopSubStageStatus30 = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00;
            shopStageInfoDO.setShopSubStage(ShopSubStageEnum.SHOP_STAGE_3.getShopSubStage());
            shopStageInfoDO.setShopSubStageStatus(shopSubStageStatus30.getShopSubStageStatus());
            shopStageInfoDO.setPlanCompleteTime(planCompleteTime);
            shopStageInfoDO.setRemark(ShopSubStageEnum.SHOP_STAGE_3.getShopSubStageName() + CommonConstants.PATH_BAR +shopSubStageStatus30.getShopSubStageStatusName());
            shopStageInfoDAO.insertSelective(shopStageInfoDO);
        }
        if (ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_40.getShopSubStageStatus().equals(shopStageInfoDO.getShopSubStageStatus())){
            //未完成 拆分数据 老的证照办理变为食品许可证
            ShopSubStageEnum shopSubStageEnum = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_40.getShopSubStageEnum();
            shopStageInfoDO.setRemark(shopSubStageEnum.getShopSubStageName() + CommonConstants.PATH_BAR +ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_40.getShopSubStageStatusName());
            shopStageInfoDO.setPlanCompleteTime(plusFDays.toString());
            shopStageInfoDAO.updateByPrimaryKeySelective(shopStageInfoDO);
            //新增营业执照证照办理
            shopStageInfoDO.setId(null);
            ShopSubStageStatusEnum shopSubStageStatus30 = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_30;
            shopStageInfoDO.setShopSubStage(shopSubStageStatus30.getShopSubStageEnum().getShopSubStage());
            shopStageInfoDO.setShopSubStageStatus(shopSubStageStatus30.getShopSubStageStatus());
            shopStageInfoDO.setRemark(shopSubStageStatus30.getShopSubStageName() + CommonConstants.PATH_BAR +shopSubStageStatus30.getShopSubStageStatusName());
            shopStageInfoDO.setPlanCompleteTime(planCompleteTime);
            shopStageInfoDAO.insertSelective(shopStageInfoDO);
        }
        //待审核
        Integer a = 410;
        if (a.equals(shopStageInfoDO.getShopSubStageStatus())){
            //未完成 拆分数据 老的证照办理变为食品许可证
            ShopSubStageStatusEnum shopSubStageStatus43 = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_43;
            shopStageInfoDO.setShopSubStageStatus(shopSubStageStatus43.getShopSubStageStatus());
            shopStageInfoDO.setRemark(shopSubStageStatus43.getShopSubStageName() + CommonConstants.PATH_BAR +shopSubStageStatus43.getShopSubStageStatusName());
            shopStageInfoDO.setIsTerminated(Boolean.TRUE);
            shopStageInfoDO.setPlanCompleteTime(planCompleteTime);
            shopStageInfoDO.setActualCompleteTime(DateUtil.formatDate(new Date()));
            shopStageInfoDAO.updateByPrimaryKeySelective(shopStageInfoDO);
            //新增营业执照证照办理
            shopStageInfoDO.setId(null);
            ShopSubStageStatusEnum shopSubStageStatus30 = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_33;
            shopStageInfoDO.setShopSubStage(shopSubStageStatus30.getShopSubStageEnum().getShopSubStage());
            shopStageInfoDO.setShopSubStageStatus(shopSubStageStatus30.getShopSubStageStatus());
            shopStageInfoDO.setRemark(shopSubStageStatus30.getShopSubStageName() + CommonConstants.PATH_BAR +shopSubStageStatus30.getShopSubStageStatusName());
            shopStageInfoDO.setIsTerminated(Boolean.TRUE);
            shopStageInfoDO.setPlanCompleteTime(planCompleteTime);
            shopStageInfoDO.setActualCompleteTime(DateUtil.formatDate(new Date()));
            shopStageInfoDAO.insertSelective(shopStageInfoDO);
        }
        if (ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_43.getShopSubStageStatus().equals(shopStageInfoDO.getShopSubStageStatus())){
            //未完成 拆分数据 老的证照办理变为食品许可证
            ShopSubStageEnum shopSubStageEnum = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_43.getShopSubStageEnum();
            shopStageInfoDO.setRemark(shopSubStageEnum.getShopSubStageName() + CommonConstants.PATH_BAR +ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_43.getShopSubStageStatusName());
            shopStageInfoDO.setPlanCompleteTime(plusFDays.toString());
            shopStageInfoDAO.updateByPrimaryKeySelective(shopStageInfoDO);
            //新增营业执照证照办理
            shopStageInfoDO.setId(null);
            ShopSubStageStatusEnum shopSubStageStatus30 = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_33;
            shopStageInfoDO.setShopSubStage(shopSubStageStatus30.getShopSubStageEnum().getShopSubStage());
            shopStageInfoDO.setShopSubStageStatus(shopSubStageStatus30.getShopSubStageStatus());
            shopStageInfoDO.setRemark(shopSubStageStatus30.getShopSubStageName() + CommonConstants.PATH_BAR +shopSubStageStatus30.getShopSubStageStatusName());
            shopStageInfoDO.setIsTerminated(Boolean.TRUE);
            shopStageInfoDO.setPlanCompleteTime(planCompleteTime);
            shopStageInfoDO.setActualCompleteTime(DateUtil.formatDate(new Date()));
            shopStageInfoDAO.insertSelective(shopStageInfoDO);
        }
    }



    public Integer initShopStageInfo(Long lineId, Long shopId,Boolean flag) {
        List<ShopStageInfoDO> addShopStageList = new ArrayList<>();
        LocalDate selectStartDate = LocalDate.now();
        List<ShopSubStageEnum> list = Arrays.asList(
                ShopSubStageEnum.SHOP_STAGE_15,
                ShopSubStageEnum.SHOP_STAGE_16,
                ShopSubStageEnum.SHOP_STAGE_17,
                ShopSubStageEnum.SHOP_STAGE_18,
                ShopSubStageEnum.SHOP_STAGE_19,
                ShopSubStageEnum.SHOP_STAGE_20,
                ShopSubStageEnum.SHOP_STAGE_21,
                ShopSubStageEnum.SHOP_STAGE_22,
                ShopSubStageEnum.SHOP_STAGE_23,
                ShopSubStageEnum.SHOP_STAGE_24
        );

        for (ShopSubStageEnum shopSubStageEnum :list) {
            ShopStageInfoDO shopStageInfo = new ShopStageInfoDO();
            shopStageInfo.setLineId(lineId);
            shopStageInfo.setShopId(shopId);
            ShopStageEnum shopStageEnum = shopSubStageEnum.getShopStageEnum();
            shopStageInfo.setShopStage(shopStageEnum.getShopStage());
            shopStageInfo.setShopSubStage(shopSubStageEnum.getShopSubStage());
            ShopSubStageStatusEnum initStatus = shopSubStageEnum.getInitStatus();
            if (ShopSubStageEnum.SHOP_STAGE_15.equals(shopSubStageEnum)&&flag){
                initStatus = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_150;
            }
            shopStageInfo.setShopSubStageStatus(initStatus.getShopSubStageStatus());
            shopStageInfo.setRemark(shopSubStageEnum.getShopSubStageName() + CommonConstants.PATH_BAR +initStatus.getShopSubStageStatusName());
            shopStageInfo.setIsTerminated(Boolean.FALSE);
            shopStageInfo.setPlanCompleteTime(shopSubStageEnum.getPlanCompleteTime(selectStartDate));
            addShopStageList.add(shopStageInfo);
        }
        return shopStageInfoDAO.batchInsert(addShopStageList);
    }
}
