package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.PlatformBuildStageDTO;
import com.cool.store.dto.Preparation.ScheduleDTO;
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.mapper.ShopStageInfoMapper;
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 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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

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

    @Resource
    private ShopStageInfoMapper shopStageInfoMapper;

    /**
     * 初始化店铺阶段信息
     * @param lineId
     * @param shopIds
     * @return
     */
    public Integer initShopStageInfo(Long lineId, List<Long> shopIds) {
        if(CollectionUtils.isEmpty(shopIds)){
            return CommonConstants.ZERO;
        }
        List<ShopStageInfoDO> addShopStageList = new ArrayList<>();
        LocalDate selectStartDate = LocalDate.now();
        for (Long shopId : shopIds) {
            for (ShopSubStageEnum shopSubStageEnum : ShopSubStageEnum.values()) {
                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();
                shopStageInfo.setShopSubStageStatus(initStatus.getShopSubStageStatus());
                shopStageInfo.setRemark(shopSubStageEnum.getShopSubStageName() + CommonConstants.PATH_BAR +shopSubStageEnum.getInitStatus().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);
    }
    /**
     * @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);
    }


    /**
     * 批量更新店铺状态
     * @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();
        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);
        }

        return shopStageInfoMapper.batchUpdateShopStageStatus(shopId, shopStageList);
    }

    /**
     * 更新子阶段到未开始状态
     * @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 Collections.emptyList();
        }
        return shopStageInfoMapper.getScheduleList(shopIdList);
    }
    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);
    }

    /**
     * 获取可以上传租赁合同的店铺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){
        return shopStageInfoMapper.getSpecialShopStageInfo( shopIds, shopSubStage, shopSubStageStatusList,investmentUserId,authRegionIds);
    }
    /**
     * @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);
    }
}
