package com.vctgo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vctgo.system.domain.SysCity;
import com.vctgo.system.domain.SysDistrict;
import com.vctgo.system.domain.SysProvince;
import com.vctgo.system.mapper.SysProvinceMapper;
import com.vctgo.system.service.ISysProvinceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 省份信息Service业务层处理
 *
 * @author wanglonghua
 * @date 2023-06-08
 */
@Slf4j
@Service
public class SysProvinceServiceImpl extends ServiceImpl<SysProvinceMapper, SysProvince> implements ISysProvinceService {
    /**
     * 查询省份信息
     *
     * @param id 省份信息主键
     * @return 省份信息
     */
    @Override
    public SysProvince selectSysProvinceById(Long id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询省份信息列表
     *
     * @param sysProvince 省份信息
     * @return 省份信息
     */
    @Override
    public List<SysProvince> selectSysProvinceList(SysProvince sysProvince) {
        List<SysProvince> provinceList = new ArrayList<>();
        LambdaQueryWrapper<SysProvince> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotEmpty(sysProvince.getProvinceId()), SysProvince::getProvinceId, sysProvince.getProvinceId())
                .like(StringUtils.isNotEmpty(sysProvince.getProvince()), SysProvince::getProvince, sysProvince.getProvince());
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<SysProvince> selectProvinceListRelationChild(String provinceId,
                                                             String cityId) {
        List<SysProvince> provinceList = new ArrayList<>();
        List<SysCity> cityList = new ArrayList<>();
        List<SysDistrict> districtList = new ArrayList<>();
        provinceList = this.baseMapper.selectProvinceList(provinceId);
        cityList = this.baseMapper.selectCityList(provinceId, cityId);
        Map<String, List<SysCity>> groupByProvinceMap = cityList.stream().collect(Collectors.groupingBy(SysCity::getProvinceId));
        districtList = this.baseMapper.selectDistrictList(cityId);
        Map<String, List<SysDistrict>> groupByCityMap = districtList.stream().filter(obj -> !"市辖区".equals(obj.getDistrict())).collect(Collectors.groupingBy(SysDistrict::getCityId));
        cityList.stream().forEach(city -> {
            if (groupByCityMap.containsKey(city.getCityId())) {
                city.setChildrenList(groupByCityMap.get(city.getCityId()));
            }
        });
        provinceList.stream().forEach(province -> {
            if (groupByProvinceMap.containsKey(province.getProvinceId())) {
                province.setChildrenList(groupByProvinceMap.get(province.getProvinceId()));
            }
        });
        return provinceList;
    }

    @Override
    public List<SysProvince> getProvince() {
        return this.baseMapper.selectList();
    }

}
