package com.prolog.upcloud.base.wh.operate.controller;

import com.prolog.framework.bz.common.print.ReportData;
import com.prolog.framework.bz.core.collection.ValidList;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.upcloud.base.wh.operate.controller.common.BaseController;
import com.prolog.upcloud.base.wh.operate.enums.OmsMoveBillType;
import com.prolog.upcloud.base.wh.operate.enums.WmsMoveBillType;
import com.prolog.upcloud.base.wh.operate.mq.MqConsumer;
import com.prolog.upcloud.base.wh.operate.service.MoveService;
import com.prolog.upcloud.base.wh.operate.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "商品移库")
@Slf4j
@RestController
@RequestMapping("move")
public class MoveController extends BaseController {

    @Resource
    private MoveService moveService;
    @Resource
    private MqConsumer eisHandle;

    @UserLog(value = "新增移库单")
    @ApiOperation(value = "新增移库单")
    @PostMapping("v1/add")
    public RestMessage<Object> addMove(@Validated @RequestBody WmsMoveSaveVO saveVO) {
        moveService.saveMoveInfo(saveVO);
        return success();
    }

    @UserLog(value = "新增移库单并返回打印数据")
    @ApiOperation(value = "新增移库单并返回打印数据")
    @PostMapping("v1/addMoveBackPrint")
    public RestMessage<Object> addMoveBackPrint(@Validated @RequestBody WmsMoveSaveVO saveVO) {
        List<ReportData> reportData = moveService.saveMoveInfoBackPrint(saveVO);
        return success(reportData);
    }

    @UserLog(value = "审核移库单")
    @ApiOperation(value = "审核移库单")
    @PostMapping("v1/audit")
    public RestMessage<Object> auditMove(@Validated @RequestBody MoveAuditParamsVO auditMoveParamsVO) {
        moveService.auditMove(auditMoveParamsVO);
        return success();
    }

    @UserLog(value = "移库确认")
    @ApiOperation(value = "移库确认")
    @PostMapping("v1/check")
    public RestMessage<Object> confirmMove(@Validated @RequestBody MoveConfirmParamsVO moveConfirmParamsVO) {
        String message = moveService.confirmMove(moveConfirmParamsVO);
        return RestMessage.newInstance(true, message, null);
    }

    @UserLog(value = "移库下架确认")
    @ApiOperation(value = "移库下架确认")
    @PostMapping("moveDownConfirm")
    public RestMessage<Object> moveDownConfirm(@RequestBody MoveDListVO wmsMoveD) {
        moveService.moveDownConfirm(wmsMoveD);
        return RestMessage.newInstance(true, "操作成功！", null);
    }

    @UserLog(value = "移库上架确认")
    @ApiOperation(value = "移库上架确认")
    @PostMapping("moveUpConfirm")
    public RestMessage<Object> moveUpConfirm(@RequestBody MoveDListVO wmsMoveD) {
        moveService.moveUpConfirm(wmsMoveD);
        return RestMessage.newInstance(true, "操作成功！", null);
    }

    @UserLog(value = "校验货格号库存商品大类")
    @ApiOperation(value = "校验货格号库存商品大类")
    @PostMapping("checkGoodsCaseCodeStock")
    public RestMessage<Object> checkGoodsCaseCodeStock(@RequestBody MoveDListVO wmsMoveD) {
        moveService.checkGoodsCaseCodeStock(wmsMoveD);
        return RestMessage.newInstance(true, "操作成功！", null);
    }

    @UserLog(value = "移库确认")
    @ApiOperation(value = "移库确认")
    @PostMapping("v1/check/eis")
    public RestMessage<Object> confirmMoveEis(@Validated @RequestBody MoveConfirmParamsVO moveConfirmParamsVO) {
        String message = moveService.updateToW23(moveConfirmParamsVO);
        return RestMessage.newInstance(true, message, null);
    }

    @UserLog(value = "反审移库单")
    @ApiOperation(value = "反审移库单")
    @PostMapping("v1/reject")
    public RestMessage<Object> rejectMove(@Validated @RequestBody MoveLineIdParamsVO reject) {
        moveService.auditRejectMove(reject.getLineBillId());
        return success();
    }

    @UserLog(value = "移位")
    @ApiOperation(value = "移位")
    @PostMapping("/v1/locator")
    public RestMessage<Object> moveLocator(@Validated @RequestBody WmsMoveSaveVO saveVO) {
        moveService.saveMoveLocatorInfo(saveVO);
        return success();
    }

    @ApiOperation(value = "移库/移位列表")
    @GetMapping("v1/list")
    public RestMessage<Page<MoveDListVO>> getMoveDetailList(MoveQueryDVO queryVO) {
        startPage();
        Page<MoveDListVO> page = moveService.getMoveDetailList(queryVO);
        return success(page == null ? emptyPage() : page);
    }

    @ApiOperation(value = "移库下架任务查询")
    @PostMapping("moveDownShelvesTask")
    public RestMessage<List<MoveDListVO>> getMoveDownShelvesTask(@RequestBody MoveQueryDVO queryVO) {
        List<MoveDListVO> list = moveService.getMoveDownShelvesTask(queryVO);
        return success(list);
    }

