package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.BuildStageDTO;
import com.cool.store.dto.PlatformBuildStageDTO;
import com.cool.store.dto.Preparation.ScheduleDTO;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.entity.ShopStageInfoDO;
import com.cool.store.enums.FranchiseBrandEnum;
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.mapper.ShopStageInfoMapper;
import com.cool.store.request.DeskRequest;
import com.cool.store.vo.Preparation.PreparationProcessVO;
import com.cool.store.vo.shop.RentInfoToDoVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import tk.mybatis.mapper.entity.Example;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

/**
 * @author zhangchenbiao
 * @FileName: ShopStageInfoDAO
 * @Description:
 * @date 2024-04-10 17:33
 */
@Repository
public class ShopStageInfoDAO {

    @Resource
    private ShopStageInfoMapper shopStageInfoMapper;

    /**
     * 初始化店铺阶段信息
     * @param lineInfoDO
     * @param shopInfoList
     * @return flag =true:表示意向加盟节点完成正常初始化。false:表示意向加盟节点未完成新建分店阶段都为未开始-100。
     */
    public Integer initShopStageInfo(LineInfoDO lineInfoDO, List<ShopInfoDO> shopInfoList, Boolean flag) {
        if (CollectionUtils.isEmpty(shopInfoList)) {
            return CommonConstants.ZERO;
        }
        List<ShopStageInfoDO> addShopStageList = new ArrayList<>();
        LocalDate selectStartDate = LocalDate.now();
        for (ShopInfoDO shopInfoDO : shopInfoList) {
            Long shopId = shopInfoDO.getId();
            for (ShopSubStageEnum shopSubStageEnum : ShopSubStageEnum.values()) {
                if (ShopSubStageEnum.SHOP_STAGE_24.equals(shopSubStageEnum) && String.valueOf(FranchiseBrandEnum.ZXSMZ.getCode()).equals(shopInfoDO.getFranchiseBrand())) {
                    // 三明治跳过营帐通开通
                    continue;
                }
                // 开业运营方案删除
                if (shopSubStageEnum.equals(ShopSubStageEnum.SHOP_STAGE_14)) {
                    continue;
                }
                ShopStageInfoDO shopStageInfo = new ShopStageInfoDO();
                shopStageInfo.setLineId(lineInfoDO.getId());
                shopStageInfo.setShopId(shopId);
                ShopStageEnum shopStageEnum = shopSubStageEnum.getShopStageEnum();
                shopStageInfo.setShopStage(shopStageEnum.getShopStage());
                shopStageInfo.setShopSubStage(shopSubStageEnum.getShopSubStage());
                ShopSubStageStatusEnum initStatus  = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00;
                if (flag) {
                    initStatus = shopSubStageEnum.getInitStatus(lineInfoDO.getUseStandardStore());
                }
                //标准店和普通店
                if (initStatus == null){
                    continue;
                }
                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 shopStageInfoMapper.batchInsert(addShopStageList);
    }

    public Integer batchInsert(List<ShopStageInfoDO> addShopStageList) {
        if (CollectionUtils.isEmpty(addShopStageList)) {
            return 0;
        }
        return shopStageInfoMapper.batchInsert(addShopStageList);
    }

    public Integer insertSelective(ShopStageInfoDO shopStageInfoDO) {
        if (Objects.isNull(shopStageInfoDO)) {
            return 0;
        }
        return shopStageInfoMapper.insertSelective(shopStageInfoDO);
    }

    public Integer updateByPrimaryKeySelective(ShopStageInfoDO shopStageInfoDO) {
        if (Objects.isNull(shopStageInfoDO)) {
            return 0;
        }
        return shopStageInfoMapper.updateByPrimaryKeySelective(shopStageInfoDO);
    }


    public Integer batchUpdate(List<ShopStageInfoDO> shopStageList) {
        if (CollectionUtils.isEmpty(shopStageList)) {
            return 0;
        }
        return shopStageInfoMapper.batchUpdate(shopStageList);
    }

    /**
     * 获取店铺阶段信息
     * @param shopId
     * @return
     */
    public List<ShopStageInfoDO> getShopStageInfo(Long shopId, Integer shopStage) {
        if (Objects.isNull(shopId)) {
            return Lists.newArrayList();
        }
        return shopStageInfoMapper.getShopStageInfo(shopId, shopStage);
    }

    public PreparationProcessVO getPreparationProcess(Long shopId) {
        if (Objects.isNull(shopId)) {
            return null;
        }
        return shopStageInfoMapper.getPreparationProcess(shopId);
    }

    public Integer getAllCompletionCount(Long shopId) {
        if (Objects.isNull(shopId)) {
            return 0;
        }
        return shopStageInfoMapper.getAllCompletionCount(shopId);
    }

    public Integer getCompletionCount(Long shopId) {
        if (Objects.isNull(shopId)) {
            return 0;
        }
        return shopStageInfoMapper.getCompletionCount(shopId);
    }

    /**
     * @Auther: wangshuo
     * @Date: 2024/5/3
     * @description:更新完成时间byshopId
     */
    public Integer updateByShopId(ShopStageInfoDO shopStageInfoDO) {
        if (Objects.isNull(shopStageInfoDO)) {
            return CommonConstants.ZERO;
        }
        return shopStageInfoMapper.updateByShopId(shopStageInfoDO);
    }

    /**
     * 获取子阶段信息
     * @param shopId
     * @param shopSubStageEnum
     * @return
     */
    public ShopStageInfoDO getShopSubStageInfo(Long shopId, ShopSubStageEnum shopSubStageEnum) {
        if (Objects.isNull(shopId) || Objects.isNull(shopSubStageEnum)) {
            return null;
        }
        return shopStageInfoMapper.getShopSubStageInfo(shopId, shopSubStageEnum.getShopSubStage());
    }

    public Integer updateShopStageInfo(Long shopId, ShopSubStageStatusEnum shopStageInfo) {
        if (Objects.isNull(shopId) || Objects.isNull(shopStageInfo)) {
            return CommonConstants.ZERO;
        }
        String remark = shopStageInfo.getShopSubStageName() + CommonConstants.PATH_BAR + shopStageInfo.getShopSubStageStatusName();
        boolean isTerminated = shopStageInfo.isTerminated();
        return shopStageInfoMapper.updateShopStageInfo(shopId, shopStageInfo.getShopSubStageEnum().getShopSubStage(), shopStageInfo.getShopSubStageStatus(), isTerminated, remark);
    }

    //数据处理专用
    public Integer dataUpdateShopStageInfo(List<BuildStageDTO> list, ShopSubStageStatusEnum shopStageInfo) {
        if (CollectionUtils.isEmpty(list) || Objects.isNull(shopStageInfo)) {
            return CommonConstants.ZERO;
        }
        String remark = shopStageInfo.getShopSubStageName() + CommonConstants.PATH_BAR + shopStageInfo.getShopSubStageStatusName();
        boolean isTerminated = shopStageInfo.isTerminated();
        return shopStageInfoMapper.dataUpdateShopStageInfo(list, shopStageInfo.getShopSubStageEnum().getShopSubStage(), shopStageInfo.getShopSubStageStatus(), isTerminated, remark);
    }


    /**
     * 批量更新店铺状态
     * @param shopId
     * @param subStageStatusList
     * @return
     */
    public Integer batchUpdateShopStageStatus(Long shopId, List<ShopSubStageStatusEnum> subStageStatusList) {
        if (Objects.isNull(shopId) || CollectionUtils.isEmpty(subStageStatusList)) {
            return CommonConstants.ZERO;
        }
        List<ShopStageInfoDO> shopStageList = Lists.newArrayList();
        extracted(subStageStatusList, shopId, shopStageList);

        return shopStageInfoMapper.batchUpdateShopStageStatus(shopId, shopStageList);
    }


    /**
     * 批量更新店铺状态
     * @param shopIds
     * @param subStageStatusList
     * @return
     */
    public Integer batchUpdateByShopIdsAndSubStageStatus(List<Long> shopIds, List<ShopSubStageStatusEnum> subStageStatusList) {
        if (CollectionUtils.isEmpty(shopIds) || CollectionUtils.isEmpty(subStageStatusList)) {
            return CommonConstants.ZERO;
        }
        List<ShopStageInfoDO> shopStageList = Lists.newArrayList();
        for (Long shopId : shopIds) {
            extracted(subStageStatusList, shopId, shopStageList);
        }

        return shopStageInfoMapper.batchUpdateByShopIdsAndSubStageStatus(shopIds, shopStageList);
    }

    private static void extracted(List<ShopSubStageStatusEnum> subStageStatusList, Long shopId, List<ShopStageInfoDO> shopStageList) {
        for (ShopSubStageStatusEnum subStageStatus : subStageStatusList) {
            String remark = subStageStatus.getShopSubStageName() + CommonConstants.PATH_BAR + subStageStatus.getShopSubStageStatusName();
            ShopStageInfoDO shopStageInfo = new ShopStageInfoDO();
            shopStageInfo.setShopId(shopId);
            shopStageInfo.setShopSubStage(subStageStatus.getShopSubStageEnum().getShopSubStage());
            shopStageInfo.setShopSubStageStatus(subStageStatus.getShopSubStageStatus());
            shopStageInfo.setRemark(remark);
            shopStageInfo.setIsTerminated(subStageStatus.isTerminated());
            shopStageList.add(shopStageInfo);
        }
    }

    /**
     * 更新子阶段到未开始状态
     * @param shopId
     * @param shopSubStageEnum
     * @return
     */
    public Integer updateShopStageToNotStarted(Long shopId, ShopSubStageEnum shopSubStageEnum) {
        if (Objects.isNull(shopId) || Objects.isNull(shopSubStageEnum)) {
            return CommonConstants.ZERO;
        }
        return shopStageInfoMapper.updateShopStageToNotStarted(shopId, shopSubStageEnum.getShopSubStage());
    }

    /**
     * 更新店铺阶段状态及审核信息
     * @param shopId
     * @param shopStageInfo
     * @return
     */
    public Integer updateShopStageAndAuditInfo(Long shopId, ShopSubStageStatusEnum shopStageInfo, Long auditId) {
        if (Objects.isNull(shopId) || Objects.isNull(shopStageInfo)) {
            return CommonConstants.ZERO;
        }
        String remark = shopStageInfo.getShopSubStageName() + CommonConstants.PATH_BAR + shopStageInfo.getShopSubStageStatusName();
        boolean isTerminated = shopStageInfo.isTerminated();
        return shopStageInfoMapper.updateShopStageAndAuditInfo(shopId, shopStageInfo.getShopSubStageEnum().getShopSubStage(), shopStageInfo.getShopSubStageStatus(), isTerminated, remark, auditId);
    }

    public Page<RentInfoToDoVO> getRentContractToDoPage(String userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ShopSubStageStatusEnum shopSubStageStatus = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_21;
        return shopStageInfoMapper.getRentContractToDoPage(userId, shopSubStageStatus.getShopSubStageEnum().getShopSubStage(), shopSubStageStatus.getShopSubStageStatus());
    }

    public List<ShopStageInfoDO> getShopIdListByStageStatus(Integer shopSubStageStatus) {
        if (shopSubStageStatus == null || shopSubStageStatus == CommonConstants.ZERO) {
            return null;
        }
        return shopStageInfoMapper.getShopIdListByStageStatus(shopSubStageStatus);
    }

    public List<ScheduleDTO> getScheduleList(List<Long> shopIdList) {
        if (CollectionUtils.isEmpty(shopIdList)) {
            return new ArrayList<>();
        }
        return shopStageInfoMapper.getScheduleList(shopIdList);
    }
    public ScheduleDTO getScheduleAll(Long shopId) {
        if (Objects.isNull(shopId)) {
            return null;
        }
        return shopStageInfoMapper.getScheduleAll(shopId);
    }
    public List<ScheduleDTO> getPlatformBuildList(List<Long> shopIdList) {
        if (CollectionUtils.isEmpty(shopIdList)) {
            return Collections.emptyList();
        }
        return shopStageInfoMapper.getPlatformBuildList(shopIdList);
    }

    public List<ShopStageInfoDO> getShopContractActualCompletionTime(List<Long> shopIdList) {
        if (CollectionUtils.isEmpty(shopIdList)) {
            return Collections.emptyList();
        }
        return shopStageInfoMapper.getShopContractActualCompletionTime(shopIdList);
    }


    public List<ShopStageInfoDO> getOpenActivityActualCompletionTime(List<Long> shopIdList) {
        if (CollectionUtils.isEmpty(shopIdList)) {
            return new ArrayList<>();
        }
        return shopStageInfoMapper.getOpenActivityActualCompletionTime(shopIdList);
    }

    /**
     * 获取可以上传租赁合同的店铺id
     * @param shopIds
     * @return
     */
    public List<Long> getCanSubmitRentContractShopIds(List<Long> shopIds) {
        if (CollectionUtils.isEmpty(shopIds)) {
            return Lists.newArrayList();
        }
        return shopStageInfoMapper.getCanSubmitRentContractShopIds(shopIds);
    }

    /**
     * @Auther: wangshuo
     * @Date: 2024/5/5
     * @description:获取施工阶段未完成的店铺
     */
    public List<Long> getShopContractIncompletion() {
        return shopStageInfoMapper.getShopContractIncompletion();
    }

    /**
     * @Auther: wangshuo
     * @Date: 2024/5/13
     * @description:批量更新店铺某一阶段的状态
     */
    public Integer batchUpdateShopStageStatus(List<Long> shopIdList, Integer shopSubStageEnum, Integer shopSubStageStatusEnum) {
        if (CollectionUtils.isEmpty(shopIdList)) {
            return CommonConstants.ZERO;
        }
        return shopStageInfoMapper.batchUpdateStatus(shopIdList, shopSubStageEnum, shopSubStageStatusEnum);
    }

    public List<ShopStageInfoDO> getSpecialShopStageInfo(List<Long> shopIds, Integer shopSubStage,
                                                         List<Integer> shopSubStageStatusList,
                                                         String investmentUserId,
                                                         List<String> authRegionIds,Boolean ownShopFlag, DeskRequest deskRequest,
                                                         String operationsConsultantUserId) {
        return shopStageInfoMapper.getSpecialShopStageInfo(shopIds, shopSubStage, shopSubStageStatusList, investmentUserId, authRegionIds,ownShopFlag,  deskRequest, operationsConsultantUserId);
    }

    public List<ShopStageInfoDO> getOperationsConsultantStageInfo(List<Long> shopIds, Integer shopSubStage,
                                                         List<Integer> shopSubStageStatusList,
                                                         String investmentUserId, DeskRequest deskRequest) {
        return shopStageInfoMapper.getOperationsConsultantStageInfo(shopIds, shopSubStage, shopSubStageStatusList, investmentUserId,   deskRequest);
    }

    /**
     * @Auther: wangshuo
     * @Date: 2024/5/9
     * @description:获取处于XXXX阶段的列表
     */
    public List<ShopStageInfoDO> getSubStageList(List<Long> shopIds, Integer shopSubStage) {
        if (CollectionUtils.isEmpty(shopIds) || shopSubStage == null) {
            return Collections.emptyList();
        }
        return shopStageInfoMapper.getSubStageList(shopIds, shopSubStage);
    }

    public List<ShopStageInfoDO> getSubStages(List<Long> shopIds, Integer shopSubStage) {
        if (CollectionUtils.isEmpty(shopIds)) {
            return Collections.emptyList();
        }
        return shopStageInfoMapper.getSubStages(shopIds, shopSubStage);
    }

    /**
     * 获取线索未开业的数量
     * @param lineId
     * @return
     */
    public Integer getNotOpenShopCountByLineId(Long lineId) {
        if (Objects.isNull(lineId)) {
            return CommonConstants.ZERO;
        }
        ShopSubStageEnum shopSubStageEnum = ShopSubStageEnum.SHOP_STAGE_16;
        return shopStageInfoMapper.getShopCountByLineIdAndStageStatus(lineId, shopSubStageEnum.getShopStageEnum().getShopStage(), shopSubStageEnum.getShopSubStage(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00.getShopSubStageStatus());
    }

    /**
     * @Auther: wangshuo
     * @Date: 2024/5/11
     * @description:获取处于a阶段或b阶段，和c阶段的
     */
    public List<ShopStageInfoDO> getSubStageListBySubStageStatus(ShopSubStageStatusEnum a, ShopSubStageStatusEnum b, ShopSubStageStatusEnum c) {

        return shopStageInfoMapper.getSubStageListBySubStageStatus(a.getShopSubStageStatus(), b.getShopSubStageStatus(), c.getShopSubStageStatus());
    }

    public ShopStageInfoDO getByShopIdAndSubStage(Long shopId, Integer shopSubStage) {
        return shopStageInfoMapper.getByShopIdAndSubStage(shopId, shopSubStage);
    }

    public List<PlatformBuildStageDTO> getPlatformBuildStage(List<Long> shopIds) {
        if (CollectionUtils.isEmpty(shopIds)) {
            return new ArrayList<>();
        }
        return shopStageInfoMapper.getPlatformBuildStage(shopIds);
    }

    /**
     * 数据处理  查询所有子阶段  通过子阶段对应的阶段状态 做对应的数据处理
     * @param shopSubStage
     * @return
     */
    public List<ShopStageInfoDO> getSubStages(Integer shopSubStage) {
        return shopStageInfoMapper.getSubStageList(null, shopSubStage);
    }

    //获取新店筹备总阶段总数排除发票回传,flag=0查询全部 =1 查询已完成
    public Integer allNumber(Long shopId, Integer flag) {
        return shopStageInfoMapper.getAllNumber(shopId, flag);
    }

    /**
     * @Auther: wangshuo
     * @Date: 2025/1/15
     * @description:批量获取线索下门店的选址未开始的数据
     */
    public List<ShopStageInfoDO> getByLineIdAndSubStage(Long lineId) {
        if (lineId == null) {
            return new ArrayList<>();
        }
        Example example = new Example(ShopStageInfoDO.class);
        example.createCriteria().andEqualTo("lineId", lineId).andEqualTo("shopSubStage", ShopSubStageEnum.SHOP_STAGE_1.getShopSubStage())
                .andEqualTo("shopSubStageStatus", ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00.getShopSubStageStatus());
        List<ShopStageInfoDO> shopStageInfos = shopStageInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(shopStageInfos)){
            return new ArrayList<>();
        }
        return shopStageInfos;
    }
    /**
     * @Auther: wangshuo
     * @Date: 2025/4/28
     * @description: 获取特定阶段 用作导出
     */
    public List<ShopStageInfoDO> getShopSubStages(List<Long> shopIds) {
        if (CollectionUtils.isEmpty(shopIds)){
            return new ArrayList<>();
        }
        return shopStageInfoMapper.getShopSubStages(shopIds);
    }

    //数据处理专用
    public Integer dataUpdateStatus(List<Long> list, ShopSubStageStatusEnum shopStageInfo) {
        if (CollectionUtils.isEmpty(list) || Objects.isNull(shopStageInfo)) {
            return CommonConstants.ZERO;
        }
        String remark = shopStageInfo.getShopSubStageName() + CommonConstants.PATH_BAR + shopStageInfo.getShopSubStageStatusName();
        boolean isTerminated = shopStageInfo.isTerminated();
        return shopStageInfoMapper.dataUpdateStatus(list, shopStageInfo.getShopSubStageEnum().getShopSubStage(), shopStageInfo.getShopSubStageStatus(), isTerminated, remark);
    }

    //数据处理专用
    public Integer dataUpdateAcceptanceStatus(List<Long> list, ShopSubStageStatusEnum shopStageInfo) {
        if (CollectionUtils.isEmpty(list) || Objects.isNull(shopStageInfo)) {
            return CommonConstants.ZERO;
        }
        String remark = shopStageInfo.getShopSubStageName() + CommonConstants.PATH_BAR + shopStageInfo.getShopSubStageStatusName();
        boolean isTerminated = shopStageInfo.isTerminated();
        return shopStageInfoMapper.dataUpdateAcceptanceStatus(list, shopStageInfo.getShopSubStageEnum().getShopSubStage(), shopStageInfo.getShopSubStageStatus(), isTerminated, remark);
    }

    public List<ShopStageInfoDO> getStageByShopStage(List<Long> shopIds, Integer shopStage){
        if (CollectionUtils.isEmpty(shopIds) ||  shopStage == null){
            return null;
        }
        Example example = new Example(ShopStageInfoDO.class);
        example.createCriteria().andIn("shopId",shopIds).andEqualTo("shopStage",shopStage);
        return shopStageInfoMapper.selectByExample(example);
    }

    public List<ShopStageInfoDO> getStageByShopSubStage(List<Long> shopIds, Integer shopSubStage){
        if (CollectionUtils.isEmpty(shopIds) ||  shopSubStage == null){
            return null;
        }
        Example example = new Example(ShopStageInfoDO.class);
        example.createCriteria().andIn("shopId",shopIds).andEqualTo("shopSubStage",shopSubStage);
        return shopStageInfoMapper.selectByExample(example);
    }
    //查询处于xx状态的数据
    public List<ShopStageInfoDO> getShopStageInfoByShopSubStageStatus(Integer shopSubStageStatus) {
        Example example = new Example(ShopStageInfoDO.class);
        example.createCriteria().andEqualTo("shopSubStageStatus", shopSubStageStatus);
        return shopStageInfoMapper.selectByExample(example);
    }
}
