package com.cool.store.dao;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.point.LineCountDTO;
import com.cool.store.entity.PointInfoDO;
import com.cool.store.mapper.PointInfoMapper;
import com.cool.store.request.AllPointPageRequest;
import com.cool.store.request.PointPageRequest;
import com.cool.store.request.RecommendPointPageRequest;
import com.cool.store.utils.StringUtil;
import com.cool.store.vo.point.PointHomePageDataVO;
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: ShopPointInfoDAO
 * @Description:
 * @date 2024-03-29 10:15
 */
@Repository
public class PointInfoDAO {

    @Resource
    private PointInfoMapper pointInfoMapper;

    public PointInfoDO getPointInfoById(Long id) {
        PointInfoDO shopPointInfo = pointInfoMapper.selectByPrimaryKey(id);
        if(Objects.isNull(shopPointInfo) || shopPointInfo.getDeleted()){
            return null;
        }
        return shopPointInfo;
    }

    public Long addPointInfo(PointInfoDO shopPointInfo) {
        pointInfoMapper.insertSelective(shopPointInfo);
        return shopPointInfo.getId();
    }

    public Integer updatePointInfo(PointInfoDO shopPointInfo) {
        return pointInfoMapper.updateByPrimaryKeySelective(shopPointInfo);
    }

    /**
     * 完善铺位信息
     * @param shopPointInfo
     * @return
     */
    public Integer perfectPointInfo(PointInfoDO shopPointInfo) {
        return pointInfoMapper.perfectPointInfo(shopPointInfo);
    }

    public Integer updatePointInfoByPrimaryKey(PointInfoDO shopPointInfo) {
        return pointInfoMapper.updateByPrimaryKey(shopPointInfo);
    }

    public PointHomePageDataVO getMyPointData(String userId) {
        return pointInfoMapper.getMyPointData(userId);
    }

    /**
     * 获取我的铺位
     * @param request
     * @return
     */
    public Page<PointInfoDO> getMyPointPage(PointPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return pointInfoMapper.getMyPointPage(request);
    }

    public Map<Long, Integer> getSelectedShopNumMap(List<Long> lineIds) {
        if(CollectionUtils.isEmpty(lineIds)){
            return Maps.newHashMap();
        }
        List<LineCountDTO> selectedShopNum = pointInfoMapper.getSelectedShopNum(lineIds);
        return selectedShopNum.stream().collect(Collectors.toMap(k->k.getLineId(), v->v.getSelectedShopNum()));
    }

    public List<PointInfoDO> getPointListByIds(List<Long> pointIds){
        if(CollectionUtils.isEmpty(pointIds)){
            return Lists.newArrayList();
        }
        return pointInfoMapper.getPointListByIds(pointIds);
    }

    public Page<PointInfoDO> getRecommendPointList(RecommendPointPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return pointInfoMapper.getRecommendPointList(request);
    }

    public Integer updateSelectedDevelopmentManager(Long lineId, String developmentManager) {
        if(Objects.isNull(lineId) || StringUtils.isBlank(developmentManager)){
            return null;
        }
        return pointInfoMapper.updateSelectedDevelopmentManager(lineId, developmentManager);
    }

    public Page<PointInfoDO> getTeamPointPage(AllPointPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return pointInfoMapper.getTeamPointPage(request);
    }

    public Integer recyclePoint(Long pointId) {
        if(Objects.isNull(pointId)){
            return CommonConstants.ZERO;
        }
        return pointInfoMapper.recyclePoint(pointId);
    }
}
