package com.taais.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.core.BaseController;
import com.taais.system.domain.bo.SysConfigBo;
import com.taais.system.domain.vo.SysConfigVo;
import com.taais.system.service.ISysConfigService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author wgk
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {
    @Resource
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @SaCheckPermission("system:config:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysConfigVo>> list(SysConfigBo config) {
        return CommonResult.success(configService.selectConfigPage(config));
    }

    /**
     * 导出参数配置列表
     */
    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:config:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigBo config) {
        List<SysConfigVo> list = configService.selectConfigList(config);
        ExcelUtil.exportExcel(list, "参数数据", SysConfigVo.class, response);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @SaCheckPermission("system:config:query")
    @GetMapping(value = "/{configId}")
    public CommonResult<SysConfigVo> getInfo(@PathVariable Long configId) {
        return CommonResult.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @SaCheckPermission("system:config:query")
    @GetMapping(value = "/configKey/{configKey}")
    public CommonResult<SysConfigVo> getConfigKey(@PathVariable String configKey) {
        return CommonResult.success(configService.selectConfigByKey(configKey), "操作成功");
    }

    /**
     * 新增参数配置
     */
    @SaCheckPermission("system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody SysConfigBo config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return CommonResult.fail("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        boolean inserted = configService.insertConfig(config);
        if (!inserted) {
            return CommonResult.fail("新增参数记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改参数配置
     */
    @SaCheckPermission("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SysConfigBo config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return CommonResult.fail("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        boolean updated = configService.updateConfig(config);
        if (!updated) {
            return CommonResult.fail("修改参数'" + config.getConfigName() + "'失败!");
        }
        return CommonResult.success();
    }

    /**
     * 根据参数键名修改参数配置
     */
    @SaCheckPermission("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateByKey")
    public CommonResult<Void> updateByKey(@RequestBody SysConfigBo config) {
        boolean updated = configService.updateConfigByKey(config);
        if (!updated) {
            return CommonResult.fail("修改参数'" + config.getConfigName() + "'失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除参数配置
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public CommonResult<Void> remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return CommonResult.success();
    }

    /**
     * 刷新参数缓存
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public CommonResult<Void> refreshCache() {
        configService.resetConfigCache();
        return CommonResult.success();
    }
}
