package com.hotent.baseInfo.controller;


import cn.hutool.core.util.ObjectUtil;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.exception.BaseException;
import com.hotent.baseInfo.model.CostCatalog;
import com.hotent.baseInfo.vo.CatalogTreeVo;
import com.hotent.baseInfo.vo.CostDistrictTreeVo;
import com.hotent.util.JsonUtil;
import com.hotent.util.treeutil.SumTree;
import com.hotent.util.treeutil.newtreeutil.TreeDotUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.hotent.base.model.CommonResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;
import com.hotent.baseInfo.model.CostDistrict;
import com.hotent.baseInfo.manager.CostDistrictManager;

import java.util.List;

/**
 * 地区表 前端控制器
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-17
 */
@RestController
@RequestMapping("/api/costDistrict/v1/")
@Api(tags = "地区管理")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostDistrictController{

	@Autowired
	private CostDistrictManager costDistrictManager;


	/**
	 * 获取省级数据
	 */
	@GetMapping(value="/getProvinces")
	@ApiOperation(value="获取省级数据",httpMethod = "GET",notes = "根据id获取地区表数据详情")
	public CommonResult<List<CostDistrict>> getProvinces() throws Exception{
		List<CostDistrict> provinces = costDistrictManager.getProvinces();
		return CommonResult.<List<CostDistrict>>ok().value(provinces);
	}


	//查询省市区树结构
	@GetMapping(value="/getDistrictTree")
	@ApiOperation(value="查询省市区树结构",httpMethod = "GET",notes = "查询省市区树结构")
	public CommonResult<List<SumTree<CostDistrictTreeVo>>> getDistrictTree() throws Exception{
		List<CostDistrict> costDistricts = costDistrictManager.getDistrictTree();
		List<CostDistrictTreeVo> allList = JsonUtil.getJsonToList(costDistricts, CostDistrictTreeVo.class);
		for (int i = 0; i < costDistricts.size(); i++) {
			CostDistrict costCatalog = costDistricts.get(i);
			allList.get(i).setId(costCatalog.getId().toString());
			allList.get(i).setParentId(costCatalog.getPid().toString());
		}
		List<SumTree<CostDistrictTreeVo>> trees = TreeDotUtils.convertListToTreeDotFilter(allList);
		CommonResult<List<SumTree<CostDistrictTreeVo>>> result = CommonResult.ok();
		result.setValue(trees);
		return result;
	}

	/**
	 * 根据pid获取市、区数据
	 */
	@GetMapping(value="/getListByPid")
	@ApiOperation(value="根据pid获取市、区数据",httpMethod = "GET",notes = "根据id获取地区表数据详情")
	public CommonResult<List<CostDistrict>> getListByPid(@ApiParam(name="pid",value="父级id") @RequestParam(value="pid") Integer pid) throws Exception{
		if(ObjectUtil.isEmpty(pid)){
			throw new BaseException("pid不能为空");
		}
		List<CostDistrict> provinces = costDistrictManager.getListByPid(pid);
		return CommonResult.<List<CostDistrict>>ok().value(provinces);
	}

	@GetMapping(value="/getByCode")
	@ApiOperation(value="根据code获取数据",httpMethod = "GET",notes = "根据code获取地区表数据详情")
	public CommonResult<CostDistrict> getByCode(@ApiParam(name="code",value="地区编码") @RequestParam(value="code") String code) throws Exception{
		if(ObjectUtil.isEmpty(code)){
			throw new BaseException("code不能为空");
		}
		CostDistrict costDistrict = costDistrictManager.getByCode(code);
		return CommonResult.<CostDistrict>ok().value(costDistrict);
	}




}
