package com.chushang.system.controller;

import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.excel.utils.ExcelUtils;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.security.utils.SecurityUtils;
import com.chushang.system.entity.po.SysConfig;
import com.chushang.system.entity.po.SysDictType;
import com.chushang.system.service.ISysConfigService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

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

    /**
     * 获取参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/list")
    public AjaxResult list(SysConfig config)
    {
        CommonParam commonParam = CommonParam.buildPageRequest();
        PageResult pageResult = configService.pageConfigList(config, commonParam);
        return AjaxResult.success(pageResult);
    }

    @SysLog(value = "参数管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:config:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfig config)
    {
        CommonParam commonParam = CommonParam.buildAllRequest();
        List<SysConfig> list = configService.allConfigList(config, commonParam);
        ExcelUtils.exportList(response, SysConfig.class, list, "参数数据");
    }

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

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey)
    {
        SysConfig config = new SysConfig();
        config.setConfigKey(configKey);
        CommonParam commonParam = CommonParam.buildPageRequest();
        return AjaxResult.success(configService.getOne(configService.buildWrapper(config, commonParam)));
    }

    /**
     * 新增参数配置
     */
    @RequiresPermissions("system:config:add")
    @SysLog(value = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysConfig config)
    {
        if (configService.checkConfigKeyUnique(config, CommonParam.buildAllRequest()))
        {
            return AjaxResult.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        configService.saveOrUpdate(config);
        return AjaxResult.success();
    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("system:config:edit")
    @SysLog(value = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysConfig config)
    {
        if (configService.checkConfigKeyUnique(config, CommonParam.buildAllRequest()))
        {
            return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        configService.saveOrUpdate(config);
        return AjaxResult.success();
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("system:config:remove")
    @SysLog(value = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        configService.removeBatchByIds(Arrays.asList(configIds));
        return AjaxResult.success();
    }
}
