package com.yiqi.web.controller.system;

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

import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.system.domain.SysConfigRecord;
import com.yiqi.system.service.ISysConfigRecordService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 配置Controller
 *
 * @author douya
 * @date 2024-11-01
 */
@Api(tags = "管理后台 - 配置")
@RestController
@RequestMapping(UrlConstants.managerApi + "/system/configRecord")
public class SysConfigRecordController extends BaseController {
    @Autowired
    private ISysConfigRecordService sysConfigRecordService;


    //<editor-folder desc="基础函数">

    /**
     * 查询配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:configRecord:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询配置列表")
    public TableDataInfo list(SysConfigRecord sysConfigRecord) {
        startPage();
        List<SysConfigRecord> list = sysConfigRecordService.selectSysConfigRecordList(sysConfigRecord);
        return getDataTable(list);
    }


    /**
     * 导出配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:configRecord:export')")
    @Log(title = "配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出配置列表")
    public void export(HttpServletResponse response, SysConfigRecord sysConfigRecord) {
        List<SysConfigRecord> list = sysConfigRecordService.selectSysConfigRecordList(sysConfigRecord);
        ExcelUtil<SysConfigRecord> util = new ExcelUtil<SysConfigRecord>(SysConfigRecord.class);
        util.exportExcel(response, list, "配置数据");
    }

    /**
     * 获取配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:configRecord:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取配置详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(sysConfigRecordService.selectSysConfigRecordById(id));
    }


    @ApiOperation("通过参数获取参数配置，多个参数用英文逗号隔开")
    @GetMapping(value = "getConfigValueByKey")
    public AjaxResult getConfigValueByKey(@RequestParam String configKeys) {
        String[] keys = configKeys.split(";");
        List<SysConfigRecord> sysConfigRecordList = sysConfigRecordService.getConfigRecordByKey(keys);
        Map<String, Object> map = new HashMap<>();
        for (SysConfigRecord record : sysConfigRecordList) {
            map.put(record.getMenuName(), record.getValue());
        }
        return success(map);
    }

    /**
     * 新增配置
     */
    @PreAuthorize("@ss.hasPermi('system:configRecord:add')")
    @Log(title = "配置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增配置")
    public AjaxResult add(@RequestBody SysConfigRecord sysConfigRecord) {
        return toAjax(sysConfigRecordService.insertSysConfigRecord(sysConfigRecord));
    }

    /**
     * 修改配置
     */
    @PreAuthorize("@ss.hasPermi('system:configRecord:edit')")
    @Log(title = "配置", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改配置")
    public AjaxResult edit(@RequestBody SysConfigRecord sysConfigRecord) {
        return toAjax(sysConfigRecordService.updateSysConfigRecord(sysConfigRecord));
    }

    /**
     * 删除配置
     */
    @PreAuthorize("@ss.hasPermi('system:configRecord:remove')")
    @Log(title = "配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除配置")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(sysConfigRecordService.deleteSysConfigRecordByIds(ids));
    }

    /**
     * 修改配置状态
     */
    @PreAuthorize("@ss.hasPermi('system:configRecord:edit')")
    @Log(title = "配置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateStatus")
    @ApiOperation(value = "修改配置状态")
    public AjaxResult updateSysConfigRecordStatus(@RequestBody SysConfigRecord sysConfigRecord) {
        return toAjax(sysConfigRecordService.updateSysConfigRecordStatus(sysConfigRecord));
    }

    /**
     * 删除配置分类
     */
    @DeleteMapping("/setting/{id}")
    @ApiOperation(value = "删除配置分类")
    public AjaxResult remove(@PathVariable Integer id) {
        return toAjax(sysConfigRecordService.deleteSysConfigRecordById(id));
    }


}
