package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.FranchiseReportDTO;
import com.cool.store.dto.IsCreateStoreDTO;
import com.cool.store.dto.LicenseSyncDTO;
import com.cool.store.dto.Preparation.PreparationDTO;
import com.cool.store.dto.openPreparation.OpenPlanShopInfoDTO;
import com.cool.store.dto.point.LineCountDTO;
import com.cool.store.dto.point.ShopPointDTO;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.point.ShopStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.ShopInfoMapper;
import com.cool.store.request.*;
import com.cool.store.response.MiniShopsResponse;
import com.cool.store.response.PlatformBuildListResponse;
import com.cool.store.utils.UUIDUtils;
import com.cool.store.vo.shop.StageShopCountVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

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

/**
 * @author zhangchenbiao
 * @FileName: ShopInfoDAO
 * @Description:
 * @date 2024-03-29 10:14
 */
@Slf4j
@Repository
public class ShopInfoDAO {

    @Resource
    private ShopInfoMapper shopInfoMapper;


    public Integer batchAddShop(List<ShopInfoDO> shopInfoList){
        if(CollectionUtils.isEmpty(shopInfoList)){
            return CommonConstants.ZERO;
        }
        for (ShopInfoDO shopInfo : shopInfoList) {
            validateShopName(shopInfo.getShopName());
            shopInfo.setStoreId(UUIDUtils.get32UUID());
        }
        return shopInfoMapper.batchAddShop(shopInfoList);
    }

    /**
     * 获取门店信息
     * @param shopId
     * @return
     */
    public ShopInfoDO getShopInfo(Long shopId){
        ShopInfoDO shopInfo = shopInfoMapper.selectByPrimaryKey(shopId);
        if(Objects.isNull(shopInfo) || shopInfo.getDeleted()){
            return null;
        }
        return shopInfo;
    }

    public ShopInfoDO getShopInfoByStoreId(String storeId) {
        ShopInfoDO shopInfoDO = new ShopInfoDO();
        shopInfoDO.setStoreId(storeId);
        shopInfoDO.setDeleted(false);
        return shopInfoMapper.selectOne(shopInfoDO);
    }

    public List<ShopInfoDO> getShopInfoByStoreIds(List<String> storeIds) {
        if (CollectionUtils.isEmpty(storeIds)) {
            return Collections.emptyList();
        }
        Example example = new Example(ShopInfoDO.class);
        example.createCriteria()
                .andIn("storeId", storeIds)
                .andEqualTo("deleted", false);
        return shopInfoMapper.selectByExample(example);
    }

    public List<ShopInfoDO> getShopList(Long lineId){
        if(Objects.isNull(lineId)){
            return new ArrayList<>();
        }
        return shopInfoMapper.getShopList(lineId);
    }

    public List<ShopInfoDO> getShopIdBySuccessOpen(String eid){

        return shopInfoMapper.getShopIdBySuccessOpen(eid);
    }

    public List<ShopInfoDO> getShopListByRegion(Long lineId,List<Long> regionIdList,String userId){
        if(Objects.isNull(lineId)){
            return new ArrayList<>();
        }
        return shopInfoMapper.getShopListByRegion(lineId,regionIdList,userId);
    }

    /**
     * 新增门店信息
     * @param shopInfo
     * @return
     */
    public Long addShopInfo(ShopInfoDO shopInfo){
        validateShopName(shopInfo.getShopName());
        shopInfo.setStoreId(UUIDUtils.get32UUID());
        shopInfoMapper.insertSelective(shopInfo);
        return shopInfo.getId();
    }

