package com.hw.wms.controller;

import com.hw.common.core.constant.WmsConstants;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.utils.uuid.Seq;
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.common.security.annotation.RequiresPermissions;
import com.hw.wms.domain.*;
import com.hw.wms.domain.vo.*;
import com.hw.wms.service.*;
import javafx.scene.control.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 移动端手持Controller
 *
 * @author xs
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/wms/mobile")
public class WmsMobileController extends BaseController {
    @Autowired
    private IWmsRawInstockService wmsRawInstockService;

    @Autowired
    private IWmsRawOutstockService wmsRawOutstockService;

    @Autowired
    private IWmsRawReturnService wmsRawReturnService;

    @Autowired
    private IWmsProductInstockService wmsProductInstockService;

    @Autowired
    private IWmsProductOutstockService wmsProductOutstockService;

    @Autowired
    private IWmsProductOutstockDetailService wmsProductOutstockDetailService;

    @Autowired
    private IWmsMoveMergeService wmsMoveMergeService;

    @Autowired
    private IWmsTransferService wmsTransferService;

    @Autowired
    private IWmsBaseWarehouseService wmsBaseWarehouseService;

    @Autowired
    private IWmsProductStockService wmsProductStockService;

    /**
     * 新增原材料入库记录
     */
//    @RequiresPermissions("wms:mobile:addrawinstock")
    @Log(title = "原材料入库记录", businessType = BusinessType.INSERT)
    @PostMapping(("/addRawInstock"))
    public AjaxResult addRawInstock(@Validated @RequestBody WmsRawInstockVo wmsRawInstockVo) {
        return toAjax(wmsRawInstockService.insertWmsRawInstock(wmsRawInstockVo));
    }


    /**
     * 查询原材料待审核状态的出库记录列表
     */
//    @RequiresPermissions("wms:rawoutstock:list")
    @GetMapping("/getToAuditedRawOutstocks")
    public TableDataInfo getToAuditedRawOutstocks() {
        startPage();
        WmsRawOutstock wmsRawOutstock = new WmsRawOutstock();
        wmsRawOutstock.setAuditStatus(WmsConstants.WMS_AUDIT_STATUS_TOAUDIT);
        List<WmsRawOutstock> list = wmsRawOutstockService.selectWmsRawOutstockList(wmsRawOutstock);
        return getDataTable(list);
    }

    /**
     * 获取原材料出库记录详细信息
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getRawOutstockByOutstockId/{rawOutstockId}")
    public AjaxResult getRawOutstockByOutstockId(@PathVariable("rawOutstockId") Long rawOutstockId) {
        return success(wmsRawOutstockService.selectWmsRawOutstockByRawOutstockId(rawOutstockId));
    }

    /**
     * 审核原材料出库记录
     */
    @Log(title = "原材料出库记录", businessType = BusinessType.AUDIT)
    @PostMapping(("/auditRawOutstock"))
    public AjaxResult auditRawOutstock(@Validated @RequestBody WmsRawOutstockAuditVo wmsRawOutstockAuditVo) {
        return success(wmsRawOutstockService.auditWmsRawOutstock(wmsRawOutstockAuditVo));
    }

    /**
     * 查询原材料审核通过的出库记录明细列表
     */
//    @RequiresPermissions("wms:rawoutstock:list")
    @GetMapping("/getAuditPassRawOutstockDetails")
    public TableDataInfo getAuditPassRawOutstockDetails(WmsRawOutstockDetail wmsRawOutstockDetail) {
        startPage();
//        WmsRawOutstockDetail wmsRawOutstockDetail = new WmsRawOutstockDetail();
//        wmsRawOutstockDetail.setApplyBy(request.getParameter("applyBy"));
        List<WmsRawOutstockDetail> list = wmsRawOutstockService.selectAuditPassRawOutstockDetails(wmsRawOutstockDetail);
        return getDataTable(list);
    }

    /**
     * 原材料出库确认
     */
    @Log(title = "原材料出库记录", businessType = BusinessType.CONFIRM)
    @PostMapping(("/confirmRawOutstock"))
    public AjaxResult confirmRawOutstock(@Validated @RequestBody WmsRawOutstockConfirmVo wmsRawOutstockConfirmVo) {
        return toAjax(wmsRawOutstockService.confirmWmsRawOutstock(wmsRawOutstockConfirmVo));
    }


    /**
     * 查询原材料待审核状态的退库记录列表
     */
//    @RequiresPermissions("wms:rawoutstock:list")
    @GetMapping("/getToAuditedRawReturns")
    public TableDataInfo getToAuditedRawReturns() {
        startPage();
        WmsRawReturn wmsRawReturn = new WmsRawReturn();
        wmsRawReturn.setAuditStatus(WmsConstants.WMS_AUDIT_STATUS_TOAUDIT);
        List<WmsRawReturn> list = wmsRawReturnService.selectWmsRawReturnJoinList(wmsRawReturn);
        return getDataTable(list);
    }

