package cn.ysk.cashier.controller.product;

import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.pojo.product.TbProductStockOperate;
import cn.ysk.cashier.service.TbProductStockOperateService;
import cn.ysk.cashier.dto.product.OutAndOnDto;
import cn.ysk.cashier.dto.product.TbProductStockOperateQueryCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2024-01-25
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "出入库操作记录管理")
@RequestMapping("/api/tbProductStockOperate")
public class TbProductStockOperateController {

    private final TbProductStockOperateService tbProductStockOperateService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportTbProductStockOperate(HttpServletResponse response, TbProductStockOperateQueryCriteria criteria) throws IOException {
        tbProductStockOperateService.download(tbProductStockOperateService.queryAll(criteria), response);
    }

    @PostMapping("/list")
    public ResponseEntity<Object> queryTbProductStockOperate(@RequestBody TbProductStockOperateQueryCriteria criteria){
        return new ResponseEntity<>(tbProductStockOperateService.queryAllPage(criteria),HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Object> queryById(@PathVariable Integer id){
        return new ResponseEntity<>(tbProductStockOperateService.findById(id),HttpStatus.OK);
    }

    @GetMapping("/nullify/{id}")
    public ResponseEntity<Object> nullify(@PathVariable Integer id){
        tbProductStockOperateService.nullify(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    @PostMapping
    @ApiOperation("新增/product/StockOperate")
    @PreAuthorize("@el.check('tbProductStockOperate:add')")
    public ResponseEntity<Object> createTbProductStockOperate(@Validated @RequestBody TbProductStockOperate resources){
        return new ResponseEntity<>(tbProductStockOperateService.create(resources),HttpStatus.CREATED);
    }

    @PostMapping("/outAndOn")
    @ApiOperation("新增/product/StockOperate")
    @Log("商品::#outAndOnDto.type")
//    @PreAuthorize("@el.check('tbProductStockOperate:add')")
    public ResponseEntity<Object> createOutAndONOperate(@RequestBody OutAndOnDto outAndOnDto){
        return new ResponseEntity<>(tbProductStockOperateService.createOutAndONOperate(outAndOnDto),HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改/product/StockOperate")
    @PreAuthorize("@el.check('tbProductStockOperate:edit')")
    public ResponseEntity<Object> updateTbProductStockOperate(@Validated @RequestBody TbProductStockOperate resources){
        tbProductStockOperateService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @ApiOperation("删除/product/StockOperate")
    @PreAuthorize("@el.check('tbProductStockOperate:del')")
    public ResponseEntity<Object> deleteTbProductStockOperate(@RequestBody Integer[] ids) {
        tbProductStockOperateService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
