package cn.ysk.cashier.cons.rest;

import cn.hutool.core.util.ObjectUtil;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.cons.domain.TbConUnit;
import cn.ysk.cashier.cons.service.TbConUnitService;
import cn.ysk.cashier.cons.service.dto.TbConUnitQueryCriteria;
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 java.util.Objects;
import javax.servlet.http.HttpServletResponse;

/**
* @author admin
* @date 2024-07-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "耗材单位添加管理")
@RequestMapping("/api/tbConUnit")
public class TbConUnitController {

    private final TbConUnitService tbConUnitService;

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

    @GetMapping
    @ApiOperation("查询耗材单位添加")
    public ResponseEntity<Object> queryTbConUnit(TbConUnitQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbConUnitService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增耗材单位添加")
    @ApiOperation("新增耗材单位添加")
    public ResponseEntity<Object> createTbConUnit(@Validated @RequestBody TbConUnit resources){

        if(ObjectUtil.isEmpty(resources.getParentId())|| Objects.isNull(resources.getParentId())){
            resources.setParentId(0);
        }
        resources.setStatus("0");
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return new ResponseEntity<>(tbConUnitService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改耗材单位添加")
    @ApiOperation("修改耗材单位添加")
    public ResponseEntity<Object> updateTbConUnit(@Validated @RequestBody TbConUnit resources){

        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        tbConUnitService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除耗材单位添加")
    @ApiOperation("删除耗材单位添加")
    public ResponseEntity<Object> deleteTbConUnit(@RequestBody Integer[] ids) {
        tbConUnitService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }



    @RequestMapping("queryTbConUnitInfo")
    public ResponseEntity<Object> queryTbConUnitInfo(TbConUnitQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbConUnitService.queryAllInfo(criteria,pageable),HttpStatus.OK);
    }
}