package org.nl.system.controller.param;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.param.ISysParamService;
import org.nl.system.service.param.dao.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统参数表 前端控制器
 * </p>
 *
 * @author generator
 * @since 2022-12-14
 */
@Api(tags = "系统参数管理")
@Slf4j
@RestController
@RequestMapping("/api/param")
class SysParamController {

    @Autowired
    private ISysParamService paramService;
    @GetMapping
    @Log("查询系统参数")
    @ApiOperation("查询系统参数")
    //@SaCheckPermission("param:list")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, PageQuery page){
        return new ResponseEntity<>(TableDataInfo.build(paramService.queryPage(whereJson, page)), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增系统参数")
    @ApiOperation("新增系统参数")
    //@SaCheckPermission("param:add")
    public ResponseEntity<Object> create(@Validated @RequestBody Param param){
        paramService.create(param);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改系统参数")
    @ApiOperation("修改系统参数")
    //@SaCheckPermission("param:edit")
    public ResponseEntity<Object> update(@Validated @RequestBody Param param){
        paramService.update(param);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除系统参数")
    @ApiOperation("删除系统参数")
    //@SaCheckPermission("param:del")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        List<String> Ids = Arrays.asList(ids);
        paramService.deleteByIds(Ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/getValueByCode/{code}")
    @Log("根据编码获取值")
    @ApiOperation("根据编码获取值")
    @SaIgnore
    public ResponseEntity<Object> getValueByCode(@PathVariable String code) {
        return new ResponseEntity<>(paramService.findByCode(code), HttpStatus.CREATED);
    }

}