    /**
     * 获取原材料退库记录详细信息,审核使用
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getRawReturnByReturnId/{returnId}")
    public AjaxResult getRawReturnByReturnId(@PathVariable("returnId") Long returnId) {
        return success(wmsRawReturnService.selectWmsRawReturnByRawReturnId(returnId));
    }

    /**
     * 审核原材料出库记录
     */
    @Log(title = "原材料退库记录", businessType = BusinessType.AUDIT)
    @PostMapping(("/auditRawReturn"))
    public AjaxResult auditRawReturn(@Validated @RequestBody WmsRawReturnAuditVo wmsRawReturnAuditVo) {
        return toAjax(wmsRawReturnService.auditWmsRawReturn(wmsRawReturnAuditVo));
    }

    /**
     * 查询原材料审核通过的退库记录明细列表
     */
//    @RequiresPermissions("wms:rawoutstock:list")
    @PostMapping("/getAuditPassRawReturnDetails")
    public TableDataInfo getAuditPassRawReturnDetails(WmsRawReturnDetail wmsRawReturnDetail) {
        startPage();
//        WmsRawReturnDetail wmsRawReturnDetail = new WmsRawReturnDetail();
//        wmsRawReturnDetail.setApplyBy(request.getParameter("applyBy"));
        List<WmsRawReturnDetail> list = wmsRawReturnService.selectAuditPassRawReturnDetails(wmsRawReturnDetail);
        return getDataTable(list);
    }

    /**
     * 获取原材料退库记录详细信息,退库确认使用
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getRawReturnByReturnId4Confirm/{returnId}")
    public AjaxResult getRawReturnByReturnId4Confirm(@PathVariable("returnId") Long returnId) {
        return success(wmsRawReturnService.selectWmsRawReturnByRawReturnId4Confirm(returnId));
    }

    /**
     * 原材料退库确认
     */
    @Log(title = "原材料退库记录", businessType = BusinessType.CONFIRM)
    @PostMapping(("/confirmRawReturn"))
    public AjaxResult confirmRawReturn(@Validated @RequestBody WmsRawReturnConfirmVo wmsRawReturnConfirmVo) {
        return toAjax(wmsRawReturnService.confirmWmsRawReturn(wmsRawReturnConfirmVo));
    }


    /**
     * 人工成品入库
     */
    @Log(title = "成品入库记录", businessType = BusinessType.INSERT)
    @PostMapping(("/addProductInstock"))
    public AjaxResult addProductInstock(@Validated @RequestBody WmsProductInstockVo wmsProductInstockVo) {
        return toAjax(wmsProductInstockService.insertWmsProductInstock(wmsProductInstockVo));
    }


    /**
     * 获取半成品仓库列表
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getSemiWarehouseList")
    public AjaxResult getSemiWarehouseList() {
        WmsBaseWarehouse queryBaseWarehouse = new WmsBaseWarehouse();
        queryBaseWarehouse.setWarehouseInstockType(WmsConstants.WMS_WAREHOUSE_INSTOCK_TYPE_SEMI);
        return success(wmsBaseWarehouseService.selectWmsBaseWarehouseList(queryBaseWarehouse));
    }

    /**
     * 获取成品仓库列表
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getProductWarehouseList")
    public AjaxResult getProductWarehouseList() {
        WmsBaseWarehouse queryBaseWarehouse = new WmsBaseWarehouse();
        queryBaseWarehouse.setWarehouseInstockType(WmsConstants.WMS_WAREHOUSE_INSTOCK_TYPE_PRODUCT);
        return success(wmsBaseWarehouseService.selectWmsBaseWarehouseList(queryBaseWarehouse));
    }


    /**
     * 根据仓库ID获取成品库存信息
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getProductStocksByWarehouseId")
    public TableDataInfo getProductStocksByWarehouseId( WmsProductStock queryProductStock) {
        startPage();
//        WmsProductStock queryProductStock = new WmsProductStock();
//        queryProductStock.setWarehouseId(Long.valueOf(request.getParameter("warehouseId")));
        List list = wmsProductStockService.selectWmsProductStockJoinList(queryProductStock);
        return getDataTable(list);
    }


    /**
     * 人工申请半成品出半成品库入成品库
     */
    @Log(title = "转库", businessType = BusinessType.TRANSFER)
    @PostMapping(("/applyProductTransfer"))
    public AjaxResult applyProductTransfer(@Validated @RequestBody List<WmsTransfer> wmsTransfers) {
        return toAjax(wmsTransferService.applyTransfers(wmsTransfers));
    }


