package com.zm.order.tool.server.stream.factory;

import com.zm.order.enums.OrderPayType;
import com.zm.order.enums.PerformancePlatform;
import com.zm.order.tool.server.enums.BizTypeEnum;
import com.zm.order.tool.server.stream.enums.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * bean模块转换器工厂
 *
 * <p>
 *
 * </p>
 *
 * @author nianchang
 * @date 2023/03/13 10:49 上午
 */
public class MapperFactory {
    private static final Logger logger = LoggerFactory.getLogger(MapperFactory.class);

    private static final String DELIMITER = "-";

    /**
     * 正向
     */
    private static Map<String, OrderTypeMapping> orderType = new HashMap<>();

    /**
     * 逆向
     */
    private static Map<String, OrderTypeMapping> reverseOrderType = new HashMap<>();

    /**
     * 正向
     */
    private static Map<String, OrderSourceMapping> orderSource = new HashMap<>();

    /**
     * 逆向
     */
    private static Map<String, OrderSourceMapping> reverseOrderSource = new HashMap<>();

    /**
     * 正向
     */
    private static Map<String, OrderStatusMapping> orderStatus = new HashMap<>();

    /**
     * 逆向
     */
    private static Map<String, OrderStatusMapping> reverseOrderStatus = new HashMap<>();

    /**
     * 正向
     */
    private static Map<String, OrderSubTypeMapping> orderSubType = new HashMap<>();

    /**
     * 逆向
     */
    private static Map<String, OrderSubTypeMapping> reverseOrderSubType = new HashMap<>();

    /**
     * 正向
     */
    private static Map<String, OrderPayTypeMapping> payType = new HashMap<>();

    /**
     * 逆向
     */
    private static Map<String, OrderPayTypeMapping> reversePayType = new HashMap<>();

    /**
     * 正向
     */
    private static Map<String, RefundStatusMapping> refundStatus = new HashMap<>();

    /**
     * 逆向
     */
    private static Map<String, RefundStatusMapping> reverseRefundStatus = new HashMap<>();

    /**
     * 正向
     */
    private static Map<String, PerformancePlatformMapping> platform = new HashMap<>();

    /**
     * 逆向
     */
    public static Map<String, PerformancePlatformMapping> reversePlatform = new HashMap<>();