    @ApiOperation(value = "移库上架任务查询")
    @PostMapping("moveUpShelvesTask")
    public RestMessage<List<MoveDListVO>> getMoveUpShelvesTask(@RequestBody MoveQueryDVO queryVO) {
        List<MoveDListVO> list = moveService.getMoveUpShelvesTask(queryVO);
        return success(list);
    }

    @ApiOperation(value = "ERP提取移库主单列表")
    @GetMapping("v1/erp-head-list")
    public RestMessage<Page<MoveErpHeadListVO>> getErpMoveHeadList(@RequestParam("warehouseId") String warehouseId) {
        startPage();
        return success(moveService.getErpMoveHeadList(warehouseId, OmsMoveBillType.WMSTOERP_MOVE));
    }

    @ApiOperation(value = "提取待检放行移库单主单列表")
    @GetMapping("v1/erp-check-head-list")
    public RestMessage<Page<MoveErpHeadListVO>> getErpCheckMoveHeadList(@RequestParam("warehouseId") String warehouseId) {
        startPage();
        return success(moveService.getErpMoveHeadList(warehouseId, OmsMoveBillType.WMSTOERP_WAITCHECK));
    }

    @ApiOperation(value = "ERP提取移库单明细列表")
    @GetMapping("v1/erp-list")
    public RestMessage<Page<MoveErpListVO>> getErpMoveList(@RequestParam("billId") String erpBillId, @RequestParam("billNo") String erpBillNo) {
        startPage();
        return success(moveService.getErpMoveList(erpBillId, erpBillNo, OmsMoveBillType.WMSTOERP_MOVE));
    }

    @ApiOperation(value = "待检放行移库单明细列表")
    @GetMapping("v1/erp-check-list")
    public RestMessage<Page<MoveErpListVO>> getErpCheckMoveList(@RequestParam("billId") String erpBillId, @RequestParam("billNo") String erpBillNo) {
        startPage();
        return success(moveService.getErpMoveList(erpBillId, erpBillNo, OmsMoveBillType.WMSTOERP_WAITCHECK));
    }

    @UserLog(value = "ERP移库单生成")
    @ApiOperation(value = "ERP移库单生成")
    @PostMapping("v1/erp-add")
    public RestMessage<Object> addErpMove(@Validated @RequestBody ValidList<MoveBillCreateVO> saveList) {
        moveService.createMoveBill(saveList, WmsMoveBillType.ERP);
        return success();
    }

    @UserLog(value = "待检放行移库单生成")
    @ApiOperation(value = "待检放行移库单生成")
    @PostMapping("v1/check-add")
    public RestMessage<Object> addCheckMove(@Validated @RequestBody ValidList<MoveBillCreateVO> saveList) {
        Object createMoveBill = moveService.createMoveBill(saveList, WmsMoveBillType.CHECK);
        return success(createMoveBill);
    }


    @UserLog(value = "GSP移库单生成")
    @ApiOperation(value = "GSP移库单生成")
    @PostMapping("v1/gsp-add")
    public RestMessage<Object> addGspMove(@Validated @RequestBody ValidList<MoveBillCreateVO> saveList) {
        moveService.createMoveBill(saveList, WmsMoveBillType.GSP);
        return success();
    }

    @UserLog(value = "GSP质量复核移库单生成")
    @ApiOperation(value = "GSP质量复核移库单生成")
    @PostMapping("v1/gsp-quality-add")
    public RestMessage<Object> addGspQualityMove(@Validated @RequestBody ValidList<MoveBillCreateVO> saveList) {
        moveService.createMoveBill(saveList, WmsMoveBillType.GSP_QUALITY);
        return success();
    }

    @UserLog(value = "货主转换移库单生成")
    @ApiOperation(value = "货主转换移库单生成")
    @PostMapping("v1/stock-change-add")
    public RestMessage<Object> addStockChangeMove(@Validated @RequestBody ValidList<MoveBillCreateVO> saveList) {
        moveService.createMoveBill(saveList, WmsMoveBillType.SC);
        return success();
    }

    @ApiOperation(value = "移库单主信息列表")
    @GetMapping("v1/head-list")
    public RestMessage<Page<MoveListVO>> getMoveList(MoveQueryVO queryVO) {
        startPage();
        Page<MoveListVO> page = moveService.getMoveList(queryVO);
        return success(page == null ? emptyPage() : page);
    }

    @ApiOperation(value = "移库单明细信息列表")
    @GetMapping("v1/detail-list")
    public RestMessage<Page<MoveVO>> getMoveDetailList(@RequestParam(name = "headerId") String headerId, @RequestParam(name = "status", required = false) String status) {
        startPage();
        Page<MoveVO> page = moveService.getMoveDetailList(headerId, status);
        return success(page == null ? emptyPage() : page);
    }

    @UserLog(value = "合托出库开票")
    @ApiOperation(value = "合托出库开票")
    @PostMapping("/v1/saveStereo")
    public RestMessage<Object> saveStereo(@Validated @RequestBody ValidList<MoveStereoAddVO> list) {
        moveService.saveStereo(list);
        return success();
    }

    @ApiOperation("移位打印")
    @PostMapping("/getMovePrintData")
    @UserLog()
    public RestMessage<Object> getMovePrintData(@Validated @RequestBody MovePrintDataVO vo) {
        return RestMessage.doSuccess(moveService.getMovePrintData(vo));
    }
}
