package com.hys.app.controller.erp.use;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.database.BaseQueryParam;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dos.LockedStockDetailDO;
import com.hys.app.model.erp.dto.*;
import com.hys.app.model.erp.enums.WarehouseOutStatusEnum;
import com.hys.app.model.erp.vo.GoodsVO;
import com.hys.app.model.erp.vo.ProductStockVO;
import com.hys.app.model.erp.vo.ProductVO;
import com.hys.app.model.erp.vo.WarehouseOutVO;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.erp.WarehouseOutManager;
import com.hys.app.service.kuaidiniao.entity.PreOrderModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 出库单相关API
 *
 * @author 张崧
 * @since 2023-12-07 16:50:20
 */
@RestController
@RequestMapping("/admin/erp/warehouseOut")
@Api(tags = "出库单相关API")
@Validated
public class WarehouseOutManagerController {

    @Autowired
    private WarehouseOutManager warehouseOutManager;

    @ApiOperation(value = "分页列表")
    @GetMapping
    public WebPage<WarehouseOutVO> list(WarehouseOutQueryParams queryParams) {
        return warehouseOutManager.list(queryParams);
    }

    @ApiOperation(value = "添加")
    @PostMapping
    @Log(client = LogClient.admin, detail = "创建出库单")
    public Long add(@RequestBody @Valid WarehouseOutDTO warehouseOutDTO) {
        return warehouseOutManager.add(warehouseOutDTO);
    }

    @ApiOperation(value = "编辑")
    @PutMapping("/{id}")
    @Log(client = LogClient.admin, detail = "编辑id为[${id}]出库单")
    public void edit(@PathVariable Long id, @RequestBody @Valid WarehouseOutDTO warehouseOutDTO) {
        warehouseOutDTO.setId(id);
        warehouseOutManager.edit(warehouseOutDTO);
    }

    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public WarehouseOutVO getDetail(@PathVariable Long id) {
        return warehouseOutManager.getDetail(id);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @Log(client = LogClient.admin, detail = "删除ID为[${id}]的出库单")
    public void delete(@PathVariable Long id) {
        warehouseOutManager.delete(id);
    }

    @ApiOperation(value = "导出出库单统计列表")
    @GetMapping("/export")
    public void export(HttpServletResponse response, WarehouseOutStatisticsParam params) {
        this.warehouseOutManager.export(response, params);
    }

    @ApiOperation(value = "根据仓库找到所有sku库存大于0的商品")
    @GetMapping("/getGoodByWarehouse/warehouseIdAndOrderType/{orderType}/{warehouseIds}")
    @Log(client = LogClient.admin, detail = "根据仓库找到所有sku库存大于0的商品")
    public IPage<ProductStockVO> getGoodByWarehouse(BaseQueryParam queryParam,
                                                    @PathVariable(value = "orderType") String orderType,
                                                    @PathVariable(value = "warehouseIds") String warehouseIds) {
        String[] split = warehouseIds.split(",");
        List<Long> list = new ArrayList<>();
        for (String s : split) {
            list.add(Long.valueOf(s));
        }
        return warehouseOutManager.getGoodByWarehouse(null, queryParam, orderType, list,queryParam.getKeyWord());
    }
    @ApiOperation(value = "根据仓库找到所有sku库存大于0的商品")
    @GetMapping("/getGoodByWarehouse/{warehouseId}")
    public IPage<ProductStockVO> getGoodByWarehouse(@PathVariable(value = "warehouseId") Long warehouseId, BaseQueryParam queryParam) {
        return warehouseOutManager.getGoodByWarehouse(warehouseId, queryParam, null, null,queryParam.getKeyWord());
    }

    @ApiOperation(value = "找到这个仓库所有的商品")
    @GetMapping("/getGoodByWarehouseInventory/{warehouseId}")
    public IPage<ProductStockVO> getGoodByWarehouseInventory(@PathVariable(value = "warehouseId") Long warehouseId, BaseQueryParam queryParam) {
        return warehouseOutManager.getGoodByWarehouseInventory(warehouseId, queryParam,queryParam.getKeyWord());
    }
    @ApiOperation(value = "获取冻结库存")
    @GetMapping("/getLockedStock")
    public IPage<LockedStockDetailDO> getLockedStock(LockedStockParams queryParam) {
        return warehouseOutManager.getLockedStock(queryParam);
    }
}

