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.MiniShopDTO;
import com.cool.store.dto.PCPointListDTO;
import com.cool.store.dto.PointInfoDTO;
import com.cool.store.dto.point.LineCountDTO;
import com.cool.store.entity.PointInfoDO;
import com.cool.store.mapper.PointInfoMapper;
import com.cool.store.request.*;
import com.cool.store.utils.StringUtil;
import com.cool.store.vo.point.MiniPointPageVO;
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 tk.mybatis.mapper.entity.Example;

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: 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<PCPointListDTO> getMyPointPage(String eid,PointPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return pointInfoMapper.getMyPointPage(eid,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 List<PointInfoDTO> getPointAndDetailByIds(List<Long> pointIds){
        if(CollectionUtils.isEmpty(pointIds)){
            return Lists.newArrayList();
        }
        return pointInfoMapper.getPointAndDetailByIds(pointIds);
    }

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

    public List<MiniPointPageVO> getRecommendOrMyList(MiniPointRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return pointInfoMapper.getRecommendOrMyList(request);
    }

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

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

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

    public List<MiniPointPageVO> getMiniPointPage(String eid,MiniPointRequest request ) {
        List<MiniPointPageVO> pointInfoDOPage = pointInfoMapper.MiniPointPageVO(eid,request);
        if(CollectionUtils.isEmpty(pointInfoDOPage)){
            return Lists.newArrayList();
        }

        return pointInfoDOPage ;
    }

   public List<MiniPointPageVO> getPointByOpportunityPointCode(String code){
        if (StringUtils.isBlank(code)){
            return  new ArrayList<>();
        }
       return pointInfoMapper.getPointByOpportunityPointCode(code);
    }

    public List<MiniShopDTO> getShopByOpportunityPointCode(String eid,String code){
        if (StringUtils.isBlank(code)){
            return  new ArrayList<>();
        }
        return pointInfoMapper.getShopByOpportunityPointCode(eid,code);
    }

    /**
     * @Auther: wangshuo
     * @Date: 2025/5/9
     * @description:数据处理用 省市区详细地址面积
     */
    public Integer updateAddress(List<PointInfoDO> list){
        if (CollectionUtils.isEmpty(list)){
            return CommonConstants.ZERO;
        }
        return pointInfoMapper.updateAddress(list);
    }
}
