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

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.prolog.cs.basic.warehouse.model.vo.WarehouseLocatorBasicInfo;
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.core.RestMsgHelper;
import com.prolog.upcloud.base.wh.operate.enums.ReplenishStatus;
import com.prolog.upcloud.base.wh.operate.resource.WarehouseClient;
import com.prolog.upcloud.base.wh.operate.service.AdjustService;
import com.prolog.upcloud.base.wh.operate.service.InvCheckService;
import com.prolog.upcloud.base.wh.operate.service.MoveService;
import com.prolog.upcloud.base.wh.operate.service.WmsReplenishService;
import com.prolog.upcloud.base.wh.operate.utils.StringUtils;
import com.prolog.upcloud.base.wh.operate.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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 javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Collections;

/**
 * @author wujunjie
 * @date 2021-04-20
 */
@RestController
@Api(tags = "PDA库内接口")
@RequestMapping("/pda")
@Slf4j
@Validated
public class PdaController extends BaseController {

    @Resource
    private MoveService moveService;

    @Resource
    private InvCheckService checkService;

    @Resource
    private AdjustService adjustService;

    @Resource
    private WmsReplenishService replenishService;

    @Resource
    private WarehouseClient warehouseClient;


    @UserLog(value = "PDA移位")
    @ApiOperation(value = "移位")
    @PostMapping("/v1/move/locator")
    public RestMessage<Object> moveLocator(@Valid @RequestBody WmsMovePdaSaveVO saveVO) {
        moveService.saveMoveLocatorInfoPda(saveVO);
        return success();
    }

    @ApiOperation(value = "移库列表")
    @GetMapping("v1/move")
    public RestMessage<Page<PdaMoveListVO>> getMoveDetails(@RequestParam(required = false) String headerId, @RequestParam(required = false) String code,@RequestParam(required = false) String containercode, @RequestParam String status) {
        startPage();
        Page<PdaMoveListVO> page = moveService.getMoveDetails(headerId, code, containercode,status);
        return success(page == null ? emptyPage() : page);
    }

    @ApiOperation(value = "移库明细")
    @GetMapping("v1/move/detail")
    public RestMessage<PdaMoveDetailVO> getMoveDetail(@RequestParam String moveLineId) {
        return success(moveService.getMoveDetail(moveLineId));
    }

    @UserLog(value = "PDA移库")
    @ApiOperation(value = "PDA移库")
    @PostMapping("v1/move")
    public RestMessage<Object> move(@Validated @RequestBody PdaMoveVO vo) {
        String msg=moveService.move(vo.getMoveLineId(), vo.getInOrOut());
        return RestMessage.success(msg,null);
    }

    @ApiOperation(value = "PDA获取盘点任务列表")
    @ApiImplicitParam(name = "warehouseId", value = "仓库编码", required = true, dataType = "String")
    @GetMapping("v1/check/list")
    public RestMessage<Page<InvCheckListVO>> getCheckList(String warehouseId) {
        startPage();
        Page<InvCheckListVO> page = checkService.getCheckList(warehouseId);
        return success(page == null ? emptyPage() : page);
    }

