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

import com.prolog.eis.constants.base.BaseTaskTypeConstants;
import com.prolog.eis.constants.base.ContainerTaskStatusConstants;
import com.prolog.eis.constants.base.LocationType;
import com.prolog.eis.core.dto.route.CarryTaskCallbackDto;
import com.prolog.eis.core.model.biz.carry.CarryTaskCallback;
import com.prolog.eis.dao.shelves.PalletInfoMapper;
import com.prolog.eis.feign.RouteFeign;
import com.prolog.eis.model.shelves.DismantlingLocationHitRecord;
import com.prolog.eis.model.shelves.PalletInfo;
import com.prolog.eis.service.shelves.CallBackDataService;
import com.prolog.eis.service.shelves.CarryTaskCallbackService;
import com.prolog.eis.service.shelves.DismantlingLocationHitRecordService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.utils.JsonUtils;
import com.prolog.framework.utils.StringUtils;
import com.prolog.fx.inf.core.InterfaceType;
import com.prolog.fx.inf.core.annotation.InterfaceMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CarryTaskCallbackServiceImpl implements CarryTaskCallbackService {

    @Resource
    RouteFeign routeFeign;

    @Resource
    PalletInfoMapper palletInfoMapper;

    @Resource
    CallBackDataService callBackDataService;

    @Resource
    private DismantlingLocationHitRecordService dismantlingLocationHitRecordService;

    @Override
    @InterfaceMethod(key = "carryTaskCallBack", name = "拆零区货架容器到位回告", type = InterfaceType.Receive)
    public void callBack() throws Exception {

        List<CarryTaskCallbackDto> allCallback = getAllCallBack();

        if(allCallback.isEmpty()){
            log.info("暂无任务回告！");
            return;
        }
        List<PalletInfo> palletInfoList = palletInfoMapper.findByMap(null, PalletInfo.class);
        if (palletInfoList.isEmpty()) {
            log.warn("无货架数据！");
            return;
        }

        palletInfoList = palletInfoList.stream().filter(p -> !StringUtils.isEmpty(p.getContainerMoveTaskId())).collect(Collectors.toList());

        Map<String, PalletInfo> containerInfoMap = palletInfoList.stream().collect(Collectors.toMap(PalletInfo::getContainerMoveTaskId, o -> o));
        PalletInfo palletInfo = null;
        //int taskType = 0;

        for (CarryTaskCallbackDto carryTaskCallbackDto : allCallback) {
            //long startTime = System.currentTimeMillis();

            try {
                if(StringUtils.isEmpty(carryTaskCallbackDto.getId())){
                    continue;
                }
                palletInfo = containerInfoMap.get(carryTaskCallbackDto.getId());
                if (!containerInfoMap.containsKey(carryTaskCallbackDto.getId())) {
                    log.warn("任务号" + carryTaskCallbackDto.getId() + "的容器信息不存在");
                    containerNoCallBack(carryTaskCallbackDto);
                    continue;
                }
                if (!palletInfo.getPalletNo().equals(carryTaskCallbackDto.getContainerNo())) {
                    log.warn("任务号" + carryTaskCallbackDto.getId() + "的容器信息无法对应");
                    containerNoCallBack(carryTaskCallbackDto);
                    continue;
                }
                callBackDataService.pallNoArriveDeal(palletInfo,carryTaskCallbackDto);

            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }


    }

    //拆零区托盘到位转历史
    @InterfaceMethod(key = "carryTaskCallBack", name = "拆零区容器到位回告", type = InterfaceType.Receive)
    private void containerNoCallBack(CarryTaskCallbackDto dto){
        Criteria criteria = Criteria.forClass(DismantlingLocationHitRecord.class);
        criteria.setRestriction(Restrictions.eq("locationType", LocationType.LOCATION_TYPE_CONTAINER).and(Restrictions.eq("taskType", BaseTaskTypeConstants.INBOUND_TYPE)));
        List<DismantlingLocationHitRecord> records = dismantlingLocationHitRecordService.findByCriteria(criteria);
        Map<String, DismantlingLocationHitRecord> recordMap = records.stream().collect(Collectors.toMap(DismantlingLocationHitRecord::getContainerNo, o -> o));
        if (!recordMap.containsKey(dto.getContainerNo())) {
           return;
        }
        // 搬运任务转历史
        log.info("开始 请求搬运任务转历史");
        CarryTaskCallback carryTaskCallback = new CarryTaskCallback();
        carryTaskCallback.setId(dto.getId());
        RestMessage<String> restMessage = routeFeign.toCallbackHis(carryTaskCallback);
        if (!restMessage.isSuccess()) {
            throw new BizException("搬运任务转历史失败：" + restMessage.getMessage());
        }
    }



    @Override
    public List<CarryTaskCallbackDto> getAllCallBack() throws Exception {
        RestMessage<List<CarryTaskCallbackDto>> restMessage = routeFeign.findAllCallback();
        if (restMessage.isSuccess()) {
            return restMessage.getData();
        }
        throw new Exception("路径服务调用失败：" + restMessage.getMessage());
    }
}
