package com.seer.teach.mall.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.seer.teach.common.enums.*;
import com.seer.teach.mall.context.OrderLogContextHolder;
import com.seer.teach.mall.entity.OrderChildEntity;
import com.seer.teach.mall.entity.OrderEntity;

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

/**
 * 订单操作日志工具类
 *
 * @author Captain
 */
public class OrderLogUtils {

    public static void setOrderInfo(Integer orderId, Integer childOrderId) {
        OrderLogContextHolder.OrderLogInfo logInfo = OrderLogContextHolder.getOrCreateLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
    }

    /**
     * 设置订单基础信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param operateType 操作类型枚举
     * @param content 操作内容描述
     */
    public static void setOrderInfo(Integer orderId, Integer childOrderId, OperateTypeEnum operateType, String content) {
        OrderLogContextHolder.OrderLogInfo logInfo = OrderLogContextHolder.getOrCreateLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(operateType.getCode());
        logInfo.setContent(content);
    }

    /**
     * 设置订单基础信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param operateType 操作类型枚举
     * @param content 操作内容描述
     * @param extendMap 扩展信息
     */
    public static void setOrderInfo(Integer orderId, Integer childOrderId, OperateTypeEnum operateType, String content,
                                    Map<String, Object> extendMap) {
        setOrderInfo(orderId, childOrderId, operateType, content);
        OrderLogContextHolder.OrderLogInfo logInfo = OrderLogContextHolder.getOrCreateLogInfo();
        logInfo.setExtendMap(extendMap);
    }

    /**
     * 设置订单状态变更信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param beforeStatus 变更前状态
     * @param afterStatus 变更后状态
     */
    public static void setStatusInfo(Integer orderId, Integer childOrderId, Integer beforeStatus, Integer afterStatus) {
        OrderLogContextHolder.OrderLogInfo logInfo = OrderLogContextHolder.getOrCreateLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setBeforeStatus(beforeStatus);
        logInfo.setAfterStatus(afterStatus);
    }

    /**
     * 设置订单状态变更信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param beforeStatus 变更前状态
     * @param afterStatus 变更后状态
     * @param extendMap 扩展信息
     */
    public static void setStatusInfo(Integer orderId, Integer childOrderId, Integer beforeStatus, Integer afterStatus,
                                     Map<String, Object> extendMap) {
        setStatusInfo(orderId, childOrderId, beforeStatus, afterStatus);
        OrderLogContextHolder.OrderLogInfo logInfo = OrderLogContextHolder.getOrCreateLogInfo();
        if (ObjectUtil.isNotEmpty(extendMap)) {
            logInfo.setExtendMap(extendMap);
        }
    }

    /**
     * 设置用户信息
     *
     * @param userId 用户ID
     * @param userType 用户类型枚举
     */
    public static void setUserInfo(Integer userId, UserTypeEnum userType) {
        OrderLogContextHolder.OrderLogInfo logInfo = OrderLogContextHolder.getOrCreateLogInfo();
        logInfo.setUserId(userId);
        logInfo.setUserType(userType.getCode());
    }

    /**
     * 设置商家信息
     *
     * @param merchantId 商家ID
     */
    public static void setMerchantInfo(Long merchantId) {
        OrderLogContextHolder.setMerchantId(merchantId);
    }

    /**
     * 设置订单状态信息
     *
     * @param shippingStatus 发货状态枚举
     * @param commentStatus 评论状态枚举
     */
    public static void setOrderStatusInfo(ShippingStatusEnum shippingStatus,
                                          CommentStatusEnum commentStatus) {
        OrderLogContextHolder.OrderLogInfo logInfo = OrderLogContextHolder.getOrCreateLogInfo();
        if (shippingStatus != null) {
            logInfo.setShippingStatus(shippingStatus.getCode());
        }
        if (commentStatus != null) {
            logInfo.setCommentStatus(commentStatus.getCode());
        }
    }

