package cn.iocoder.yudao.module.order.listener;

import cn.iocoder.yudao.framework.apollo.core.event.Order.OrderApprovalTypeCheckEvent;
import cn.iocoder.yudao.module.order.enums.OrderApprovalTypeEnum;
import cn.iocoder.yudao.module.order.service.approval.OrderApprovalService;
import cn.iocoder.yudao.module.order.service.orderWarehouseApproval.OrderWarehouseApprovalService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 校验审批类型是否互斥
 *
 * @author zhengYi
 */
@Component("OrderApprovalTypeCheckListener")
@AllArgsConstructor
@Slf4j
public class OrderApprovalTypeCheckListener {

    private final OrderApprovalService orderApprovalService;
    private final OrderWarehouseApprovalService orderWarehouseApprovalService;

    @EventListener(OrderApprovalTypeCheckEvent.class)
    public void orderApprovalTypeCheckEvent(OrderApprovalTypeCheckEvent event) {
        // 枚举配置中的互斥类型集合不能添加新值，这里需要重新定义新集合补充当前审批类型进行查询
        List<Integer> typeList = null;
        if (Objects.nonNull(event.getApprovalType())) {
            OrderApprovalTypeEnum approvalTypeEnum = OrderApprovalTypeEnum.valueOf(event.getApprovalType());
            if (Objects.isNull(approvalTypeEnum)) {
                // 没有配置相关审批类型则不做校验
                event.setResult(Boolean.FALSE);
                return;
            }
            List<Integer> types = approvalTypeEnum.getMutualExclusionValues();
            if (Objects.isNull(types)) {
                // 没有配置相关审批类型则不做校验
                event.setResult(Boolean.FALSE);
                return;
            }

            // 包含-1则表示订单不能有任何审批状态存在,所以不用区分类型，查询所有的进行中审批
            if (!types.contains(-1)) {
                typeList = new ArrayList<>(types);
                typeList.add(event.getApprovalType());
            }
        }else {
            typeList = new ArrayList<>(event.getApprovalTypeList());
        }
        // 查询当前订单正在进行的审批
        boolean result = orderApprovalService.countProcessingApproval(event.getOrderId(), event.getOrderItemIdList(), typeList);
        // 入仓相关正在进行的审批业务查询
        if (!result){
            List<Integer> warehouseApprovalTypeList = null;
            if (Objects.nonNull(typeList)){
                // 1 入仓修改 2 调仓 3 退仓
                warehouseApprovalTypeList = new ArrayList<>();
                if (typeList.contains(OrderApprovalTypeEnum.WAREHOUSE_UPDATE.getValue())){
                    warehouseApprovalTypeList.add(1);
                }
                if (typeList.contains(OrderApprovalTypeEnum.WAREHOUSE_ADJUST.getValue())){
                    warehouseApprovalTypeList.add(2);
                }
                if (typeList.contains(OrderApprovalTypeEnum.WAREHOUSE_ROLLBACK.getValue())){
                    warehouseApprovalTypeList.add(3);
                }
                if (warehouseApprovalTypeList.size() == 0){
                    event.setResult(result);
                    return;
                }
            }
            result = orderWarehouseApprovalService.countProcessingApproval(event.getOrderId(), event.getOrderItemIdList(), warehouseApprovalTypeList);
        }
        // TODO 当订单互斥所有审批类型时，是否也要对自编号的审批状态进行校验
        event.setResult(result);
    }
}
