package cn.ysk.cashier.cons.rest;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.cons.domain.TbConsType;
import cn.ysk.cashier.cons.service.TbConsTypeService;
import cn.ysk.cashier.cons.service.dto.TbConsTypeQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletResponse;

/**
* @author admin
* @date 2024-06-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "耗材类型管理")
@RequestMapping("/api/tbConsType")
public class TbConsTypeController {

    private final TbConsTypeService tbConsTypeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportTbConsType(HttpServletResponse response, TbConsTypeQueryCriteria criteria) throws IOException {
        tbConsTypeService.download(tbConsTypeService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询耗材类型")
    public ResponseEntity<Object> queryTbConsType(TbConsTypeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbConsTypeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增耗材类型")
    @ApiOperation("新增耗材类型")
    public ResponseEntity<Object> createTbConsType(@Validated @RequestBody TbConsType resources) throws Exception {
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return new ResponseEntity<>(tbConsTypeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改耗材类型")
    @ApiOperation("修改耗材类型")
    public ResponseEntity<Object> updateTbConsType(@Validated @RequestBody TbConsType resources){
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        tbConsTypeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除耗材类型")
    @ApiOperation("删除耗材类型")
    public ResponseEntity<Object> deleteTbConsType(@RequestBody Integer[] ids) {
        tbConsTypeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}