package cn.ysk.cashier.system.rest;

import cn.hutool.core.bean.BeanUtil;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.system.domain.Params;
import cn.ysk.cashier.system.service.ParamsService;
import cn.ysk.cashier.system.service.dto.ParamsDTO;
import cn.ysk.cashier.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.Map;


/**
 * 参数管理
 *
 * @author tankaikai
 * @since 2024-09-27 11:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/params")
@Api(tags="参数管理")
public class ParamsController {

    private final ParamsService paramsService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = "size", value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = "paramCode", value = "参数编码", paramType = "query", dataType="String")
    })
    public ResponseEntity page(@ApiIgnore @RequestParam Map<String, Object> params){
        Map<String, Object> page = paramsService.page(params);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public ResponseEntity get(@PathVariable("id") Long id){
        Params entity = paramsService.getById(id);
        ParamsDTO data = BeanUtil.toBean(entity, ParamsDTO.class);
        return ResponseEntity.ok().body(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @Log("保存")
    public ResponseEntity save(@RequestBody ParamsDTO dto){
        Params entity = BeanUtil.toBean(dto, Params.class);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        entity.setCreateBy(currentUserId);
        entity.setCreateTime(new Date());
        entity.setUpdateBy(currentUserId);
        entity.setUpdateTime(entity.getCreateTime());
        paramsService.save(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @ApiOperation("修改")
    @Log("修改")
    public ResponseEntity update(@RequestBody ParamsDTO dto){
        Long paramId = dto.getParamId();
        Params entity = paramsService.getById(paramId);
        BeanUtil.copyProperties(dto, entity,"createBy","createTime");
        Long currentUserId = SecurityUtils.getCurrentUserId();
        entity.setUpdateBy(currentUserId);
        entity.setUpdateTime(entity.getCreateTime());
        paramsService.updateById(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @Log("删除")
    public ResponseEntity delete(@PathVariable("paramId") Long paramId){
        paramsService.removeById(paramId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}