package com.CUST.brain.web.controller.system;

import com.CUST.brain.common.api.CommonResult;
import com.CUST.brain.dao.domain.system.SysParamType;
import com.CUST.brain.service.SysParamTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 系统参数类型管理
 */
@RestController
@RequestMapping("/system/param-type")
@Api(tags = "系统参数类型管理")
public class SysParamTypeController {

    @Resource
    private SysParamTypeService sysParamTypeService;

    @GetMapping("/list")
    @ApiOperation("分页查询参数类型列表")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Page<SysParamType>> list(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("类型名称") @RequestParam(required = false) String typeName) {
        
        Page<SysParamType> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysParamType> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(typeName != null, SysParamType::getTypeName, typeName)
                .eq(SysParamType::getFlag, 0)
                .orderByAsc(SysParamType::getSortOrder);
        
        return CommonResult.success(sysParamTypeService.page(page, wrapper));
    }

    @GetMapping("/all")
    @ApiOperation("获取所有参数类型")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<List<SysParamType>> listAll() {
        return CommonResult.success(sysParamTypeService.listAll());
    }

    @GetMapping("/{id}")
    @ApiOperation("获取参数类型详情")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<SysParamType> getById(@PathVariable String id) {
        return CommonResult.success(sysParamTypeService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增参数类型")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Boolean> add(@Valid @RequestBody SysParamType paramType) {
        return CommonResult.success(sysParamTypeService.save(paramType));
    }

    @PutMapping
    @ApiOperation("修改参数类型")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Boolean> update(@Valid @RequestBody SysParamType paramType) {
        return CommonResult.success(sysParamTypeService.updateParamType(paramType));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除参数类型")
    @PreAuthorize("hasAuthority('system:param')")
    public CommonResult<Boolean> delete(@PathVariable String id) {
        return CommonResult.success(sysParamTypeService.removeById(id));
    }
} 