package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.SysConfig;
import com.qiaoba.api.system.entity.param.SysConfigParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.common.poi.util.ExcelUtil;
import com.qiaoba.module.system.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
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 javax.validation.constraints.Size;
import java.util.List;

/**
 * 参数管理 web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@Validated
@RestController
@RequestMapping("/system/config")
@RequiredArgsConstructor
@Tag(name = "参数管理")
public class SysConfigController {

    private final SysConfigService sysConfigService;

    @PreAuthorize("hasAuthority('system:config:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public TableDataInfo list(@Validated SysConfigParam param, PageQuery pageQuery) {
        return sysConfigService.selectPageList(param, pageQuery);
    }

    @PreAuthorize("hasAuthority('system:config:export')")
    @PostMapping("/export")
    @Operation(summary = "导出数据")
    public void export(@Validated HttpServletResponse response, SysConfigParam param) {
        List<SysConfig> list = sysConfigService.selectList(param);
        ExcelUtil.exportExcel(list, SysConfig.class, "参数数据", response);
    }

    @PreAuthorize("hasAuthority('system:config:query')")
    @GetMapping(value = "/{configId}")
    @Operation(summary = "获取详情")
    public AjaxResult getInfo(@Size(min = 19, max = 20, message = "ID不合法") @PathVariable String configId) {
        return AjaxResult.success(sysConfigService.selectById(configId));
    }

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

    @PreAuthorize("hasAuthority('system:config:add')")
    @PostMapping
    @Operation(summary = "新增配置")
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody SysConfig config) {
        return AjaxResult.toAjax(sysConfigService.insert(config));
    }

    @PreAuthorize("hasAuthority('system:config:edit')")
    @PutMapping
    @Operation(summary = "修改配置")
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody SysConfig config) {
        return AjaxResult.toAjax(sysConfigService.updateById(config));
    }

    @PreAuthorize("hasAuthority('system:config:remove')")
    @DeleteMapping("/{configIds}")
    @Operation(summary = "删除配置")
    public AjaxResult remove(@PathVariable List<String> configIds) {
        return AjaxResult.toAjax(sysConfigService.deleteByIds(configIds));
    }

    @PreAuthorize("hasAuthority('system:config:remove')")
    @DeleteMapping("/refreshCache")
    @Operation(summary = "刷新缓存")
    public AjaxResult refreshCache() {
        sysConfigService.resetConfigCache();
        return AjaxResult.success();
    }
}
