package com.ruoyi.api.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.base.area.domain.SysCnArea;
import com.ruoyi.base.area.service.ISysCnAreaService;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 行政区域Controller
 *
 * @author ruoyi
 * @date 2023-06-25
 */
@Validated
@Api(value = "行政区域控制器", tags = {"行政区域管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/base/cnArea")
public class ApiSysCnAreaController extends AbstractApiController {

    private final ISysCnAreaService iSysCnAreaService;

    /**
     * 查询所有城市且按字母排序
     */
    @ApiOperation("查询所有城市且按字母排序")
    @GetMapping("/all-city")
    public R<Map<String, List<SysCnArea>>> allCity() {
        return R.ok(iSysCnAreaService.allCity());
    }

    /**
     * 获得省市区三级数据
     */
    @ApiOperation("获得省市区三级数据")
    @GetMapping("/tree-lbs")
    public R getTreeLbs() {
        JSONObject treeLbs = iSysCnAreaService.getTreeLbs();
        return R.ok(treeLbs);
    }


//    /**
//     * 刷新拼音
//     */
//    @ApiOperation("刷新拼音")
//    @GetMapping("/refreshPinYi")
//    public R refreshPinYi() {
//        iSysCnAreaService.refreshPinYi();
//        return R.ok();
//    }

    /**
     * 根据父级code查下一级
     *
     * @param parentCode
     * @return
     */
    @ApiOperation("根据父级code查下一级")
    @GetMapping("/lbs-children/{parentCode}")
    public R<List<SysCnArea>> getChildren(@PathVariable("parentCode") String parentCode) {
        List<SysCnArea> list = iSysCnAreaService.getChildren(parentCode);
        return R.ok(list);
    }


    /**
     * 根据行政编号查父级
     *
     * @param code
     * @return
     */
    @ApiOperation("根据行政编号查父级")
    @GetMapping("/lbs-path/{code}")
    public R<List<SysCnArea>> getPath(@PathVariable("code") String code) {
        List<SysCnArea> list = iSysCnAreaService.getPath(code);
        return R.ok(list);
    }


    /**
     * 根据行政编号查城市
     *
     * @param code
     * @return
     */
    @ApiOperation("根据行政编号查城市")
    @GetMapping("/lbs-city/{code}")
    public R<SysCnArea> getCity(@PathVariable("code") String code) {
        SysCnArea byCode = iSysCnAreaService.getByCode(code);
        if(byCode==null)
        {
            byCode = iSysCnAreaService.getByCode("420100");
            return R.ok(byCode);
        }
        while (true)
        {
            if (byCode != null) {
                if (byCode.getLevel() == 2) {
                    break;
                } else {
                    code = byCode.getParent();
                    byCode = iSysCnAreaService.getByCode(code);
                }
            } else {
                break;
            }
        }
        if(byCode==null)
        {
            byCode = iSysCnAreaService.getByCode("420100");
        }
        return R.ok(byCode);
    }

}
