package com.pj.project.tb_district;

import cn.hutool.core.util.StrUtil;
import com.pj.api.dto.DistrictDTO;
import com.pj.api.dto.DistrictInfoDTO;

import com.pj.utils.sg.AjaxJson;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


/**
 * Controller: tb_district -- 全国地区表
 *
 * @author qzyReal
 */
@RestController
@RequestMapping("/TbDistrict/")
public class TbDistrictController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbDistrictService tbDistrictService;


    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    public AjaxJson getById(Integer id) {
        TbDistrict t = tbDistrictService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 获取树表格
     * @return
     */
    @RequestMapping(value = "getTreeData")
    public AjaxJson getTreeData() {
        List<TbDistrict> list = tbDistrictService.getTreeData();
        return AjaxJson.getSuccessData(list);
    }

    /**
     * 远程调用
     * @param ids
     * @return List<DistrictDTO>
     */
    @RequestMapping(value = "rpc/getByIds")
    public List<DistrictDTO> getByIds(String ids) {
        List<TbDistrict> list = tbDistrictService.listByIds(StrUtil.splitTrim(ids,","));
        List<DistrictDTO>districtDTOS=new ArrayList<>();
        list.parallelStream().forEach(tbDistrict -> {
            DistrictDTO districtDTO=new DistrictDTO();
            BeanUtils.copyProperties(tbDistrict,districtDTO);
            districtDTOS.add(districtDTO);
        });
        districtDTOS.sort(Comparator.comparing(DistrictDTO::getLevel));
        return districtDTOS;
    }

    /**
     * 根据城市名称获取地区数据
     * @param cityName
     * @return
     */
  @RequestMapping(value = "rpc/findInfoByCityName")
    public DistrictInfoDTO findByCityName(String cityName) {
      return  tbDistrictService.findInfoByCityName(cityName);
    }





}
