package com.wansenai.api.stock;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wansenai.entities.stock.BasStockAccount;
import com.wansenai.service.stock.BasStockAccountService;
import com.wansenai.utils.response.Response;
import com.wansenai.vo.stock.StockAccountVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>库存账接口</p>
 * @author clunt
 */
@Tag(name = "库存账接口")
@RestController
@RequestMapping("basStockAccount")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BasStockAccountController {

    private final BasStockAccountService stockAccountService;

    @Operation(summary = "获取库存列表")
    @GetMapping("/list")
    public Response<IPage<StockAccountVO>> getStockList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "仓库ID") @RequestParam(required = false) Long warehouseId,
            @Parameter(description = "商品ID") @RequestParam(required = false) Long goodsId) {
        
        Page<BasStockAccount> pageRequest = new Page<>(page, pageSize);
        return Response.responseData(stockAccountService.getStockVOPage(pageRequest, warehouseId, goodsId));
    }

    @Operation(summary = "获取库存详情")
    @GetMapping("/{id}")
    public Response<StockAccountVO> getStockById(@Parameter(description = "库存ID") @PathVariable Long id) {
        return Response.responseData(stockAccountService.getStockDetailById(id));
    }

    @Operation(summary = "查询库存信息")
    @GetMapping("/info")
    public Response<BasStockAccount> getStockInfo(
            @Parameter(description = "仓库ID") @RequestParam Long warehouseId,
            @Parameter(description = "商品ID") @RequestParam Long goodsId) {
        return Response.responseData(stockAccountService.getStockInfo(warehouseId, goodsId));
    }

    @Operation(summary = "添加库存信息")
    @PostMapping("/add")
    public Response<Boolean> addStock(@RequestBody BasStockAccount stock) {
        return Response.responseData(stockAccountService.addStock(stock));
    }

    @Operation(summary = "批量添加库存")
    @PostMapping("/batch")
    public Response<Boolean> batchAddStock(@RequestBody List<BasStockAccount> stockList) {
        return Response.responseData(stockAccountService.batchAddStock(stockList));
    }

    @Operation(summary = "更新库存信息")
    @PutMapping("/update")
    public Response<Boolean> updateStock(@RequestBody BasStockAccount stock) {
        return Response.responseData(stockAccountService.updateStock(stock));
    }

    @Operation(summary = "更新库存数量")
    @PutMapping("/qty/{id}")
    public Response<Boolean> updateStockQty(
            @Parameter(description = "库存ID") @PathVariable Long id,
            @Parameter(description = "数量") @RequestParam BigDecimal qty) {
        return Response.responseData(stockAccountService.updateStockQty(id, qty));
    }

    @Operation(summary = "更新锁定数量")
    @PutMapping("/lock/{id}")
    public Response<Boolean> updateLockQty(
            @Parameter(description = "库存ID") @PathVariable Long id,
            @Parameter(description = "锁定数量") @RequestParam BigDecimal qtyLock) {
        return Response.responseData(stockAccountService.updateLockQty(id, qtyLock));
    }

    @Operation(summary = "更新在途数量")
    @PutMapping("/tran/{id}")
    public Response<Boolean> updateTranQty(
            @Parameter(description = "库存ID") @PathVariable Long id,
            @Parameter(description = "在途数量") @RequestParam BigDecimal qtyTran) {
        return Response.responseData(stockAccountService.updateTranQty(id, qtyTran));
    }
}
