package com.bonus.system.controller;

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

import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.domain.SysConfig;
import com.bonus.system.service.ISysConfigService;

/**
 * 参数配置 信息操作处理
 * 
 * @author bonus
 */
@RestController
@RequestMapping("/config")
@Slf4j
public class SysConfigController extends BaseController {

    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/list")
    @SysLog(title = "参数配置", businessType = OperaType.QUERY,logType = 0,module = "系统管理->参数配置")
    public TableDataInfo list(SysConfig config) {
        try{
            startPage();
            List<SysConfig> list = configService.selectConfigList(config);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<SysConfig>());
    }

    @RequiresPermissions("system:config:export")
    @PostMapping("/export")
    @SysLog(title = "参数配置", businessType = OperaType.EXPORT,logType = 0,module = "系统管理->参数配置")
    public void export(HttpServletResponse response, SysConfig config) {
      try{
          List<SysConfig> list = configService.selectConfigList(config);
          ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
          util.exportExcel(response, list, "参数数据");
      }catch (Exception e){
          ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
          util.exportExcel(response, new ArrayList<SysConfig>(), "参数数据");
          log.error(e.toString(),e);
      }
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long configId) {
        try{
            return success(configService.selectConfigById(configId));
        }catch (Exception e){
            log.error(e.toString(),e);
            return error("系统异常");
        }
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        try{
            return success(configService.selectConfigByKey(configKey));
        }catch (Exception e){
            log.error(e.toString(),e);
            return error("系统异常");
        }
    }

    /**
     * 新增参数配置
     */
    @RequiresPermissions("system:config:add")
    @SysLog(title = "参数配置", businessType = OperaType.INSERT,logType = 0,module = "系统管理->参数配置")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysConfig config) {
        try{
            if (!configService.checkConfigKeyUnique(config)) {
                return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
            }
            config.setCreateBy(SecurityUtils.getUsername());
            return toAjax(configService.insertConfig(config));
        }catch (Exception e){
            log.error(e.toString(),e);
            return error("系统异常");
        }
    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("system:config:edit")
    @PutMapping
    @SysLog(title = "参数配置", businessType = OperaType.UPDATE,logType = 0,module = "系统管理->参数配置")
    public AjaxResult edit(@Validated @RequestBody SysConfig config) {
        try{
            if (!configService.checkConfigKeyUnique(config)) {
                return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
            }
            config.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(configService.updateConfig(config));
        }catch (Exception e){
            log.error(e.toString(),e);
            return error("系统异常");
        }
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("system:config:remove")
    @SysLog(title = "参数配置", businessType = OperaType.DELETE,logType = 0,module = "系统管理->参数配置")
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        try{
            configService.deleteConfigByIds(configIds);
            return success();
        }catch (Exception e){
            log.error(e.toString(),e);
            return error("系统异常");
        }
    }

    /**
     * 刷新参数缓存
     */
    @RequiresPermissions("system:config:remove")
    @DeleteMapping("/refreshCache")
    @SysLog(title = "参数配置", businessType = OperaType.FLASH,logType = 0,module = "系统管理->参数配置",details = "刷新参数缓存")
    public AjaxResult refreshCache() {
      try{
          configService.resetConfigCache();
          return success();
      }catch (Exception e){
          log.error(e.toString(),e);
          return error("系统异常");
      }
    }
}
