/**
 * Project Name:eis-jxyj-shelf-work
 * File Name:BackServiceImpl.java
 * Package Name:com.prolog.eis.service.wms.impl
 * Date:2022年11月8日下午4:12:35
 * Copyright (c) 2022, bluemobi All Rights Reserved.
 *
*/

package com.prolog.eis.service.shelves.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.prolog.eis.constants.wms.WmsConstants;
import com.prolog.eis.dao.BizPropertyMapper;
import com.prolog.eis.dao.shelves.BizInboundTaskDetailMapper;
import com.prolog.eis.dao.shelves.BizInboundTaskMapper;
import com.prolog.eis.dto.inbound.WmsInBackReqDto;
import com.prolog.eis.dto.result.WmsResult;
import com.prolog.eis.enums.base.BaseStatusEnum;
import com.prolog.eis.model.inbound.BizInboundTask;
import com.prolog.eis.model.inbound.BizInboundTaskDetail;
import com.prolog.eis.service.shelves.BackService;
import com.prolog.eis.service.shelves.TaskToHisService;
import com.prolog.eis.service.shelves.WmsInTaskRepeatBackService;
import com.prolog.framework.utils.MapUtils;
import com.prolog.fx.inf.core.Exception.RetryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Description: <br/>
 * Date: 2022年11月8日 下午4:12:35 <br/>
 * 
 * @version
 * @see
 */
@Service
@Slf4j
public class BackServiceImpl implements BackService {

    @Resource
    private TaskToHisService taskToHisService;


    @Resource
    BizInboundTaskMapper bizInboundTaskMapper;

    @Resource
    BizInboundTaskDetailMapper bizInboundTaskDetailMapper;

    @Resource
    WmsInTaskRepeatBackService wmsInTaskRepeatBackService;

    @Resource
    BizPropertyMapper bizPropertyMapper;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void wmsInCallBack() throws Exception {
        List<BizInboundTask> inbounds = bizInboundTaskMapper.findByMap(MapUtils.put("status", BaseStatusEnum.STATUS_FINISH.getCode().toString())
                .put("wmsLocationCode", WmsConstants.AGVHJ01).getMap(),BizInboundTask.class);
        if(CollectionUtil.isEmpty(inbounds)){
            log.info("入库任务回告为空！");
            return;
        }
            List<WmsInBackReqDto> wmsInBackReqDtos = new ArrayList<>();
            for (BizInboundTask inBoundTask:inbounds){
                //添加历史
                /*WmsInBackReqDto wmsInReqDto = new WmsInBackReqDto();
                wmsInReqDto.setMsgId(inBoundTask.getUpperSystemTaskId());
                wmsInReqDto.setTaskId(inBoundTask.getBillNo());
                wmsInReqDto.setContainerType(inBoundTask.getContainerType());
                wmsInReqDto.setContainerNo(inbounds.get(0).getContainerNo());
                wmsInReqDto.setWarehouseId(String.valueOf(inBoundTask.getWmsWarehouseId()));
                wmsInReqDto.setWmsLocationCode(inBoundTask.getWmsLocationCode());
                wmsInReqDto.setWorkType(inBoundTask.getWorkType());
                wmsInReqDto.setBusinessType(inBoundTask.getBusinessType());
                List<BizInboundTaskDetail> bizInboundTaskDetails = bizInboundTaskDetailMapper.findByMap(MapUtils.put("inboundId", inBoundTask.getId()).getMap(), BizInboundTaskDetail.class);
                wmsInReqDto.setContainerNo(inBoundTask.getContainerNo());
                if(CollectionUtil.isEmpty(bizInboundTaskDetails)){
                    throw new PrologException("容器【"+inBoundTask.getContainerNo()+"】没有任务明细");
                }
                for (BizInboundTaskDetail bizInboundTaskDetail : bizInboundTaskDetails){
                  *//*  BizProperty bizProperty = bizPropertyMapper.findByMap(MapUtils.put("id",bizInboundTaskDetail.getBizPropertyId()).getMap(), BizProperty.class)
                            .stream().findFirst().orElseThrow(() -> new PrologException("未查询到容器【" + inBoundTask.getContainerNo() + "】的品批信息"));*//*
                    wmsInReqDto.setItemId(bizInboundTaskDetail.getItemId());
                    wmsInReqDto.setPackId(bizInboundTaskDetail.getPackId());
                    wmsInReqDto.setLotId(bizInboundTaskDetail.getLotId());
                    wmsInReqDto.setOwnerId(bizInboundTaskDetail.getOwnerId());
                    wmsInReqDto.setQty(bizInboundTaskDetail.getQty());
                    wmsInReqDto.setSeqNo(bizInboundTaskDetail.getSeqNo());
                }
                wmsInBackReqDtos.add(wmsInReqDto);*/

                WmsInBackReqDto wmsInReqDto = new WmsInBackReqDto();
                wmsInReqDto.setWarehouseId(inBoundTask.getWmsWarehouseId());
                wmsInReqDto.setEnterpriseId(inBoundTask.getEnterpriseId());
                wmsInReqDto.setTaskId(inBoundTask.getBillNo());
                wmsInReqDto.setWmsLocationCode(inBoundTask.getWmsLocationCode());
                wmsInReqDto.setMsgId(inBoundTask.getUpperSystemTaskId());
                wmsInReqDto.setWorkType(inBoundTask.getWorkType());
                wmsInReqDto.setBusinessType(inBoundTask.getBusinessType());
                wmsInReqDto.setContainerNo(inBoundTask.getContainerNo());
                try {
                    if (!StrUtil.equals(inBoundTask.getWorkType(), BizInboundTask.SUPPLY_WORK_TYPE)) {
                        WmsResult wmsResult = wmsInTaskRepeatBackService.execute(wmsInReqDto);
                    }
                    taskToHisService.toInHistory(inBoundTask);
                    //删除入库任务
                    bizInboundTaskMapper.deleteById(inBoundTask.getId(), BizInboundTask.class);
                    bizInboundTaskDetailMapper.deleteByMap(MapUtils.put("inboundId", inBoundTask.getId()).getMap(), BizInboundTaskDetail.class);
                } catch (RetryException e) {
                    log.warn("入库完成回告wms失败，进入重试：" + e.getMessage());
                }
            }

    }
}
