package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.point.LineCountDTO;
import com.cool.store.dto.point.MiniPointPageDTO;
import com.cool.store.entity.PointRecommendDO;
import com.cool.store.enums.point.PointRecommendStatusEnum;
import com.cool.store.mapper.PointRecommendMapper;
import com.cool.store.request.MiniPointPageRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangchenbiao
 * @FileName: ShopPointRecommendDAO
 * @Description:点位推荐
 * @date 2024-04-01 14:36
 */
@Repository
public class PointRecommendDAO {

    @Resource
    private PointRecommendMapper pointRecommendMapper;

    /**
     * 更新推荐状态
     * @param pointId
     * @param status
     * @param excludeStatusList 排除状态
     * @return
     */
    public Integer updateRecommendStatus(Long pointId, PointRecommendStatusEnum status, List<PointRecommendStatusEnum> excludeStatusList) {
        if(Objects.isNull(status)){
            return 0;
        }
        List<Integer> excludeStatus = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(excludeStatusList)){
            excludeStatus = excludeStatusList.stream().map(PointRecommendStatusEnum::getCode).collect(Collectors.toList());
        }
        return pointRecommendMapper.updateShopPointRecommendStatus(pointId, status.getCode(), excludeStatus);
    }

    /**
     * 更新推荐状态
     * @param pointId
     * @param status
     * @param statusList
     * @return
     */
    public Integer updateRecommendStatusByStatusAndPointId(Long pointId, PointRecommendStatusEnum status, List<PointRecommendStatusEnum> statusList) {
        if(Objects.isNull(status) || CollectionUtils.isEmpty(statusList)){
            return 0;
        }
        List<Integer> statusCodeList = statusList.stream().map(PointRecommendStatusEnum::getCode).collect(Collectors.toList());
        return pointRecommendMapper.updateRecommendStatusByStatusAndPointId(pointId, status.getCode(), statusCodeList);
    }

    /**
     * 获取已推门店数量
     * @param lineIds
     * @return
     */
    public Map<Long, Integer> getPushShopNumMap(List<Long> lineIds) {
        if(CollectionUtils.isEmpty(lineIds)){
            return Maps.newHashMap();
        }
        List<LineCountDTO> pushShopNumMap = pointRecommendMapper.getPushShopNumMap(lineIds);
        return pushShopNumMap.stream().collect(Collectors.toMap(k->k.getLineId(), v->v.getRecommendShopNum()));
    }

    /**
     * 获取推荐列表
     * @param lineId
     * @return
     */
    public List<PointRecommendDO> getRecommendPointList(Long lineId) {
        if(Objects.isNull(lineId)){
            return Lists.newArrayList();
        }
        return pointRecommendMapper.getRecommendPointList(lineId);
    }

    public Integer addRecommendPoint(List<PointRecommendDO> recommendList) {
        if(CollectionUtils.isEmpty(recommendList)){
            return CommonConstants.ZERO;
        }
        return pointRecommendMapper.batchInsert(recommendList);
    }

    /**
     * 转让线索更新推荐状态
     * @param lineId
     * @return
     */
    public Integer turnLineUpdateRecommendStatus(Long lineId, String developmentManager) {
        return pointRecommendMapper.turnLineUpdateRecommendStatus(lineId, developmentManager);
    }

    /**
     * 线索获取推荐列表
     * @param request
     * @return
     */
    public Page<MiniPointPageDTO> getLineRecommendPointPage(MiniPointPageRequest request){
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return pointRecommendMapper.getLineRecommendPointPage(request);
    }

    public Integer updateStatusByPointIdAndLineId(Long pointId, Long lineId){
        if(Objects.isNull(pointId) || Objects.isNull(lineId)){
            return 0;
        }
        return pointRecommendMapper.updateStatusByPointIdAndLineId(pointId, lineId);
    }

    /**
     * 铺位拒绝
     * @param lineId
     * @param pointId
     * @param reason
     * @return
     */
    public Integer rejectPoint(Long lineId, Long pointId, String reason) {
        if(Objects.isNull(lineId) || Objects.isNull(pointId)){
            return CommonConstants.ZERO;
        }
        return pointRecommendMapper.rejectPoint(lineId, pointId, reason);
    }

    /**
     * 获取铺位推送了哪些列表
     * @param pointId
     * @return
     */
    public List<PointRecommendDO> getRecommendPointListByPointId(Long pointId) {
        if(Objects.isNull(pointId)){
            return Lists.newArrayList();
        }
        return pointRecommendMapper.getRecommendPointListByPointId(pointId);
    }
}