    /**
     * 创建一个新的订单日志信息对象
     * 
     * @return 新创建的日志信息对象
     */
    public static OrderLogContextHolder.OrderLogInfo createNewLogInfo() {
        OrderLogContextHolder.OrderLogInfo logInfo = new OrderLogContextHolder.OrderLogInfo();
        OrderLogContextHolder.addLogInfo(logInfo);
        return logInfo;
    }

    /**
     * 设置创建订单日志信息
     *
     * @param orderChildEntities 子订单
     */
    public static void setCreateOrderInfo(Collection<OrderChildEntity> orderChildEntities) {
        if(CollUtil.isEmpty(orderChildEntities)){
            return;
        }
        for (OrderChildEntity orderChild : orderChildEntities) {
            OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
            logInfo.setOrderId(orderChild.getParentOrderId());
            logInfo.setChildOrderId(orderChild.getId());
            logInfo.setUserId(orderChild.getUserId());
            logInfo.setMerchantId(orderChild.getMerchantUserId());
            logInfo.setUserType(UserTypeEnum.PARENT.getCode());
            logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
            logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
            logInfo.setAfterStatus(OrderStatusEnum.PENDING_PAYMENT.getCode());
        }
    }

    /**
     * 设置支付订单日志信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param userId 用户ID
     */
    public static void setPayOrderInfo(Integer orderId, Integer childOrderId, Integer userId) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent("用户支付订单");
        logInfo.setUserId(userId);
        logInfo.setUserType(UserTypeEnum.PARENT.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
        logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
        logInfo.setBeforeStatus(OrderStatusEnum.PENDING_PAYMENT.getCode());
        logInfo.setAfterStatus(OrderStatusEnum.PAID.getCode());
    }



