package com.CUST.brain.web.controller.system;

import com.CUST.brain.common.api.CommonResult;
import com.CUST.brain.dao.domain.system.SysParam;
import com.CUST.brain.service.SysParamService;
import com.CUST.brain.common.utils.CacheCleanUtils;
import com.CUST.brain.common.cache.ParamCacheManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("/system/param")
@Api(tags = "系统参数管理")
public class SysParamController {

    @Resource
    private SysParamService sysParamService;

    @Resource
    private CacheCleanUtils cacheCleanUtils;

    @Resource
    private ParamCacheManager paramCacheManager;

    @GetMapping("/type/{typeCode}")
    @ApiOperation("根据参数类型获取参数列表")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Map<String, String>> getParamsByType(
            @ApiParam("参数类型编码") @PathVariable String typeCode) {
        return CommonResult.success(sysParamService.getParamsByType(typeCode));
    }

    @GetMapping("/value")
    @ApiOperation("获取参数值")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<String> getParamValue(
            @ApiParam("参数类型编码") @RequestParam String typeCode,
            @ApiParam("参数名称") @RequestParam String paramName) {
        return CommonResult.success(sysParamService.getParamValue(typeCode, paramName));
    }

    @GetMapping("/list")
    @ApiOperation("分页查询参数列表")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<List<SysParam>> list(
            @ApiParam("参数类型编码") @RequestParam String typeCode) {
        return CommonResult.success(sysParamService.listByType(typeCode));
    }

    @GetMapping("/batch")
    @ApiOperation("批量获取多个参数类型的参数列表")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Map<String, List<SysParam>>> getParamsByTypes(
            @ApiParam("参数类型编码列表，多个用逗号分隔") @RequestParam String typeCodes) {
        return CommonResult.success(sysParamService.getParamsByTypes(Arrays.asList(typeCodes.split(","))));
    }

    @PostMapping
    @ApiOperation("新增参数")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Boolean> add(@RequestBody SysParam param) {
        boolean result = sysParamService.save(param);
        if (result) {
            cacheCleanUtils.clearParamCache(param.getParamType(), null);
        }
        return CommonResult.success(result);
    }

    @PutMapping
    @ApiOperation("修改参数")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Boolean> update(@RequestBody SysParam param) {
        boolean result = sysParamService.updateParam(param);
        if (result) {
            cacheCleanUtils.clearParamCache(param.getParamType(), param.getParamName());
        }
        return CommonResult.success(result);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除参数")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Boolean> delete(@PathVariable String id) {
        SysParam param = sysParamService.getById(id);
        boolean result = sysParamService.removeById(id);
        if (result && param != null) {
            cacheCleanUtils.clearParamCache(param.getParamType(), param.getParamName());
        }
        return CommonResult.success(result);
    }

    @PostMapping("/refresh")
    @ApiOperation("刷新参数缓存")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Boolean> refreshCache(
            @ApiParam("参数类型编码，不传则刷新所有参数缓存") @RequestParam(required = false) String typeCode) {
        if (typeCode != null) {
            // 删除指定类型的缓存，下次访问时会自动重新加载
            paramCacheManager.removeParamCache(typeCode);
        } else {
            // 删除所有参数缓存，下次访问时会自动重新加载
            paramCacheManager.removeAllParamCache();
        }
        return CommonResult.success(true);
    }
} 