package com.prolog.cs.biz.outbound.service.strategy.handles;

import com.prolog.cs.biz.outbound.service.PostBackErpService;
import com.prolog.cs.biz.outbound.service.strategy.STRATEGY_CODE;
import com.prolog.framework.utils.ObjectUtils;
import com.prolog.upcloud.base.strategy.model.BusinessControl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @program: upcloud-base-wh-outbound
 * @description:
 * @author: Huanglh
 * @create: 2022-01-10 17:16
 **/
@Service
@Slf4j
public class PostBackErpHandle extends AbstractStrategyHandle {

    @Autowired
    private PostBackErpService postBackErpService;

    public PostBackErpHandle() {
        super();
    }

    public PostBackErpHandle(String point, Map<STRATEGY_CODE, BusinessControl> businessControlMap) {
        super(point, businessControlMap);
    }

    /**
     * 执行操作
     * 配置点说明："do"-不判断inCheckOn配置直接执行;"out"-当前配置点为外复核，"in"-当前配置点为内复核回传
     * 参数：0-omsBillIds(List),1-errorMsg,2-billGroupIds(List)，3-warehouseId
     *
     * @param args
     */
    @Override
    public void execute(Object... args) {
        List<String> omsBillIds = super.getArg(0, List.class, args);
        if (ObjectUtils.isNull(omsBillIds)) {
            log.warn("回传ERP传入OmsBillId列表为空");
            return;
//            throw new ParameterException("回传ERP需传入OmsBillId列表");
        }

        if (!"do".equalsIgnoreCase(point)) {
            // 参数值 "0"-不开启内复核回传(默认) "1"-开启内复核回传
            String regValue = super.getRgeValue(STRATEGY_CODE.POST_BACK_ERP__IN_CHECK__ON, "0");

            // 找出所有不执行的情况，return
            if (("out".equalsIgnoreCase(point) && "1".equals(regValue)) ||
              ("in".equalsIgnoreCase(point) && "0".equals(regValue))) {
                return;
            }
        }

        // 准备参数
        String errorMsg = super.getArg(1, String.class, args);
        List<String> billGroupIds = super.getArg(2, List.class, args);
        String warehouseId = super.getArg(3, String.class, args);
        this.postBackErp(omsBillIds, errorMsg, point, billGroupIds, warehouseId);
    }

    @Override
    public Object getResult(Object... args) {
        return null;
    }

    /**
     * 回传ERP
     *
     * @param omsBillIds
     * @param errorMsg
     */
    @Transactional
    void postBackErp(List<String> omsBillIds, String errorMsg, String point, List<String> billGroupIds,
                     String warehouseId) {
        // 外复核或内复核回传ERP需要先扣库存
//        if ("out".equalsIgnoreCase(point) || "in".equalsIgnoreCase(point)) {
//            postBackErpService.invStockConfirm(billGroupIds, warehouseId);
//        }

        // 回传ERP数据写入中间表
        postBackErpService.postBackErpByTable(omsBillIds);

        // 根据业务配置参数判断是否生产回传ERP的消息
        String regValue = super.getRgeValue(STRATEGY_CODE.POST_BACK_ERP__MQ__ON, "1");
        if ("1".equals(regValue)) {
            postBackErpService.postBackErpByMq(omsBillIds, errorMsg);
        }
    }
}