    /**
     * 设置确认收货日志信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param userId 用户ID
     */
    public static void setReceiveOrderInfo(Integer orderId, Integer childOrderId, Integer userId) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent("用户确认收货");
        logInfo.setUserId(userId);
        logInfo.setUserType(UserTypeEnum.PARENT.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.SIGNED.getCode());
        logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
        logInfo.setBeforeStatus(OrderStatusEnum.SHIPPED.getCode());
        logInfo.setAfterStatus(OrderStatusEnum.COMPLETED.getCode());
    }

    /**
     * 设置删除订单日志信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param userId 用户ID
     */
    public static void setAdminDeleteOrderInfo(Integer orderId, Integer childOrderId, Integer userId) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.DELETE.getCode());
        logInfo.setContent("用户删除订单");
        logInfo.setUserId(userId);
        logInfo.setUserType(UserTypeEnum.PARENT.getCode());
        logInfo.setBeforeStatus(OrderStatusEnum.CANCELED.getCode());
        logInfo.setAfterStatus(OrderStatusEnum.CANCELED.getCode());
    }

    /**
     * 设置删除订单日志信息
     *
     * @param orderChildEntityList 订单
     */
    public static void setDeleteOrderInfo(List<OrderChildEntity> orderChildEntityList) {
        if(CollUtil.isEmpty(orderChildEntityList)){
            return;
        }
        for (OrderChildEntity orderChildEntity : orderChildEntityList) {
            OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
            logInfo.setOrderId(orderChildEntity.getParentOrderId());
            logInfo.setChildOrderId(orderChildEntity.getId());
            logInfo.setUserId(orderChildEntity.getUserId());
            logInfo.setMerchantId(orderChildEntity.getMerchantUserId());
            logInfo.setUserType(UserTypeEnum.PARENT.getCode());
            logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
            logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
            logInfo.setBeforeStatus(OrderStatusEnum.CANCELED.getCode());
            logInfo.setAfterStatus(OrderStatusEnum.DELETED.getCode());

        }
    }

    /**
     * 设置更新订单日志信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param userId 用户ID
     */
    public static void setUpdateOrderInfo(Integer orderId, Integer childOrderId, Integer userId) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent("用户更新订单");
        logInfo.setUserId(userId);
        logInfo.setUserType(UserTypeEnum.PARENT.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
        logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
        logInfo.setBeforeStatus(OrderStatusEnum.PENDING_PAYMENT.getCode());
        logInfo.setAfterStatus(OrderStatusEnum.PENDING_PAYMENT.getCode());
    }

    /**
     * 设置系统自动取消订单日志信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     */
    public static void setExpireOrderInfo(Integer orderId, Integer childOrderId) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.DELETE.getCode());
        logInfo.setContent("系统自动取消订单");
        logInfo.setUserId(0);
        logInfo.setUserType(UserTypeEnum.SYSTEM.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
        logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
        logInfo.setBeforeStatus(OrderStatusEnum.PENDING_PAYMENT.getCode());
        logInfo.setAfterStatus(OrderStatusEnum.CANCELED.getCode());
    }

    /**
     * 设置管理员修改订单日志信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param userId 管理员ID
     * @param order 订单实体
     */
    public static void setUpdateOrderInfo(Integer orderId, Integer childOrderId, Integer userId, OrderEntity order) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent("管理员修改订单");
        logInfo.setUserId(userId);
        logInfo.setUserType(UserTypeEnum.ADMIN.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.fromCode(order.getShippingStatus()).getCode());
        logInfo.setCommentStatus(CommentStatusEnum.fromCode(order.getCommentStatus()).getCode());
        logInfo.setBeforeStatus(order.getOrderStatus());
        logInfo.setAfterStatus(order.getOrderStatus());
    }

    public static void setDeliveryOrderInfo(Integer orderId, Integer childOrderId, Integer userId,ShippingStatusEnum shippingStatus,CommentStatusEnum commentStatus) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent("管理员发货");
        logInfo.setUserId(userId);
        logInfo.setShippingStatus(shippingStatus.getCode());
        logInfo.setCommentStatus(commentStatus.getCode());
        logInfo.setBeforeStatus(OrderStatusEnum.PAID.getCode());
        logInfo.setAfterStatus(OrderStatusEnum.SHIPPED.getCode());
    }

    /**
     * 设置管理员删除订单日志信息
     *
     * @param orderId 订单ID
     * @param childOrderId 子订单ID
     * @param userId 管理员ID
     * @param order 订单实体
     */
    public static void setAdminDeleteOrderInfo(Integer orderId, Integer childOrderId, Integer userId, OrderEntity order) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setUserId(userId);
        logInfo.setUserType(UserTypeEnum.ADMIN.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.fromCode(order.getShippingStatus()).getCode());
        logInfo.setCommentStatus(CommentStatusEnum.fromCode(order.getCommentStatus()).getCode());
        logInfo.setBeforeStatus(order.getOrderStatus());
        logInfo.setAfterStatus(order.getOrderStatus());
    }

    public static void setConfirmOrderInfo(Integer orderId, Integer childOrderId, int userId, Integer beforeStatus, Integer afterStatus) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent("管理员确认订单");
        logInfo.setUserId(userId);
        logInfo.setUserType(UserTypeEnum.ADMIN.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
        logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
        logInfo.setBeforeStatus(beforeStatus);
        logInfo.setAfterStatus(afterStatus);
    }

    public static void setAdminCloseOrderInfo(Integer orderId, Integer childOrderId, Integer orderStatus, OrderEntity queryOrder) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent("管理员取消订单");
        logInfo.setUserId(queryOrder.getUserId());
        logInfo.setUserType(UserTypeEnum.ADMIN.getCode());
        logInfo.setBeforeStatus(queryOrder.getOrderStatus());
        logInfo.setAfterStatus(orderStatus);
    }

    public static void setDeliveryOrderExceptionInfo(Integer orderId, Integer childOrderId, int userId, String message, ShippingStatusEnum shippingStatusEnum, CommentStatusEnum commentStatusEnum) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderId);
        logInfo.setChildOrderId(childOrderId);
        logInfo.setOperateType(OperateTypeEnum.UPDATE.getCode());
        logInfo.setContent(message);
        logInfo.setUserId(userId);
        logInfo.setShippingStatus(shippingStatusEnum.getCode());
        logInfo.setCommentStatus(commentStatusEnum.getCode());
    }

    public static void setCancelChildOrderInfo(List<OrderChildEntity> orderChildren) {
        for (OrderChildEntity orderChild : orderChildren) {
           setCancelChildOrderInfo(orderChild);
        }
    }
    /**
     * 设置取消订单日志信息
     */
    public static void setCancelChildOrderInfo(OrderChildEntity orderChildEntity) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(orderChildEntity.getParentOrderId());
        logInfo.setChildOrderId(orderChildEntity.getId());
        logInfo.setUserId(orderChildEntity.getUserId());
        logInfo.setMerchantId(orderChildEntity.getMerchantUserId());
        logInfo.setUserType(UserTypeEnum.PARENT.getCode());
        logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
        logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
        logInfo.setBeforeStatus(OrderStatusEnum.PENDING_PAYMENT.getCode());
        logInfo.setAfterStatus(OrderStatusEnum.CANCELED.getCode());
    }

    public static void setAdminCloseOrderInfo(Integer userId, OrderEntity order, List<OrderChildEntity> childOrders) {
        for (OrderChildEntity childOrder : childOrders) {
            OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
            logInfo.setOrderId(childOrder.getParentOrderId());
            logInfo.setChildOrderId(childOrder.getId());
            logInfo.setUserId(userId);
            logInfo.setMerchantId(childOrder.getMerchantUserId());
            logInfo.setUserType(UserTypeEnum.ADMIN.getCode());
            logInfo.setShippingStatus(ShippingStatusEnum.NOT_SHIPPED.getCode());
            logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
            logInfo.setBeforeStatus(order.getOrderStatus());
            logInfo.setAfterStatus(OrderStatusEnum.CLOSED.getCode());
        }
    }

    public static void setUpdateOrderInfo(Integer userId,List<OrderChildEntity> childrenOrders, OrderEntity queryOrder, UserTypeEnum userTypeEnum) {
        for (OrderChildEntity orderChild : childrenOrders) {
            OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
            logInfo.setOrderId(orderChild.getParentOrderId());
            logInfo.setChildOrderId(orderChild.getId());
            logInfo.setUserId(userId);
            logInfo.setMerchantId(orderChild.getMerchantUserId());
            logInfo.setUserType(userTypeEnum.getCode());
            logInfo.setBeforeStatus(queryOrder.getOrderStatus());
        }
    }

    public static void setAdminDeleteOrderInfo(Integer userId, List<OrderChildEntity> childOrders, OrderEntity order, UserTypeEnum userTypeEnum) {
        for (OrderChildEntity orderChild : childOrders) {
            OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
            logInfo.setOrderId(orderChild.getParentOrderId());
            logInfo.setChildOrderId(orderChild.getId());
            logInfo.setUserId(userId);
            logInfo.setMerchantId(orderChild.getMerchantUserId());
            logInfo.setUserType(userTypeEnum.getCode());
            logInfo.setBeforeStatus(order.getOrderStatus());
        }
    }

    public static void setMerchantCloseOrderInfo(Integer userId,Long merchantUserId, OrderEntity queryOrder, OrderChildEntity childOrder) {
        OrderLogContextHolder.OrderLogInfo logInfo = createNewLogInfo();
        logInfo.setOrderId(childOrder.getParentOrderId());
        logInfo.setChildOrderId(childOrder.getId());
        logInfo.setUserId(userId);
        logInfo.setMerchantId(merchantUserId);
        logInfo.setUserType(UserTypeEnum.MERCHANT.getCode());
        logInfo.setBeforeStatus(queryOrder.getOrderStatus());
        logInfo.setAfterStatus(OrderStatusEnum.CLOSED.getCode());
        logInfo.setShippingStatus(childOrder.getShippingStatus());
        logInfo.setCommentStatus(CommentStatusEnum.NON_COMMENTED.getCode());
    }
}