package com.inspect.system.controller;

import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.domain.SysConfig;
import com.inspect.system.service.ISysConfigService;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/config"})
public class SysConfigController extends BaseController {
    private final ISysConfigService configService;

    public SysConfigController(ISysConfigService configService) {
        this.configService = configService;
    }

    @RequiresPermissions({"system:config:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(SysConfig config) {
        this.startPage();
        List<SysConfig> list = this.configService.selectConfigList(config);
        return this.getDataTable(list);
    }

    @Log(
            title = "参数管理",
            businessType = BizType.EXPORT
    )
    @RequiresPermissions({"system:config:export"})
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, SysConfig config) {
        List<SysConfig> list = this.configService.selectConfigList(config);
        ExcelUtil<SysConfig> util = new ExcelUtil<>(SysConfig.class);
        util.exportExcel(response, list, "参数数据");
    }

    @GetMapping({"/{configId}"})
    public AjaxResult getInfo(@PathVariable Long configId) {
        return AjaxResult.success(this.configService.selectConfigById(configId));
    }

    @GetMapping({"/configKey/{configKey}"})
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        return AjaxResult.success(this.configService.selectConfigByKey(configKey));
    }

    @GetMapping({"/configKeys/{configKeys}"})
    public AjaxResult getConfigKeys(@PathVariable String configKeys) {
        return AjaxResult.success(this.configService.selectConfigByKeys(configKeys));
    }

    @RequiresPermissions({"system:config:add"})
    @Log(
            title = "参数管理",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysConfig config) {
        if ("1".equals(this.configService.checkConfigKeyUnique(config))) {
            return AjaxResult.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        } else {
            config.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.configService.insertConfig(config));
        }
    }

    @RequiresPermissions({"system:config:edit"})
    @Log(
            title = "参数管理",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@Validated @RequestBody SysConfig config) {
        if ("1".equals(this.configService.checkConfigKeyUnique(config))) {
            return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        } else {
            config.setUpdateBy(SecurityUtils.getUsername());
            return this.toAjax(this.configService.updateConfig(config));
        }
    }

    @RequiresPermissions({"system:config:remove"})
    @Log(
            title = "参数管理",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{configIds}"})
    public AjaxResult remove(@PathVariable Long[] configIds) {
        this.configService.deleteConfigByIds(configIds);
        return this.success();
    }

    @RequiresPermissions({"system:config:remove"})
    @Log(
            title = "参数管理",
            businessType = BizType.CLEAN
    )
    @PostMapping({"/refreshCache"})
    public AjaxResult refreshCache() {
        this.configService.resetConfigCache();
        return AjaxResult.success();
    }
}
