package com.tuoheng.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.common.BaseQuery;
import com.tuoheng.model.dto.City;
import com.tuoheng.model.query.CityQuery;
import com.tuoheng.service.CityService;
import com.tuoheng.mapper.CityMapper;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 小影
* @description 针对表【t_city(行政区划表)】的数据库操作Service实现
* @createDate 2023-02-03 09:57:16
*/
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City>
    implements CityService{

    @Autowired
    private CityMapper cityMapper;

    @Override
    public JsonResult getCityList() {
        List<City> list = cityMapper.selectList(new LambdaQueryWrapper<City>()
                .eq(City::getMark, 1));

        // 获取所有一级节点
        List<City> result = list.stream().
                filter(city -> city.getPid() == 0)
                .peek(city -> city.setItemList(getChildren(city, list))).sorted(Comparator.comparingInt(city -> (city.getSort() == null ? 0 : city.getSort())))
                .collect(Collectors.toList());

        return JsonResult.success(result);
    }

    /**
     * 递归获取有行政区的子集
     *
     * @author zhu_zishuang
     * @date 3/13/21
     */
    private List<City> getChildren(City rootCity, List<City> list) {
        return list.stream().filter(city ->
                city.getPid().equals(rootCity.getId())
        ).peek(city -> {
            // 设置子集
            city.setItemList(getChildren(city, list));
        }).sorted(Comparator.comparingInt(city -> (city.getSort() == null ? 0 : city.getSort()))).collect(Collectors.toList());

    }

    /**
     * 获取城市列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        CityQuery cityQuery = (CityQuery) query;
        // 查询条件
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        // 父级ID
        if (ObjectUtil.isNull(cityQuery.getPid())) {
            queryWrapper.eq("pid", 0);
        } else {
            queryWrapper.eq("pid", cityQuery.getPid());
        }
        // 城市名称
        if (!ObjectUtil.isEmpty(cityQuery.getName())) {
            queryWrapper.like("name", cityQuery.getName());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");

        // 查询分页数据
        List<City> cityList = cityMapper.selectList(queryWrapper);
        cityList.forEach(item -> {
            //省、市、区、街道、村
            if (item.getLevel() <= 4) {
                item.setHasChildren(true);
            }
        });
        return JsonResult.success(cityList);
    }

}




