package com.ruoyi.ibs.grid.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.ibs.grid.domain.entity.AdminDivision;
import com.ruoyi.ibs.grid.domain.vo.AddressTreeVO;
import com.ruoyi.ibs.grid.service.RegionGridUtilsService;
import com.ruoyi.ibs.grid.service.SysAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 吴凯程
 * @Date 2024/9/14
 **/
@Api(tags = "地址结构树接口")
@RestController
@RequestMapping("/address")
public class SysAddressController extends BaseController {

    @Resource
    private SysAddressService sysAddressService;

    @Resource
    private RegionGridUtilsService regionGridUtilsService;


    @ApiOperation("获取网格内地址树")
    @GetMapping("/getAddressTree/full")
    public R<List<AddressTreeVO>> getFullAddressTree(String code) {
        List<String> selectedRegionCodes = regionGridUtilsService.getSelectedRegionCodes("1", null);
        return R.ok(sysAddressService.getFullAddressTreeByCache(code, selectedRegionCodes));
    }

    @ApiOperation("获取完整地址树")
    @GetMapping("/getAddressTree")
    public R<AddressTreeVO> getAddressTree() {
        return R.ok(sysAddressService.getFullAddressTree());
    }

    @ApiOperation("获取地址树")
    @GetMapping("/getAddressTree/code")
    public R<List<AddressTreeVO>> getAddressTreeByCode(String code) {
        return R.ok(sysAddressService.getAddressTreeByCode(code));
    }



    @ApiOperation("获取行政区划详细信息")
    @GetMapping("/region")
    public R<AdminDivision> getAdminDivisionByCode(String code) {
        return R.ok(sysAddressService.getAdminDivisionByCode(code));
    }

    @ApiOperation("获取总行对应行政区划")
    @GetMapping("/dept/region")
    public R<String> getRegionCodeByDept() {
        return R.ok(sysAddressService.getRegionCodeByDept());
    }

    @Anonymous
    @ApiOperation("围栏数据重新加载")
    @GetMapping("/init")
    public R<String> init() {
        sysAddressService.initSHGeoData();
        return R.ok("围栏数据加载");
    }

}
