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

import cn.hutool.core.util.StrUtil;
import com.codingapi.tx.annotation.TxTransaction;
import com.prolog.eis.constants.base.BaseTaskTypeConstants;
import com.prolog.eis.core.dto.route.CarryTaskCallbackDto;
import com.prolog.eis.service.carry.WmsCarryTaskService;
import com.prolog.eis.service.core.EisCallBackService;
import com.prolog.eis.service.core.OutboundService;
import com.prolog.eis.service.feign.RouteFeignService;
import com.prolog.eis.service.inbound.BizInboundTaskService;
import com.prolog.fx.inf.core.InterfaceType;
import com.prolog.fx.inf.core.annotation.InterfaceMethod;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class EisCallBackServiceImpl implements EisCallBackService {

    private final RouteFeignService plgFeignRouteClient;
    private final BizInboundTaskService inboundTaskService;
    private final OutboundService outboundTaskService;
    private final WmsCarryTaskService wmsCarryTaskService;

    @Override
    @TxTransaction(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    @InterfaceMethod(key = "carryTaskCallBack", name = "搬运到位回告", type = InterfaceType.Receive)
    public void handleCallBackData(CarryTaskCallbackDto carryTaskCallbackDto) throws Exception {
        if (handleData(carryTaskCallbackDto)) {
            plgFeignRouteClient.toCallbackHis(carryTaskCallbackDto.getId());
        }
    }

    private boolean handleData(CarryTaskCallbackDto carryTaskCallbackDto) throws Exception {
        //todo 回告
        if (StrUtil.isBlank(carryTaskCallbackDto.getBusinessType())) {
            return true;
        }
        switch (carryTaskCallbackDto.getBusinessType()) {
            case BaseTaskTypeConstants.INBOUND_TYPE+"":
                inboundTaskService.doInboundCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.INBOUND_RETURN+"":
                inboundTaskService.inboundReturnCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.OLD_PALLET_RETURN+"":
                inboundTaskService.inboundReturnCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.AGVL_TYPE+"":
                inboundTaskService.doAgvInboundCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.JD_TASK_TYPE+"":
                wmsCarryTaskService.doJdTaskCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.OUTBOUND_TYPE+"":
                //其它库区到位回告
                outboundTaskService.doOutboundCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.DISMANTLE_OUTBOUND_TYPE+"":
                //拆零库区到位回告
                outboundTaskService.doDismantleOutboundCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.EMPTY_OUT_TYPE+"":
                outboundTaskService.doEmptyOutboundCallBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.HJ_TYPE+"":
                inboundTaskService.hjBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.IN_TYPE+"":
                inboundTaskService.doInboundBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.DEMO+"":
                inboundTaskService.doInboundBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.SUPPLER_BACK+"":
                inboundTaskService.supplerBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.EMPTY_TYPE+"":
                inboundTaskService.emptyBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.MERGE_OUT_TYPE+"":
                outboundTaskService.mergeOutBack(carryTaskCallbackDto);
                break;
            case BaseTaskTypeConstants.CLSJ_ZC_TYPE+"":
                outboundTaskService.doDismantleInCacheCallBack(carryTaskCallbackDto);
                break;
            default:
                log.warn("容器{}任务类型不对，类型为：{}",carryTaskCallbackDto.getContainerNo(),carryTaskCallbackDto.getBusinessType());
        }
        return true;
    }


    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public void wmsInCallBack() throws Exception {
        List<BizInboundTask> inbounds = bizInboundTaskMapper.findByMap(MapUtils.put("status", BaseStatusEnum.STATUS_FINISH.getCode())
                .getMap(),BizInboundTask.class);
        if(CollectionUtil.isEmpty(inbounds)){
            return;
        }
        try {
            List<WmsInBackReqDto> wmsInBackReqDtos = new ArrayList<>();
            for (BizInboundTask inBoundTask:inbounds){

                BizInboundTaskDetail bizInboundTaskDetail = bizInboundTaskDetailMapper.findByMap(MapUtils.put("inboundId",inBoundTask.getId()).getMap(),BizInboundTaskDetail.class)
                        .stream().findFirst().orElseThrow(()->new PrologException("查询任务明细失败"));
                //添加历史
                WmsInBackReqDto wmsInReqDto = new WmsInBackReqDto();
                wmsInReqDto.setInstruction_cd(inBoundTask.getUpperSystemTaskId());
                wmsInReqDto.setShuttle_no(inBoundTask.getContainerNo());
                wmsInReqDto.setBase_cd(inBoundTask.getWmsWarehouseId());
                wmsInReqDto.setOwner_cd(bizInboundTaskDetail.getOwnerId());
                wmsInReqDto.setEnd_time(LocalDateTime.now());
                wmsInReqDto.setMessage("");
                wmsInBackReqDtos.add(wmsInReqDto);
            }
            if(!CollectionUtil.isEmpty(wmsInBackReqDtos)){
                WmsResult wmsResult = wmsInBackService.execute(wmsInBackReqDtos);
                if (!wmsResult.isSuccess()) {
                    throw new Exception("回告wms失败");
                }
            }
            for (BizInboundTask inBoundTask:inbounds){
                toInHistory(inBoundTask);
                //删除入库任务
                bizInboundTaskMapper.deleteById(inBoundTask.getId(), BizInboundTask.class);
                bizInboundTaskDetailMapper.deleteByMap(MapUtils.put("inboundId", inBoundTask.getId()).getMap(), BizInboundTaskDetail.class);
            }
        }catch (Exception e) {
            log.error("回告异常:{}", e.getMessage());
        }
    }


    private void toInHistory(BizInboundTask inBoundTask) {
        // 历史表需要提供 到时候调整
        BizInboundTaskHis wmsInTaskHis = new BizInboundTaskHis();
        inBoundTask.setStatus(30);
        inBoundTask.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(inBoundTask, wmsInTaskHis);
        inboundTaskHisMapper.save(wmsInTaskHis);

        List<BizInboundTaskDetail> detailList = bizInboundTaskDetailMapper.findByMap(MapUtils.put("inboundId", inBoundTask.getId()).getMap(), BizInboundTaskDetail.class);
        if (CollectionUtils.isEmpty(detailList)) {
            throw new PrologException("入库明细查询为空");
        }
        ArrayList<BizInboundTaskDetailHis> taskDetailHis = new ArrayList<>();
        for (BizInboundTaskDetail bizInboundTaskDetail : detailList) {
            BizInboundTaskDetailHis bizInboundTaskDetailHis = new BizInboundTaskDetailHis();
            BeanUtils.copyProperties(bizInboundTaskDetail, bizInboundTaskDetailHis);
            taskDetailHis.add(bizInboundTaskDetailHis);
        }

        inboundTaskDetailHisMapper.saveBatch(taskDetailHis);
    }
    */

}
