package com.ruoyi.ibs.datavisual.controller;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.ibs.datavisual.domain.vo.AdminRegionDataVO;
import com.ruoyi.ibs.datavisual.service.AdminRegionDataService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2025/3/28
 **/
@Api(tags = "行政区划热力图")
@RestController
@RequestMapping("/admin/region")
public class AdminRegionDataController extends BaseController{
    @Resource
    private AdminRegionDataService adminRegionDataService;

    @Resource
    private RedisCache redisCache;

    @ApiOperation("获取指标码值表")
    @Log(title = "行政区划热力图-获取指标码值表")
    @GetMapping("/index")
    public R<List<SysDictData>> getIndexDict(@RequestParam String opsDept){
        List<SysDictData> sysDictTypeArrayList = new ArrayList<SysDictData>(redisCache.getCacheObject("sys_dict:admin_region_data_metric"));
        String prefix = String.valueOf(Integer.parseInt(opsDept) + 1);
        return R.ok(sysDictTypeArrayList.stream().filter(sysDictData -> sysDictData.getDictValue().startsWith(prefix)).collect(Collectors.toList()));
    }

    @ApiOperation("获取乡镇数据")
    @Log(title = "行政区划热力图-获取乡镇数据")
    @GetMapping("/town")
    public R<List<AdminRegionDataVO>> getTownDataList(@RequestParam(required = false) String opsDept, @RequestParam String itemIdx,@RequestParam(required = false) String branchName, @RequestParam(required = false) String outletName, @RequestParam(required = false) String nickName,@RequestParam String dt){
        return R.ok(adminRegionDataService.getTownDataList(opsDept, itemIdx, branchName,  outletName,  nickName,dt));
    }

    @ApiOperation("获取村数据")
    @Log(title = "行政区划热力图-获取村数据")
    @GetMapping("/village")
    public R<List<AdminRegionDataVO>> getVillageDataList(@RequestParam(required = false) String code, @RequestParam(required = false) String opsDept, @RequestParam String itemIdx,@RequestParam(required = false) String branchName, @RequestParam(required = false) String outletName, @RequestParam(required = false) String nickName,@RequestParam String dt){
        return R.ok(adminRegionDataService.getVillageDataList(code, opsDept, itemIdx, branchName,  outletName,  nickName,dt));
    }

    @ApiOperation("获取详情")
    @Log(title = "行政区划热力图-获取详情")
    @GetMapping("/detail")
    public R<List<AdminRegionDataVO>> getDetail(@RequestParam String code, @RequestParam String opsDept, @RequestParam String itemIdx,@RequestParam String dt){
        return R.ok(adminRegionDataService.getDetail(code, opsDept, itemIdx,dt));
    }
}
