package cn.ysk.cashier.controller.product;

import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.dto.product.TbProductStocktakinDTO;
import cn.ysk.cashier.dto.product.TbProductStocktakinQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.service.product.TbProductStocktakinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequiredArgsConstructor
@Api(tags = "库存盘点操作")
@RequestMapping("/api/tbProductStocktakin")
public class TbProductStocktakinController {
    private final TbProductStocktakinService tbProductStocktakinService;

    @PostMapping
    @ApiOperation("新增盘点")
    public ResponseEntity<Object> addStocktakin(
            @RequestBody @Validated TbProductStocktakinDTO productStocktakinDTO
    ) {

        if (productStocktakinDTO.getShopId() == null ||  productStocktakinDTO.getProductId() == null || productStocktakinDTO.getStocktakinNum() == null) {
            throw new BadRequestException("参数缺失");
        }

        if (productStocktakinDTO.getStocktakinNum() < 0) {
            throw new BadRequestException("盘点数量不能为负数");
        }

        tbProductStocktakinService.addStocktakin(productStocktakinDTO);
        return ResponseEntity.ok("成功");
    }

    @GetMapping
    @ApiOperation("获取盘点数据")
    public ResponseEntity<Object> getStocktakinList(TbProductStocktakinQueryCriteria productStocktakinQueryCriteria, Pageable pageable) {
        productStocktakinQueryCriteria.setSort("id,desc");
        if (productStocktakinQueryCriteria.getShopId() == null) {
            throw new BadRequestException("店铺id缺失");
        }
        return ResponseEntity.ok(tbProductStocktakinService.getPage(productStocktakinQueryCriteria, pageable));
    }
}
