package com.hw.wms.controller;

import com.hw.common.core.constant.WmsConstants;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.wms.domain.*;
import com.hw.wms.domain.vo.*;
import com.hw.wms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Api Controller,提供给其他Module调用使用
 *
 * @author xins
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/api")
public class WmsApiController extends BaseController {
    @Autowired
    private IWmsBaseWarehouseService wmsBaseWarehouseService;

    @Autowired
    private IWmsRawOutstockService wmsRawOutstockService;

    @Autowired
    private IWmsRawInstockService wmsRawInstockService;

    @Autowired
    private IWmsRawReturnService wmsRawReturnService;

    @Autowired
    private IWmsStockTotalService wmsStockTotalService;

    @Autowired
    private IWmsProductInstockService wmsProductInstockService;


    /**
     * 根据工位查询查询仓库列表
     */
//    @RequiresPermissions("wms:rawoutstock:list")
    @GetMapping("/getWarehouses")
    public AjaxResult getWarehouses(WmsBaseWarehouse queryBaseWarehouse) {
        queryBaseWarehouse.setWarehouseInstockType(WmsConstants.WMS_WAREHOUSE_INSTOCK_TYPE_RAW);
        List<WmsBaseWarehouse> list = wmsBaseWarehouseService.selectWmsBaseWarehouseList(queryBaseWarehouse);
        return success(list);
    }


    /**
     * 获取物料库存信息
     */
//    @RequiresPermissions("mes:materialBom:list")
    @GetMapping("/getStockTotal")
    public AjaxResult getStockTotal(WmsStockTotal queryStockTotal) {
//        String ancestors = mesMaterialBom.getAncestors();
//        ancestors = "0," + ancestors;
//        mesMaterialBom.setAncestors(ancestors);
        List<WmsStockTotal> list = wmsStockTotalService.selectWmsStockTotalJoinList(queryStockTotal);
        return success(list);
    }


    /**
     * 申请领料
     */
//    @RequiresPermissions("wms:mobile:addrawinstock")
    @Log(title = "原材料出库记录", businessType = BusinessType.APPLY)
    @PostMapping(("/applyRawOutstock"))
    public AjaxResult applyRawOutstock(@Validated @RequestBody WmsRawOutstock wmsRawOutstock) {
        return toAjax(wmsRawOutstockService.applyRawOutstock(wmsRawOutstock));
    }


    /**
     * 申请退库
     */
    @Log(title = "原材料退库记录", businessType = BusinessType.APPLY)
    @PostMapping(("/applyRawReturn"))
    public AjaxResult applyRawReturn(@Validated @RequestBody List<WmsRawReturn> wmsRawReturns) {
        return toAjax(wmsRawReturnService.applyRawReturn(wmsRawReturns));
    }



    /**
     * 新增原材料入库记录（适合于四楼激光切割工位的板材入库）
     */
//    @RequiresPermissions("wms:mobile:addrawinstock")
    @Log(title = "原材料入库记录", businessType = BusinessType.INSERT)
    @PostMapping(("/addRawInstock"))
    public AjaxResult addRawInstock(@Validated @RequestBody WmsRawInstockVo wmsRawInstockVo) {
        return toAjax(wmsRawInstockService.insertWmsRawInstock(wmsRawInstockVo));
    }


    /**
     * 原材料直接出库（适合于四楼激光切割工位的板材出库）
     */
    @Log(title = "原材料出库记录", businessType = BusinessType.CONFIRM)
    @PostMapping(("/directRawOutstock"))
    public AjaxResult directRawOutstock(@RequestBody WmsRawOutstock wmsRawOutstock) {
        return toAjax(wmsRawOutstockService.directRawOutstock(wmsRawOutstock));
    }

    /**
     * 新增成品入库记录（适合于一楼生产完成品入虚拟库，没有库位）
     */
//    @RequiresPermissions("wms:mobile:addrawinstock")
    @Log(title = "成品入库记录", businessType = BusinessType.INSERT)
    @PostMapping(("/firstFloorProduceInstock"))
    public AjaxResult firstFloorProduceInstock(@Validated @RequestBody WmsProduceInstockVo wmsProduceInstockVo) {
        return toAjax(wmsProductInstockService.firstFloorProduceInstock(wmsProduceInstockVo));
    }

    /**
     * 新增成品入库记录（适合于四楼生产完成品入虚拟库，没有库位）
     */
//    @RequiresPermissions("wms:mobile:addrawinstock")
    @Log(title = "成品入库记录", businessType = BusinessType.INSERT)
    @PostMapping(("/fourthFloorProduceInstock"))
    public AjaxResult fourthFloorProduceInstock(@Validated @RequestBody WmsProduceInstockVo wmsProduceInstockVo) {
        return toAjax(wmsProductInstockService.fourthFloorProduceInstock(wmsProduceInstockVo));
    }

    /**
     * 申请返库
     */
    @Log(title = "原材料返库记录", businessType = BusinessType.APPLY)
    @PostMapping(("/applyRawBack"))
    public AjaxResult applyRawBack(@Validated @RequestBody WmsRawBackVo wmsRawBackVo) {
        return toAjax(wmsRawInstockService.applyRawBack(wmsRawBackVo));
    }


}