    /**
     * 订单类型映射
     *
     * <p>
     *      只映射常规类型，历史堂食、外卖等映射需要与其他字段关联映射，外层自行进行判断
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer orderTypeMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderTypeMapping result = orderType.get(join(bizType, code));
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 订单类型逆向映射
     *
     * <p>
     *      只映射常规类型，堂食、外卖等映射需要与其他字段关联映射，外层自行进行判断
     * </p>
     *
     * @param bizType 业务类型
     * @param code 新枚举值
     *
     * @return 旧枚举值
     */
    public static Integer reverseOrderTypeMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderTypeMapping result = reverseOrderType.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    /**
     * 订单来源映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer orderSourceMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderSourceMapping result = orderSource.get(join(bizType, code));
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 订单来源逆向映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 新枚举值
     *
     * @return 旧枚举值
     */
    public static Integer reverseOrderSourceMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderSourceMapping result = reverseOrderSource.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    /**
     * 支付方式映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer payTypeMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderPayTypeMapping result = payType.get(join(bizType, code));
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 支付方式映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer payTypeMap(Integer bizType, Integer code, Integer orderType) {
        if (bizType == null || code == null) {
            return code;
        }
        if (orderType == 30 && code == 1) {
            return OrderPayType.DY_WE_CHAT.getType();
        }
        if (orderType == 30 && code == 2) {
            return OrderPayType.DY_ALI_PAY.getType();
        }
        if (orderType == 30 && code == 17) {
            return OrderPayType.DY_PAY.getType();
        }
        OrderPayTypeMapping result = payType.get(join(bizType, code));
        if (Objects.isNull(result)) {
            logger.info("[orderConvertNonMatch][payTypeMap]baking, payType :{}", orderType);
        }
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 支付方式逆向映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 新枚举值
     *
     * @return 旧枚举值
     */
    public static Integer reversePayTypeMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderPayTypeMapping result = reversePayType.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    /**
     * 支付方式逆向映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 新枚举值
     *
     * @return 旧枚举值
     */
    public static Integer reversePayTypeMap(Integer bizType, Integer code, Integer orderType) {
        if (bizType == null || code == null) {
            return code;
        }
        if (orderType == 30 && code == 101) {
            return 1;
        }
        if (orderType == 30 && code == 102) {
            return 2;
        }
        if (orderType == 30 && code == 103) {
            return 17;
        }
        OrderPayTypeMapping result = reversePayType.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    /**
     * 订单子类型映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer orderSubTypeMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderSubTypeMapping result = orderSubType.get(join(bizType, code));
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 订单子类型逆向映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 新枚举值
     *
     * @return 旧枚举值
     */
    public static Integer reverseOrderSubTypeMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderSubTypeMapping result = reverseOrderSubType.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    /**
     * 订单状态映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer orderStatusMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderStatusMapping result = orderStatus.get(join(bizType, code));
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 订单状态映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer reverseOrderStatusMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        OrderStatusMapping result = reverseOrderStatus.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    /**
     * 退款单状态映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static Integer refundStatusMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        RefundStatusMapping result = refundStatus.get(join(bizType, code));
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 订单状态逆向映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 新枚举值
     *
     * @return 旧枚举值
     */
    public static Integer reverseRefundStatusMap(Integer bizType, Integer code) {
        if (bizType == null || code == null) {
            return code;
        }
        RefundStatusMapping result = reverseRefundStatus.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    /**
     * 履约平台映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 旧枚举值
     *
     * @return 新枚举值
     */
    public static String platform(Integer bizType, String code, Integer orderType) {
        if (bizType == null || code == null) {
            return code;
        }
        if (BizTypeEnum.CATERING.getType().equals(bizType) 
                || BizTypeEnum.ASSISTANT.getType().equals(bizType)
                || BizTypeEnum.MEAL_MATE.getType().equals(bizType)) {
            if (orderType == 3 && "0".equals(code)) {
                return PerformancePlatform.SELLER.getType();
            }
        }
        PerformancePlatformMapping result = platform.get(join(bizType, code));
        return result != null ? result.getNewCode() : code;
    }

    /**
     * 履约平台逆向映射
     *
     * <p>
     *
     * </p>
     *
     * @param bizType 业务类型
     * @param code 新枚举值
     *
     * @return 旧枚举值
     */
    public static String reversePlatform(Integer bizType, String code) {
        if (bizType == null || code == null) {
            return code;
        }
        PerformancePlatformMapping result = reversePlatform.get(join(bizType, code));
        return result != null ? result.getOldCode() : code;
    }

    private static String join(Integer bizType, Integer code) {
        return bizType + DELIMITER + code;
    }

    private static String join(Integer bizType, String code) {
        return bizType + DELIMITER + code;
    }

    static {
        for (OrderTypeMapping value : OrderTypeMapping.values()) {
            orderType.put(value.getBizType() + DELIMITER + value.getOldCode(), value);
            reverseOrderType.put(value.getBizType() + DELIMITER + value.getNewCode(), value);
        }
        for (OrderSubTypeMapping value : OrderSubTypeMapping.values()) {
            orderSubType.put(value.getBizType() + DELIMITER + value.getOldCode(), value);
            reverseOrderSubType.put(value.getBizType() + DELIMITER + value.getNewCode(), value);
        }
        for (OrderPayTypeMapping value : OrderPayTypeMapping.values()) {
            payType.put(value.getBizType() + DELIMITER + value.getOldCode(), value);
            reversePayType.put(value.getBizType() + DELIMITER + value.getNewCode(), value);
        }
        for (OrderSourceMapping value : OrderSourceMapping.values()) {
            orderSource.put(value.getBizType() + DELIMITER + value.getOldCode(), value);
            reverseOrderSource.put(value.getBizType() + DELIMITER + value.getNewCode(), value);
        }
        for (OrderStatusMapping value : OrderStatusMapping.values()) {
            orderStatus.put(value.getBizType() + DELIMITER + value.getOldCode(), value);
            reverseOrderStatus.put(value.getBizType() + DELIMITER + value.getNewCode(), value);
        }
        for (RefundStatusMapping value : RefundStatusMapping.values()) {
            refundStatus.put(value.getBizType() + DELIMITER + value.getOldCode(), value);
            reverseRefundStatus.put(value.getBizType() + DELIMITER + value.getNewCode(), value);
        }
        for (PerformancePlatformMapping value : PerformancePlatformMapping.values()) {
            platform.put(value.getBizType() + DELIMITER + value.getOldCode(), value);
            reversePlatform.put(value.getBizType() + DELIMITER + value.getNewCode(), value);
        }
    }

}
