package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
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.entity.ShopInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.ShopInfoMapper;
import com.cool.store.request.PlanListRequest;
import com.cool.store.request.PreparationRequest;
import com.cool.store.request.platformBuildListRequest;
import com.cool.store.response.PlatformBuildListResponse;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
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;
        }
        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 List<ShopInfoDO> getShopList(Long lineId){
        if(Objects.isNull(lineId)){
            return new ArrayList<>();
        }
        return shopInfoMapper.getShopList(lineId);
    }

    /**
     * 新增门店信息
     * @param shopInfo
     * @return
     */
    public Long addShopInfo(ShopInfoDO shopInfo){
        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);
        }
        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 Long getRegionIdByid(Long shopId){
       return shopInfoMapper.getRegionIdByid(shopId);
    }

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


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


    public List<PlatformBuildListResponse> platformBuildList(List<Long> regionIds, platformBuildListRequest request){
       if(CollectionUtils.isEmpty(regionIds)){
           return new ArrayList<>();
       }
       return shopInfoMapper.platformBuildList(regionIds, request);
    }
}
