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

import cn.hutool.core.collection.CollectionUtil;
import com.prolog.eis.enums.base.BaseStatusEnum;
import com.prolog.eis.enums.wms.WmsCarryTaskInCode;
import com.prolog.eis.model.inbound.BizInboundTask;
import com.prolog.eis.service.core.InboundService;
import com.prolog.eis.service.inbound.BizInboundTaskDetailService;
import com.prolog.eis.service.inbound.BizInboundTaskService;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.utils.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author dengj
 * @version 1.0
 * @date 2023/3/27 11:46
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class InboundServiceImpl implements InboundService {
    private final BizInboundTaskService bizInboundTaskService;
    private final BizInboundTaskDetailService bizInboundTaskDetailService;

    @Override
    public void inboundTaskCbWms() {
        List<BizInboundTask> inbounds = bizInboundTaskService.findByMap(MapUtils.put("status", BaseStatusEnum.STATUS_FINISH.getCode()).getMap());
        if (CollectionUtil.isEmpty(inbounds)) {
            return;
        }
        for (BizInboundTask inbound : inbounds) {
            if (inbound.getWmsLocationCode().equals(WmsCarryTaskInCode.AGVL.getCode())) {
                continue;
            }
            try {
                bizInboundTaskService.wmsInCallBack(inbound);
            } catch (Exception e) {
                log.error("入库任务【{}】回传wms失败:" + e.getMessage(), inbound.getContainerNo(), e);
            }
        }
    }


    @Override
    public void wmsInboundAgvCarryTask() throws Exception {
        Criteria criteria = new Criteria(BizInboundTask.class);
        criteria.restrictionBuilder().eq(BizInboundTask::getStatus, 0).ne(BizInboundTask::getWmsLocationCode, WmsCarryTaskInCode.AGVL.getCode()).isNotNull(BizInboundTask::getOriginalLocation).build();
        List<BizInboundTask> bizInboundTaskList = bizInboundTaskService.findByCriteria(criteria);
        if (CollectionUtil.isEmpty(bizInboundTaskList)) {
            return;
        }
        for (BizInboundTask bizInboundTask : bizInboundTaskList) {
            try {
                bizInboundTask.setSubList(bizInboundTaskDetailService.findInBoundDetailByInboundId(bizInboundTask.getId()));
                bizInboundTaskService.inboundAgvCarryTask(bizInboundTask);
            } catch (Exception e) {
                log.error("发送入库上架任务失败：{}", e.getMessage(), e);
            }
        }
    }

    @Override
    public void agvlInboundTask() {
        Criteria criteria = new Criteria(BizInboundTask.class);
        criteria.restrictionBuilder().in(BizInboundTask::getStatus, Arrays.asList(BaseStatusEnum.STATUS_INIT.getCode(), BaseStatusEnum.STATUS_OFFER.getCode()))
                .eq(BizInboundTask::getWmsLocationCode, WmsCarryTaskInCode.AGVL.getCode()).isNotNull(BizInboundTask::getOriginalLocation).build();
        List<BizInboundTask> bizInboundTaskList = bizInboundTaskService.findByCriteria(criteria);
        if (CollectionUtil.isEmpty(bizInboundTaskList)) {
            return;
        }
        //暂存区任务
        List<BizInboundTask> cacheInboundTaskList = bizInboundTaskList.stream().filter(item -> Objects.equals(item.getStatus(), 15))
                .sorted(Comparator.comparing(BizInboundTask::getId)).collect(Collectors.toList());
        //库内任务
        List<BizInboundTask> storeInboundTaskList = bizInboundTaskList.stream().filter(item -> Objects.equals(item.getStatus(), 0))
                .sorted(Comparator.comparing(BizInboundTask::getId)).collect(Collectors.toList());
        for (BizInboundTask bizInboundTask : cacheInboundTaskList) {
            try {
                bizInboundTask.setSubList(bizInboundTaskDetailService.findInBoundDetailByInboundId(bizInboundTask.getId()));
                bizInboundTaskService.sortingInboundCarryTask(bizInboundTask, true);
            } catch (Exception e) {
                log.error("发送入库上架任务失败：{}", e.getMessage(), e);
            }
        }
        long count = cacheInboundTaskList.stream().filter(item -> Objects.equals(item.getStatus(), 15)).count();
        for (BizInboundTask bizInboundTask : storeInboundTaskList) {
            try {
                bizInboundTask.setSubList(bizInboundTaskDetailService.findInBoundDetailByInboundId(bizInboundTask.getId()));
                bizInboundTaskService.sortingInboundCarryTask(bizInboundTask, count == 0);
            } catch (Exception e) {
                log.error("发送入库上架任务失败：{}", e.getMessage(), e);
            }
        }
    }
}
