package com.prolog.upcloud.base.wh.operate.service.impl.eis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.prolog.entity.dto.QueryContainerPositionDTO;
import com.prolog.entity.vo.*;
import com.prolog.enums.InventoryAuditEnum;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.inte.EisHandler;
import com.prolog.upcloud.base.wh.operate.model.EisLog;
import com.prolog.upcloud.base.wh.operate.service.InterfacePlatform;
import com.prolog.upcloud.base.wh.operate.service.MqOperateProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

import static com.prolog.constant.EisInterfaceConstant.*;
import static com.prolog.enums.OutBoundRequestEnum.INV_CONSERVE;
import static com.prolog.enums.OutBoundRequestEnum.INV_PLANCHECK;
import static com.prolog.upcloud.base.wh.operate.constant.EisLogConstant.FAIL;

@Slf4j
@Component
public class EisOperateHandler extends InterfacePlatform implements EisHandler {

    @Resource
    private MqOperateProducer producer;


    /**
     * 出库任务推送
     */
    @Override
    public void outBoundRequest(List<OutBoundRequestVO> list) {
        boolean needSynchronization = list.stream().anyMatch(t -> t.getSubList().stream().anyMatch(s -> INV_CONSERVE.name().equals(s.getBusinessType()) || INV_PLANCHECK.name().equals(s.getBusinessType())));
        if (!needSynchronization) {
            //因为时效性，如果是非养护的走异步请求
            producer.outBoundRequest(list);
            return;
        }

        EisLog result = (EisLog) call4Eis(OUT_BOUND_REQUEST, JSONObject.toJSONString(list));
        if (FAIL.equals(result.getStatus())) {
            throw new BizException(result.getResponse());
        }
    }

    /**
     * 作业完成确认
     */
    @Override
    public void operationComplete(List<OperationCompleteVO> list) {
        producer.operationComplete(list);
    }

    /**
     * 盘点审核信息推送
     */
    @Override
    public void inventoryAudit(List<InventoryAuditVO> list) {
        boolean needSynchronization = list.stream().anyMatch(t -> InventoryAuditEnum.INV_MOVE.getWorkType().equals(t.getWorkType()));
        if (!needSynchronization) {
            // 因为移库出库需要先推送盘点审核信息，后推送库存锁定接口
            producer.inventoryAudit(list);
            return;
        }
        EisLog result = (EisLog) call4Eis(INVENTORY_AUDIT, JSONObject.toJSONString(list));
        if (FAIL.equals(result.getStatus())) {
            throw new BizException(result.getResponse());
        }
    }

    /**
     * 托盘库存点位查询接口
     */
    @Override
    public List<QueryContainerPositionDTO> queryContainerPosition(QueryContainerPositionVO vo) {
        EisLog result = (EisLog) call4Eis(QUERY_CONTAINER_POSITION, JSONObject.toJSONString(vo));
        if (FAIL.equals(result.getStatus())) {
            throw new BizException(result.getResponse());
        }
        RestMessage restMessage = JSONObject.parseObject(result.getResponse(), RestMessage.class);
        Map<String, Object> data = (Map) restMessage.getData();
        Object resultList = data.get("list");
        if (null == resultList) {
            return null;
        }
        return JSONArray.parseArray(JSONObject.toJSONString(resultList), QueryContainerPositionDTO.class);
    }

    /**
     * 盘点库存锁定
     */
    @Override
    public void inventoryLock(List<InventoryLockVO> list) {
        producer.inventoryLock(list);
    }

    /**
     * 容器借道下发
     */
    @Override
    public void containerTailgatingRequest(List<ContainerTailgatingRequestVO> list) {
        throw new UnsupportedOperationException();
    }
}
