package io.modules.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.util.StrUtil;
import io.common.service.impl.CrudServiceImpl;
import io.modules.item.dao.AreaDao;
import io.modules.item.dao.CityDao;
import io.modules.item.dao.ProvinceDao;
import io.modules.item.dto.AreaTree;
import io.modules.item.dto.CityTree;
import io.modules.item.dto.ProvinceDTO;
import io.modules.item.dto.ProvinceTree;
import io.modules.item.entity.AreaEntity;
import io.modules.item.entity.CityEntity;
import io.modules.item.entity.ProvinceEntity;
import io.modules.item.service.CityService;
import io.modules.item.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.Area;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 省份信息
 */
@Service
public class ProvinceServiceImpl extends CrudServiceImpl<ProvinceDao, ProvinceEntity, ProvinceDTO> implements ProvinceService {


    @Autowired
    private CityDao cityRepository;

    @Autowired
    private AreaDao areaRepository;

    @Override
    public QueryWrapper<ProvinceEntity> getWrapper(Map<String, Object> params){
        String provinceId = (String)params.get("province_id");

        QueryWrapper<ProvinceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(provinceId), "province_id", provinceId);

        return wrapper;
    }


    @Override
    public List<ProvinceTree> tree() {
        List<ProvinceEntity> provinces = baseDao.selectList(null);
        List<ProvinceTree> provinceTrees = new ArrayList<>();
        for (ProvinceEntity province : provinces) {
            ProvinceTree provinceTree = new ProvinceTree();
            provinceTree.setValue(province.getId());
            provinceTree.setLabel(province.getProvince());
            provinceTree.setChildren(getCityTree(province.getProvinceId())); // 获取该省的城市树
            provinceTrees.add(provinceTree);
        }
        return provinceTrees;
    }
    private List<CityTree> getCityTree(String provinceId) {

        LambdaQueryWrapper<CityEntity> lwq = new LambdaQueryWrapper<>();
        lwq.eq(CityEntity::getFather, provinceId);
        List<CityEntity> cities = cityRepository.selectList(lwq);
        List<CityTree> cityTrees = new ArrayList<>();
        for (CityEntity city : cities) {
            CityTree cityTree = new CityTree();
            cityTree.setValue(city.getId());
            cityTree.setLabel(city.getCity());
            cityTree.setChildren(getAreaTree(city.getCityId())); // 获取该城市的地区树
            cityTrees.add(cityTree);
        }
        return cityTrees;
    }


    private List<AreaTree> getAreaTree(String cityId) {
        LambdaQueryWrapper<AreaEntity> lwq = new LambdaQueryWrapper<>();
        lwq.eq(AreaEntity::getFather, cityId);
        List<AreaEntity> areas = areaRepository.selectList(lwq);
        List<AreaTree> areaTrees = new ArrayList<>();
        for (AreaEntity area : areas) {
            AreaTree areaTree = new AreaTree();
            areaTree.setValue(area.getId());
            areaTree.setLabel(area.getArea());
            areaTrees.add(areaTree);
        }
        return areaTrees;
    }

}