package com.pj.project.tb_district;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.api.dto.DistrictInfoDTO;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service: tb_district -- 全国地区表
 *
 * @author qzyReal
 */
@Service
public class TbDistrictService extends ServiceImpl<TbDistrictMapper, TbDistrict> implements IService<TbDistrict> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbDistrictMapper tbDistrictMapper;

    /**
     * 增
     */
    int add(TbDistrict t) {
        return tbDistrictMapper.add(t);
    }

    /**
     * 删
     */
    int delete(Integer id) {
        return tbDistrictMapper.delete(id);
    }

    /**
     * 改
     */
    int update(TbDistrict t) {
        return tbDistrictMapper.update(t);
    }

    /**
     * 查
     */
    TbDistrict getById(Integer id) {
        return tbDistrictMapper.getById(id);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbDistrict> getList(SoMap so) {
        return tbDistrictMapper.getList(so);
    }

    /**
     * 获取省份
     *
     * @return
     */
    public List<TbDistrict> GetListProvince() {
        QueryWrapper<TbDistrict> ew = new QueryWrapper<>();
        ew.eq("pid", 1);
        return this.list(ew);
    }

    /**
     * 根据父ID查询
     *
     * @param areaId
     * @return
     */
    public List<TbDistrict> GetListCity(Long areaId) {
        QueryWrapper<TbDistrict> ew = new QueryWrapper<>();
        ew.eq("pid", areaId);
        return this.list(ew);
    }

    /**
     * 根据城市名称查询
     *
     * @param cityName 城市名
     * @return
     */
    public TbDistrict findByCityName(String cityName) {
        QueryWrapper<TbDistrict> ew = new QueryWrapper<>();
        ew.lambda().eq(TbDistrict::getDistrict, cityName);
        List<TbDistrict> list = list(ew);
        return list.isEmpty() ? null : list.get(0);
    }


    TimedCache<String, List<TbDistrict>> timedCache = CacheUtil.newTimedCache(999999999);

    public List<TbDistrict> getTreeData() {
        List<TbDistrict> cacheList = timedCache.get("address");
        if (cacheList != null && cacheList.size() > 0) {
            return cacheList;
        }
        List<TbDistrict> list = this.GetListProvince();
        list.forEach(tbDistrict -> {
            List<TbDistrict> cityList = this.GetListCity(tbDistrict.getId());
            tbDistrict.setChildren(cityList);
            cityList.forEach(city -> {
                List<TbDistrict> areaList = this.GetListCity(city.getId());
                city.setChildren(areaList);
            });
        });
        timedCache.put("address", list);
        return list;
    }

    public DistrictInfoDTO findInfoByCityName(String cityName) {
        TbDistrict tbDistrict = findByCityName(cityName);
        if (tbDistrict == null) {
            return null;
        }
        Long pid = tbDistrict.getPid();
        TbDistrict parent = getById(pid);
        List<Long> ids = new ArrayList<>();
        ids.add(tbDistrict.getId());
        StringBuilder name = new StringBuilder(cityName);
        if (parent != null) {
            ids.add(parent.getId());
            name.insert(0, parent.getDistrict());
            TbDistrict top = getById(parent.getPid());
            if (top != null) {
                ids.add(top.getId());
                name.insert(0, top.getDistrict());
            }
        }
        Collections.reverse(ids);
        DistrictInfoDTO districtInfoDTO = new DistrictInfoDTO();
        districtInfoDTO.setPath(ids.stream().map(id -> id.toString()).collect(Collectors.joining(",")));
        districtInfoDTO.setName(name.toString());
        return districtInfoDTO;
    }
}
