package com.ghy.web.controller.config;

import com.ghy.common.core.domain.AjaxResult;
import com.ghy.system.service.ISysGlobalConfigService;
import com.ghy.web.pojo.vo.SysGlobalConfigRequestVO;
import com.ghy.web.pojo.vo.SysGlobalConfigResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 但星霖
 * @Date: 2023-11-09 14:42
 * @Version: 1.0
 * @Description: web-系统配置控制层
 */
@Controller
@RequestMapping("/config/globalConfig")
@Tag(name = "web-系统配置控制层")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysGlobalConfigController {

    private String prefix = "config/globalConfig";

    @RequiresPermissions("sys:order:audio:config")
    @GetMapping()
    public String place() {
        return prefix + "/edit";
    }

    private final ISysGlobalConfigService service;


    @ResponseBody
    @Operation(summary = "系统配置查询接口(根据类型查询)", method = "GET")
    @Parameters({
            @Parameter(name = "typeId", description = "配置类型Code", required = true, in = ParameterIn.PATH)
    })
    @GetMapping(path = "/search/type/{typeId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult searchByType(@PathVariable("typeId") Integer typeId) {
        return AjaxResult.success(SysGlobalConfigResponseVO.toVo(service.loadByConfigType(typeId)));
    }

    @ResponseBody
    @RequiresPermissions("sys:order:audio:config:update")
    @Operation(summary = "系统配置修改", method = "PUT")
    @PutMapping(path = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult update(@RequestBody SysGlobalConfigRequestVO sysConfigRequestVO) {
        return AjaxResult.success(service.update(SysGlobalConfigRequestVO.toModel(sysConfigRequestVO)));
    }

}
