package com.cool.store.dao;

import cn.hutool.core.convert.Convert;
import com.cool.store.entity.HyOpenAreaInfoDO;
import com.cool.store.mapper.HyOpenAreaInfoMapper;
import com.cool.store.utils.StringUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
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.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2023/6/6 14:32
 * @Version 1.0
 */
@Repository
public class HyOpenAreaInfoDAO {

    @Resource
    HyOpenAreaInfoMapper hyOpenAreaInfoMapper;

    public List<HyOpenAreaInfoDO> queryKeyOpenArea(String keyword,Integer keyOpenStatus){
        return hyOpenAreaInfoMapper.queryKeyOpenArea( keyword,  keyOpenStatus);
    }

    public List<HyOpenAreaInfoDO> queryByKeyword(String keyword,Boolean applyFlag,String areaStatus,Boolean filterData){
        return hyOpenAreaInfoMapper.queryByKeyword(keyword,applyFlag,areaStatus,filterData);
    }

    public List<HyOpenAreaInfoDO> queryFirstLevel(){
        return hyOpenAreaInfoMapper.queryFirstLevel();
    }

    public List<HyOpenAreaInfoDO> queryByIdsExcludeFirstLevel(List<Long> ids){
        if (CollectionUtils.isEmpty(ids)){
            return new ArrayList<>();
        }
        return hyOpenAreaInfoMapper.queryByIdsExcludeFirstLevel(ids);
    }


    public List<HyOpenAreaInfoDO> getChildrenList(Long parentId){
        if (parentId==null){
            return new ArrayList<>();
        }
        return hyOpenAreaInfoMapper.getChildrenList(parentId);
    }

    public Integer getChildrenCount(String type ,Long parentId){
        if (parentId==null){
            return 0;
        }
        return hyOpenAreaInfoMapper.getChildrenCount(type,parentId);
    }


    public int batchUpdateById(String backgroundBanner,String detailBanner,String areaStatus,String updateUserId,List<Long> ids){
        if (StringUtils.isEmpty(areaStatus)){
            return 0;
        }
        return hyOpenAreaInfoMapper.batchUpdateById(backgroundBanner,detailBanner,areaStatus,updateUserId,ids);
    }

    public int batchUpdateByParentId(String backgroundBanner,String detailBanner,String areaStatus,String updateUserId,List<Long> ids){
        if (StringUtils.isEmpty(areaStatus)){
            return 0;
        }
        return hyOpenAreaInfoMapper.batchUpdateByParentId(backgroundBanner,detailBanner,areaStatus,updateUserId,ids);
    }


    public List<HyOpenAreaInfoDO> selectByIds(List<Long> ids){
        if (CollectionUtils.isEmpty(ids)){
            return Lists.newArrayList();
        }
        return hyOpenAreaInfoMapper.selectByIds(ids);
    }

    public Map<Long, String> getNameMapByIds(List<Long> ids){
        if (CollectionUtils.isEmpty(ids)){
            return Maps.newHashMap();
        }
        List<HyOpenAreaInfoDO> hyOpenAreaInfoDOS = hyOpenAreaInfoMapper.selectByIds(ids);
        return ListUtils.emptyIfNull(hyOpenAreaInfoDOS).stream().collect(Collectors.toMap(k->k.getId(), v->v.getAreaName(), (k1, k2)->k1));
    }

    public Map<Long, String> selectNameMapByIds(List<Long> ids){
        if (CollectionUtils.isEmpty(ids)){
            return Maps.newHashMap();
        }
        List<HyOpenAreaInfoDO> hyOpenAreaInfoDOS = hyOpenAreaInfoMapper.selectByIds(ids);
        return ListUtils.emptyIfNull(hyOpenAreaInfoDOS).stream().collect(Collectors.toMap(k->k.getId(), v->v.getAreaPath().replace("/"," ").trim()));
    }

    public String selectNameMapById(Long id){
        HyOpenAreaInfoDO hyOpenAreaInfoDOS = hyOpenAreaInfoMapper.selectById(id);
        return hyOpenAreaInfoDOS.getAreaPath().replace("/", " ").trim();
    }


    public HyOpenAreaInfoDO selectById(Long id){
        if (id==null){
            return null;
        }
        return hyOpenAreaInfoMapper.selectById(id);
    }

    public List<HyOpenAreaInfoDO> getAllOpenArea(){
        return hyOpenAreaInfoMapper.getAllOpenArea();
    }


    public  List<HyOpenAreaInfoDO> selectAllCity(){
        return hyOpenAreaInfoMapper.selectAllCity();
    }

    /**
     * 过滤叶子节点
     * @param openAreaIds
     * @return 叶子节点id
     */
    public List<Long> filterLeafNode(List<Long> openAreaIds){
        if(CollectionUtils.isEmpty(openAreaIds)){
            return Lists.newArrayList();
        }
        return hyOpenAreaInfoMapper.filterLeafNode(openAreaIds);
    }

    public List<HyOpenAreaInfoDO> getAllAreaCode(String id) {
        if (StringUtils.isEmpty(id)){
            return new ArrayList<>();
        }
        //获取省下所有数据
        List<HyOpenAreaInfoDO>  provinceCodeList= hyOpenAreaInfoMapper.getProvinceAllCode(id);
        if(CollectionUtils.isEmpty(provinceCodeList)){
            //获取市区下所有数据
            List<HyOpenAreaInfoDO> cityCodeList= hyOpenAreaInfoMapper.getSonArea(id);
            if(CollectionUtils.isEmpty(cityCodeList)){
                return new ArrayList<HyOpenAreaInfoDO>(){{ add(hyOpenAreaInfoMapper.selectById(Convert.toLong(id)));}};
            }else {
                return cityCodeList;
            }
        }else {
            return provinceCodeList;
        }
    }

    public HyOpenAreaInfoDO selectByAreaPath(String areaPath) {
        if (StringUtil.isEmpty(areaPath)) {
            return null;
        }
        return hyOpenAreaInfoMapper.selectByAreaPath(areaPath);
    }

    public int updateByPrimaryKeySelective(HyOpenAreaInfoDO hyOpenAreaInfoDO){
        return hyOpenAreaInfoMapper.updateByPrimaryKeySelective( hyOpenAreaInfoDO);
    }

    public Map<Long, HyOpenAreaInfoDO> getCityMap(List<Long> wantShopAreaIds) {
        List<HyOpenAreaInfoDO> openAreaInfoList = selectByIds(wantShopAreaIds);
        return openAreaInfoList.stream().collect(Collectors.toMap(k->k.getId(), v->v, (k1, k2)->k1));
    }
}