    public Integer updateShopInfo(ShopInfoDO shopInfo){
        if(Objects.isNull(shopInfo) || Objects.isNull(shopInfo.getId())){
            log.info("店铺为空 或者店铺id为空");
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        validateShopName(shopInfo.getShopName());
        return shopInfoMapper.updateByPrimaryKeySelective(shopInfo);
    }

    public ShopInfoDO getShopInfoByPointId(Long lineId, Long pointId){
        if(Objects.isNull(lineId) || Objects.isNull(pointId)){
            return null;
        }
        return shopInfoMapper.getShopInfoByPointId(lineId, pointId);
    }

    /**
     * 获取各个阶段店铺数量
     * @param lineId
     * @return
     */
    public StageShopCountVO getStageShopCount(Long lineId) {
        return shopInfoMapper.getStageShopCount(lineId);
    }

    /**
     * 解绑铺位
     * @param shopId
     * @return
     */
    public Integer unbindPoint(Long shopId){
        if(Objects.isNull(shopId)){
            return CommonConstants.ZERO;
        }
        return shopInfoMapper.unbindPoint(shopId);
    }

    /**
     * 批量获取店铺信息
     * @param shopIds
     * @return
     */
    public List<ShopInfoDO> getShopListByIds(List<Long> shopIds){
        if(CollectionUtils.isEmpty(shopIds)){
            return new ArrayList<>();
        }
        return shopInfoMapper.getShopListByIds(shopIds);
    }

    public Map<Long, Integer> getSelectedShopNumMap(List<Long> lineIds) {
        if(CollectionUtils.isEmpty(lineIds)){
            return Maps.newHashMap();
        }
        List<LineCountDTO> selectedShopNum = shopInfoMapper.getSelectedShopNum(lineIds);
        return selectedShopNum.stream().collect(Collectors.toMap(k->k.getLineId(), v->v.getSelectedShopNum()));
    }
    /**
     * @Auther: wangshuo
     * @Date: 2024/4/25
     * @description: 根据idlist 获取storeNum
     */
   public List<OpenPlanShopInfoDTO> queryStoreNumeListByid(List<Long> shopIdList){
       return shopInfoMapper.queryStoreNumeListByid(shopIdList);
   }


    public List<PreparationDTO> ListByCondition(PreparationRequest request){
        return shopInfoMapper.ListByCondition(request);
    }

    public List<PreparationDTO> ListByBranchShopRequest(BranchShopRequest request,String userId){
        return shopInfoMapper.ListByBranchShopRequest(request,userId);
    }
    public Long getRegionIdByid(Long shopId){
       return shopInfoMapper.getRegionIdByid(shopId);
    }

    public ShopInfoDO selectByStoreNum(String storeNum){
        if (StringUtils.isBlank(storeNum)) {
            return null;
        }
        return shopInfoMapper.selectByStoreNum(storeNum);
    }

    public ShopInfoDO selectByStoreCode(String storeCode){
        if (StringUtils.isBlank(storeCode)) {
            return null;
        }
        return shopInfoMapper.selectByStoreCode(storeCode);
    }


    public List<ShopInfoDO> selectByIdOrSelectAll(Long shopId){
        return shopInfoMapper.selectByIdOrSelectAll(shopId);
    }


    public List<PlatformBuildListResponse> platformBuildList(List<Long> regionIds, platformBuildListRequest request){
       return shopInfoMapper.platformBuildList(regionIds, request);
    }

    public List<LicenseSyncDTO> getShopAndStoreList(String eid , List<Long> shopIds){
        if(CollectionUtils.isEmpty(shopIds)){
            return new ArrayList<>();
        }
        return shopInfoMapper.getShopAndStoreList(eid,shopIds);
    }

    public List<ShopPointDTO> getShopPointListByDevelopmentManager(PointLinePageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return shopInfoMapper.getShopPointListByDevelopmentManager(request);
    }


    public List<ShopInfoDO> selectAllDataOrByLineId(Long shopId) {
        return shopInfoMapper.selectAllDataOrByLineId(shopId);
    }

    public Boolean batchUpdate(List<ShopInfoDO> list) {
       if (CollectionUtils.isEmpty(list)){
           return Boolean.FALSE;
       }
        return shopInfoMapper.batchUpdate(list);
    }

    public List<ShopInfoDO> selectByLines(List<Long> lineIds,List<Long> regionIds) {
        if (CollectionUtils.isEmpty(lineIds)) {
            return new ArrayList<>();
        }
        return shopInfoMapper.selectByLines(lineIds,regionIds);
    }
    public List<MiniShopsResponse> getShopListSuccessOpen(String eid,Long lineId){
         return shopInfoMapper.getShopListSuccessOpen(eid,lineId);
   }
   public  String getOpenName(String eid,Long shopId){
       if (shopId == null){
           return null;
       }
       return shopInfoMapper.getOpenName(eid,shopId);
   }
   public Integer updateShopCode(String shopCode,Long shopId){
       return shopInfoMapper.updateShopCode(shopCode,shopId);
   }

   public List<String> getShopCodeList(Long lineId){
       if (lineId == null){
           return new ArrayList<>();
       }
       return shopInfoMapper.getShopCodeList(lineId);
   }

    public List<Long> getShopIdList(Long lineId){
        if (lineId == null){
            return new ArrayList<>();
        }
        return shopInfoMapper.getShopIdList(lineId);
    }
   public MiniShopsResponse currentShopWhetherOpen(Long shopId,String eid){
       if (shopId == null){
           return null;
       }
       return shopInfoMapper.currentShopWhetherOpen(shopId,eid);
   }

   /**
    * @Auther: wangshuo
    * @Date: 2025/4/28
    * @description: 数据处理专用获取2025年4月19号之后,并且没有生成密码的门店
    */
   public List<ShopInfoDO> getListByTime(){
       return shopInfoMapper.getListByTime();
   }

   /**
    * @Auther: wangshuo
    * @Date: 2025/5/8
    * @description: 数据处理专用 处理shop 省市区
    */
   public Integer updateShopCity(List<ShopInfoDO> list){
       if (CollectionUtils.isEmpty(list)){
           return 0;
       }
       return shopInfoMapper.updateShopCity(list);
   }

   public List<ShopInfoDO> selectByShopCodeList(List<String> shopCodeList){
       Example example = new Example(ShopInfoDO.class);
       example.createCriteria().andIn("shopCode", shopCodeList).andEqualTo("deleted", false);
       return shopInfoMapper.selectByExample(example);
   }

   public List<FranchiseReportDTO> getShopFranchiseReportList(FranchiseReportRequest request){
       return shopInfoMapper.getShopFranchiseReportList(request);
   }

    public List<ShopInfoDO> selectByPartnerIds(List<String> partnerIds){
        Example example = new Example(ShopInfoDO.class);
        example.createCriteria().andIn("partnerId", partnerIds).andEqualTo("deleted", false);
        return shopInfoMapper.selectByExample(example);
    }
    /**
     * @Auther: wangshuo
     * @Date: 2025/5/29
     * @description: 获取门店列表，根据门店状态
     */
    public List<ShopInfoDO> selectByShopStatus(Long lineId,List<Integer> shopStatus){
        Example example = new Example(ShopInfoDO.class);
        example.createCriteria().andEqualTo("lineId", lineId).andIn("shopStatus", shopStatus).andEqualTo("deleted", false);
        return shopInfoMapper.selectByExample(example);
    }
    /**
     * @Auther: wangshuo
     * @Date: 2025/7/11
     * @description:查询建店资料完成并且已经在标品已经建店是门店
     */
    public List<IsCreateStoreDTO> selectIsCreateStore(){
        return shopInfoMapper.selectIsCreateStore();
    }
    /**
     * @Auther: wangshuo
     * @Date: 2025/7/11
     * @description: 查询未建店
     */
    public List<ShopInfoDO> selectByNotCreateStore(List<Long> isCreateShopIds){
        Example example = new Example(ShopInfoDO.class);
        example.createCriteria().
                andNotIn("id", isCreateShopIds).andNotEqualTo("shopStatus", 2).andEqualTo("deleted", false)
                .andNotEqualTo("regionId",459);
        return shopInfoMapper.selectByExample(example);
    }
    public Integer updateManagerRegionId(List<ShopInfoDO> shopList){
        if (CollectionUtils.isEmpty(shopList)){
            return 0;
        }
        return shopInfoMapper.updateManagerRegionId(shopList);
    }
    public ShopInfoDO getShopInfoByShopCode(String shopCode){
        Example example = new Example(ShopInfoDO.class);
        example.createCriteria().andEqualTo("shopCode", shopCode);
        return shopInfoMapper.selectOneByExample(example);
    }
    public Integer getNumByShopCode(String shopCode){
        return shopInfoMapper.getNumByShopCode(shopCode);
    }

    public List<ShopInfoDO> getShopListByShopCode(String shopCode){
        Example example = new Example(ShopInfoDO.class);
        example.createCriteria().andEqualTo("shopCode", shopCode);
        return shopInfoMapper.selectByExample(example);
    }

    public Integer updateHqtShopId (Long shopId,String hqtShopId){
        if (shopId==null || StringUtils.isBlank(hqtShopId)){
            return  0;
        }
        return shopInfoMapper.updateHqtShopId(shopId, hqtShopId);
    }
    //查询 需要推送红圈通的门店
    public List<ShopInfoDO> getPushHqtShopList(){
        return shopInfoMapper.getPushHqtShopList();
    }

    /**
     * 校验名称 不能包含旗舰二字
     * @param shopName
     */
    private void validateShopName(String shopName) {
        if (StringUtils.isNotBlank(shopName) && shopName.contains("旗舰")) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NAME_INVALID);
        }
    }
}
