package com.usky.system.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.exception.BusinessErrorCode;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.system.controller.web.page.TableDataInfo;
import com.usky.system.domain.SysConfig;
import com.usky.system.domain.constants.UserConstants;
import com.usky.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


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

    /**
     * 获取参数配置列表
     */
    @GetMapping("/list")
    public ApiResult<TableDataInfo> list(SysConfig config)
    {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return ApiResult.success(getDataTable(list));
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    public ApiResult getInfo(@PathVariable Long configId)
    {
        return ApiResult.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public ApiResult getConfigKey(@PathVariable String configKey)
    {
        return ApiResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @Log(title = "新增-参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult add(@Validated @RequestBody SysConfig config)
    {
        config.setCreateBy(SecurityUtils.getUsername());
        config.setTenantId(SecurityUtils.getTenantId());
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return ApiResult.error(BusinessErrorCode.BIZ_BUSINESS_ERROR.getCode(), "新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @Log(title = "修改-参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult edit(@Validated @RequestBody SysConfig config)
    {
        config.setTenantId(SecurityUtils.getTenantId());
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return ApiResult.error(BusinessErrorCode.BIZ_BUSINESS_ERROR.getCode(), "修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @Log(title = "删除-参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public ApiResult remove(@PathVariable Long[] configIds)
    {
        configService.deleteConfigByIds(configIds);
        return ApiResult.success();
    }

    /**
     * 刷新参数缓存
     */
    @DeleteMapping("/refreshCache")
    public ApiResult refreshCache()
    {
        configService.resetConfigCache();
        return ApiResult.success();
    }
}
