package com.shkj.web.controller.system;

import com.shkj.common.annotation.Log;
import com.shkj.common.constant.UserConstants;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.AjaxResult;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.common.enums.BusinessType;
import com.shkj.common.utils.poi.ExcelUtil;
import com.shkj.system.domain.SysParameter;
import com.shkj.system.service.ISysParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @menu 参数配置 信息操作处理
 * 
 * @author lch
 */
@RestController
@RequestMapping("/system/config")
public class SysParameterController extends BaseController
{
    @Autowired
    private ISysParameterService parameterService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysParameter config)
    {
        startPage();
        List<SysParameter> list = parameterService.selectConfigList(config);
        return getDataTable(list);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysParameter config)
    {
        List<SysParameter> list = parameterService.selectConfigList(config);
        ExcelUtil<SysParameter> util = new ExcelUtil<SysParameter>(SysParameter.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long configId)
    {
        return AjaxResult.success(parameterService.selectConfigById(configId));
    }

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

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysParameter config)
    {
        if (UserConstants.NOT_UNIQUE.equals(parameterService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.error("参数键名已存在");
        }
        config.setCreateBy(getUsername());
        return toAjax(parameterService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysParameter config)
    {
        if (UserConstants.NOT_UNIQUE.equals(parameterService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.error("参数键名已存在");
        }
        config.setUpdateBy(getUsername());
        return toAjax(parameterService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        parameterService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        parameterService.resetConfigCache();
        return AjaxResult.success();
    }
}
