package com.czg.controller.admin;

import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.system.dto.SysParamsDTO;
import com.czg.system.service.SysParamsService;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author GYJoker
 */
@RequestMapping("/admin/sysParams")
@RestController
public class SysParamController {

    @Resource
    private SysParamsService sysParamsService;

    /**
     * 新增参数
     *
     * @param paramsDTO 参数
     * @return 新增结果
     */
    @PostMapping
    @SaAdminCheckPermission(parentName = "系统参数", value = "sysParams:add", name = "新增参数")
    public CzgResult<String> insertParams(@RequestBody @Validated({InsertGroup.class}) SysParamsDTO paramsDTO) {
        return sysParamsService.insertParams(paramsDTO);
    }

    /**
     * 修改参数
     *
     * @param paramsDTO 参数
     * @return 修改结果
     */
    @PutMapping
    @SaAdminCheckPermission(parentName = "系统参数", value = "sysParams:update", name = "修改参数")
    public CzgResult<String> updateParams(@RequestBody @Validated({UpdateGroup.class}) SysParamsDTO paramsDTO) {
        return sysParamsService.updateParams(paramsDTO);
    }

    /**
     * 删除参数
     *
     * @param code 参数code
     * @return 删除结果
     */
    @DeleteMapping("/{code}")
    @SaAdminCheckPermission(parentName = "系统参数", value = "sysParams:delete", name = "删除参数")
    public CzgResult<Boolean> deleteParams(@PathVariable String code) {
        return sysParamsService.deleteParams(code);
    }

    /**
     * 根据参数编码获取参数
     *
     * @param code 参数编码
     * @return 参数
     */
    @GetMapping("/code/{code}")
    @SaAdminCheckPermission(parentName = "系统参数", value = "sysParams:get:code", name = "获取参数根据code")
    public CzgResult<SysParamsDTO> getParamByCode(@PathVariable String code) {
        return sysParamsService.getParamsByCode(code);
    }

    /**
     * 根据参数类型获取参数列表
     *
     * @param type 参数类型
     * @return 参数列表
     */
    @GetMapping("/type/{type}")
    @SaAdminCheckPermission(parentName = "系统参数", value = "sysParams:get:type", name = "获取参数根据类型")
    public CzgResult<List<SysParamsDTO>> getParamsByType(@PathVariable String type) {
        return sysParamsService.getParamsByType(Integer.valueOf(type));
    }

}
