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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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 IWmsMoveMergeService wmsMoveMergeService;

    /**
     * 新增原材料入库记录
     */
//    @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 AjaxResult getToAuditedRawOutstocks() {
        startPage();
        WmsRawOutstock wmsRawOutstock = new WmsRawOutstock();
        wmsRawOutstock.setAuditStatus(WmsConstants.WMS_AUDIT_STATUS_TOAUDIT);
        List<WmsRawOutstock> list = wmsRawOutstockService.selectWmsRawOutstockList(wmsRawOutstock);
        return success(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")
    @PostMapping("/getAuditPassRawOutstockDetails")
    public AjaxResult getAuditPassRawOutstockDetails(@Validated @RequestBody WmsRawOutstockDetailVo wmsRawOutstockDetailVo) {
        startPage();
        WmsRawOutstockDetail wmsRawOutstockDetail = new WmsRawOutstockDetail();
        wmsRawOutstockDetail.setApplyBy(wmsRawOutstockDetailVo.getApplyBy());
        List<WmsRawOutstockDetail> list = wmsRawOutstockService.selectAuditPassRawOutstockDetails(wmsRawOutstockDetail);
        return success(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 AjaxResult getToAuditedRawReturns() {
        startPage();
        WmsRawReturn wmsRawReturn = new WmsRawReturn();
        wmsRawReturn.setAuditStatus(WmsConstants.WMS_AUDIT_STATUS_TOAUDIT);
        List<WmsRawReturn> list = wmsRawReturnService.selectWmsRawReturnJoinList(wmsRawReturn);
        return success(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 AjaxResult getAuditPassRawReturnDetails(@Validated @RequestBody WmsRawOutstockDetailVo wmsRawOutstockDetailVo) {
        startPage();
        WmsRawReturnDetail wmsRawReturnDetail = new WmsRawReturnDetail();
        wmsRawReturnDetail.setApplyBy(wmsRawOutstockDetailVo.getApplyBy());
        List<WmsRawReturnDetail> list = wmsRawReturnService.selectAuditPassRawReturnDetails(wmsRawReturnDetail);
        return success(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));
    }

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

}
