package cn.ysk.cashier.controller.product;

import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.service.product.StockService;
import cn.ysk.cashier.utils.JSONUtil;
import cn.ysk.cashier.vo.StockUpdateValueVO;
import cn.ysk.cashier.vo.StockUpdateWarnLineVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "库存管理")
@RequestMapping("/api/stock")
@Slf4j
public class StockController {

    private final StockService stockService;


    @PostMapping("updateProductData")
    public ResponseEntity<Object> updateProductData(@Validated @RequestBody List<StockUpdateValueVO> updateValueVO) {
        log.info(JSONUtil.toJSONString(updateValueVO));
        stockService.updateProductStatus(updateValueVO);
        return new ResponseEntity<>("修改成功", HttpStatus.OK);
    }

    @PutMapping("warnLine")
    @ApiOperation("修改商品警告线")
    @Log("修改商品警告值为:#stockUpdateWarnLineVO.warnLine")
    public ResponseEntity<Object> updateProductWarnLine(@RequestBody StockUpdateWarnLineVO stockUpdateWarnLineVO) {
        if (StrUtil.isBlank(stockUpdateWarnLineVO.getShopId()) || stockUpdateWarnLineVO.getWarnLine() == null || stockUpdateWarnLineVO.getWarnLine() <= 0) {
            throw new BadRequestException("参数有误");
        }
        stockService.updateProductWarnLine(stockUpdateWarnLineVO);
        return new ResponseEntity<>("修改成功", HttpStatus.OK);
    }

}