    @ApiOperation(value = "获取下一个盘点任务详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "checkHeaderId", value = "盘点任务单头编码", required = true, dataType = "String"), @ApiImplicitParam(name = "warehouseId", value = "仓库编码", required = true, dataType = "String"), @ApiImplicitParam(name = "locatorCode", value = "货位编码", required = false, dataType = "String"), @ApiImplicitParam(name = "containerCode", value = "容器编码", required = false, dataType = "String")})
    @GetMapping("v1/check/detail")
    public RestMessage<InvCheckDetailPdaVO> getCheckDetailList(String checkHeaderId, String warehouseId, String locatorCode, String containerCode) {
        String locatorId = "";
        if (StringUtils.isNotEmpty(locatorCode)) {
            WarehouseLocatorBasicInfo locatorBasicInfo = RestMsgHelper.unpack(warehouseClient.getLocationInfoByCode(warehouseId, locatorCode));
            Assert.notNull(locatorBasicInfo, "货位信息不存在，请检查");
            locatorId = locatorBasicInfo.getWarehouseLocatorId();
        }
        return RestMessage.querySuccess(checkService.getCurrentCheckDetail(checkHeaderId, warehouseId, locatorId, containerCode));
    }

    @UserLog("PDA盘点录入")
    @ApiOperation(value = "盘点录入")
    @PostMapping("/v1/check")
    public RestMessage<InvCheckDetailPdaVO> updateInvCheck(@RequestBody WmsInvCheckUpdateVO update) {
        checkService.updateInvCheck(Collections.singletonList(update));
        return RestMessage.querySuccess(checkService.getCurrentCheckDetail(update.getCheckHeaderId(), update.getWarehouseId(), null, null));
    }

    @UserLog("PDA新增损溢(pad,无计划盘点)")
    @ApiOperation(value = "新增损溢(pad,无计划盘点)")
    @PostMapping("v1/adjust/add")
    public RestMessage<?> saveAdjustByPad(@RequestBody WmsAdjustAddVO wmsAdjustAddVO) {
        adjustService.saveAdjustByPad(wmsAdjustAddVO);
        return RestMessage.success("操作成功", null);
    }

    @ApiOperation("PDA补货上架查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "warehouseId", value = "仓库编码", required = true, dataType = "String"), @ApiImplicitParam(name = "detailId", value = "补货子单编码", dataType = "String")})
    @GetMapping("v1/list")
    public RestMessage<Page<ReplenishListVO>> getReplenishList(ReplenishPdaQueryVO queryVO) {
        startPage();
        ReplenishQueryVO replenishQueryVO = new ReplenishQueryVO();
        replenishQueryVO.setWarehouseId(queryVO.getWarehouseId());
        replenishQueryVO.setDetailId(StrUtil.isBlank(queryVO.getDetailId())? queryVO.getCondition() : queryVO.getDetailId());
        replenishQueryVO.setStatus(ReplenishStatus.OUT.getStatus());
        replenishQueryVO.setBusinessType(queryVO.getBusinessType());
        Page<ReplenishListVO> page = replenishService.getReplenishList(replenishQueryVO);
        return success(page == null ? emptyPage() : page);
    }

    @ApiOperation("PDA补货信息明细")
    @ApiImplicitParams({@ApiImplicitParam(name = "detailId", value = "补货子单编码", dataType = "String")})
    @GetMapping("v1/detail")
    public RestMessage<ReplenishPdaDetailVO> getReplenishDetail(@Valid @NotBlank(message = "明细ID不能为空") String detailId) {
        return success(replenishService.getReplenishDetail(detailId));
    }

    @ApiOperation(value = "补货下架确认列表查询")
    @GetMapping("/v1/getReplenishUnshelveList")
    public RestMessage<Page<ReplenishUnshelveVO>> getReplenishUnshelveList(
            @Valid @NotBlank(message = "warehouseId不能为空") String warehouseId,
            String condition,
            String bldgId,
            String dcId,
            Integer pageNum,
            Integer pageSize,
            @RequestParam(value = "businessType", required = false) String businessType
    ) {
        Page<ReplenishUnshelveVO> page = replenishService.getReplenishUnshelveList(
                warehouseId,
                condition,
                bldgId,
                dcId,
                pageNum,
                pageSize,
                businessType
        );
        return RestMessage.success("操作成功", page);
    }

    @UserLog("PDA上架/下架确认")
    @ApiOperation("PDA上架/下架确认")
    @PostMapping("/v1/confirm")
    public RestMessage<Object> confirmReplenish(@Validated @RequestBody MoveConfirmParamsVO vo) {
        replenishService.confirmReplenish(vo);
        return success();
    }

    @ApiOperation("立库合托作业查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "warehouseId", value = "仓库编码", required = true, dataType = "String"), @ApiImplicitParam(name = "containerCode", value = "容器编号", dataType = "String")})
    @GetMapping("/v1/getStereoList")
    public RestMessage<Page<StereoDtlVO>> getStereoList(@Valid @NotBlank(message = "warehouseId不能为空") String warehouseId, @Valid @NotBlank(message = "请扫描容器号") String containerCode) {
        startPage();
        return success(moveService.getStereoListByContainerCode(warehouseId, containerCode));
    }

    @ApiOperation("立库合托作业确认")
    @PostMapping("/v1/confirmStereo")
    public RestMessage<Object> confirmStereo(@Validated @RequestBody ConfirmStereoVO vo) {
        String itemName = moveService.confirmStereo(vo);
        return RestMessage.success(String.format("商品【%s】已合托完成", itemName), null);
    }

    @ApiOperation("立库合托作业结合")
    @PostMapping("/v1/finishStereo")
    public RestMessage<Object> finishStereo(@Validated @RequestBody FinishStereoVO vo) {
        moveService.finishStereo(vo);
        return success();
    }

}