    /**
     * 获取待转库列表
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getTransfers")
    public TableDataInfo getTransfers() {
        startPage();
        WmsTransfer queryTransfer = new WmsTransfer();
        queryTransfer.setExecuteStatus(WmsConstants.WMS_EXECUTE_STATUS_NOT_FINISH);
        List list = wmsTransferService.selectWmsTransferJoinList(queryTransfer);
        return getDataTable(list);
    }


    /**
     * 转库出库，五楼半成品出半成品库
     */
    @Log(title = "转库", businessType = BusinessType.INSERT)
    @PostMapping(("/transferOutstock"))
    public AjaxResult transferOutstock(@RequestBody WmsTransferDetail wmsTransferDetail) {
        return toAjax(wmsTransferService.transferOutstock(wmsTransferDetail));
    }


    /**
     * 转库入库，五楼半成品入成品库
     */
    @Log(title = "转库", businessType = BusinessType.INSERT)
    @PostMapping(("/transferInstock"))
    public AjaxResult transferInstock(@RequestBody WmsTransferDetail wmsTransferDetail) {
        return toAjax(wmsTransferService.transferInstock(wmsTransferDetail));
    }


    /**
     * 根据销售订单编号获取成品库存列表
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getProductStocksBySalesorderCode")
    public TableDataInfo getProductStocksBySalesorderCode(WmsProductStock queryProductStock) {
        startPage();
//        WmsProductStock queryProductStock = new WmsProductStock();
//        queryProductStock.setSaleorderCode(request.getParameter("salesorderCode"));
//        queryProductStock.setStockType(WmsConstants.PRODUCT_STOCK_STOCK_TYPE_PRODUCT);
        List list = wmsProductStockService.selectWmsProductStocksBySaleorder(queryProductStock);
        return getDataTable(list);
    }

    /**
     * 成品出库申请（包括人工合agv的出库申请）
     */
    @Log(title = "成品出库记录", businessType = BusinessType.INSERT)
    @PostMapping(("/applyProductOutstock"))
    public AjaxResult applyProductOutstock(@RequestBody WmsProductOutstock wmsProductOutstock) {
        return toAjax(wmsProductOutstockService.insertWmsProductOutstock(wmsProductOutstock));
    }


    /**
     * 获取人工成品出库任务列表
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getProductOutStocks")
    public TableDataInfo getProductOutStocks(WmsProductOutstock queryProductOutstock) {
        startPage();
        queryProductOutstock.setOperationType(WmsConstants.WMS_OPERATION_TYPE_MANUAL);
        queryProductOutstock.setExecuteStatus(WmsConstants.WMS_EXECUTE_STATUS_NOT_FINISH);
        queryProductOutstock.setProductType(WmsConstants.WMS_PRODUCT_OUTSTOCK_PRODUCT_TYPE_PRODUCT);
        List list = wmsProductOutstockService.selectWmsProductOutstockJoinList(queryProductOutstock);
        return getDataTable(list);
    }

    /**
     * 获取人工成品出库任务明细,参数是productOutstockId
     */
//    @RequiresPermissions("wms:rawoutstock:query")
    @GetMapping(value = "/getProductOutStockWithDetails")
    public AjaxResult getProductOutStockWithDetails(WmsProductOutstock queryProductOutstock) {
        AjaxResult ajax = AjaxResult.success();
        WmsProductOutstock productOutstock = wmsProductOutstockService.
                selectWmsProductOutstockJoinByOutstockId(queryProductOutstock.getProductOutstockId());

        startPage();
        WmsProductOutstockDetail queryProductOutstockDetail = new WmsProductOutstockDetail();
        List productOutstockDetails = wmsProductOutstockDetailService.selectWmsProductOutstockDetailList(queryProductOutstockDetail);

        ajax.put("productOutstock",productOutstock);
        ajax.put("productOutstockDetails",productOutstockDetails);
        return ajax;
    }

    /**
     * 人工成品出库确认
     */
    @Log(title = "成品出库明细", businessType = BusinessType.INSERT)
    @PostMapping(("/confirmProductOutstockDetail"))
    public AjaxResult confirmProductOutstockDetail(@RequestBody WmsProductOutstockDetail wmsProductOutstockDetail) {
        return toAjax(wmsProductOutstockDetailService.insertWmsProductOutstockDetail(wmsProductOutstockDetail));
    }













    /**
     * 人工移库
     */
    @Log(title = "人工移库", businessType = BusinessType.INSERT)
    @PostMapping(("/moveRaw"))
    public AjaxResult moveRaw(@Validated @RequestBody WmsMoveMergeVo wmsMoveMergeVo) {
        return toAjax(wmsMoveMergeService.manualMove(wmsMoveMergeVo, WmsConstants.WMS_MOVEMERGE_TYPE_MOVE));
    }

}
