package com.changhu.controller;

import com.changhu.common.annotation.JsonBody;
import com.changhu.common.cache.GlobalCacheManager;
import com.changhu.common.pojo.model.JsonResult;
import com.changhu.common.pojo.vo.SelectNodeVo;
import com.changhu.common.pojo.vo.TreeNodeVo;
import com.changhu.pojo.params.PoliceUnitRegisterParams;
import com.changhu.pojo.params.SecurityUnitRegisterParams;
import com.changhu.pojo.queryParams.QueryUnitListByIdentityQueryParams;
import com.changhu.service.AdministrativeDivisionService;
import com.changhu.service.CommonService;
import com.changhu.support.minio.service.MinioService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author 20252
 * @createTime 2024/8/30 下午2:16
 * @desc CommonController...
 */
@Tag(name = "公共接口")
@RequestMapping("/common")
@JsonBody
public class CommonController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private MinioService minioService;

    @Autowired
    private AdministrativeDivisionService administrativeDivisionService;

    @Cacheable(value = "common", key = "'AdministrativeDivisionLevel' + #level + 'Tree'")
    @Operation(summary = "行政区划树")
    @GetMapping("/administrativeDivisionTree")
    public List<TreeNodeVo<String>> AdministrativeDivisionTree(@Schema(description = "等级") @RequestParam(defaultValue = "4") Integer level) {
        return administrativeDivisionService.tree(level);
    }

    @Operation(summary = "根据父编码查询子级行政区划")
    @GetMapping("/administrativeDivisionByParentCode")
    public List<TreeNodeVo<String>> administrativeDivisionByParentCode(@Schema(description = "父编码") @RequestParam(defaultValue = "0") String parentCode) {
        return administrativeDivisionService.administrativeDivisionByParentCode(parentCode);
    }

    @Operation(summary = "保安单位注册")
    @PostMapping("/securityUnitRegister")
    public JsonResult<Void> securityUnitRegister(@RequestBody @Valid SecurityUnitRegisterParams params) {
        return commonService.securityUnitRegister(params);
    }

    @Operation(summary = "公安单位注册")
    @PostMapping("/policeUnitRegister")
    public JsonResult<Void> policeUnitRegister(@RequestBody @Valid PoliceUnitRegisterParams params) {
        return commonService.policeUnitRegister(params);
    }

    @Operation(summary = "获取临时上传地址")
    @GetMapping("/getResignedObjectUrl")
    public String getResignedObjectUrl(@RequestParam @Schema(description = "bucketName") String bucketName,
                                       @RequestParam @Schema(description = "对象地址") String objectName) {
        return minioService.getResignedObjectUrl(bucketName, objectName);
    }

    @Operation(summary = "根据身份查询单位列表")
    @PostMapping("/queryUnitListByIdentity")
    public List<SelectNodeVo<Long>> queryUnitListByIdentity(@RequestBody QueryUnitListByIdentityQueryParams queryParams) {
        return commonService.queryUnitListByIdentity(queryParams);
    }


    @Operation(summary = "字典数据")
    @GetMapping("/enums")
    public Map<String, List<SelectNodeVo<?>>> enums() {
        // 初始化 enumsResult
        Map<String, List<SelectNodeVo<?>>> enumsResult = new HashMap<>();
        //将缓存的数据转换
        GlobalCacheManager.ENUM_CACHE.forEach((aClass, item) -> enumsResult.put(aClass.getSimpleName(), item.values().stream().toList()));
        return enumsResult;
    }

}
