package com.ruoyi.web.modules.order.support;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.delivery.vo.DeliveryRegionVO;
import com.ruoyi.web.modules.delivery.vo.ShopDeliverySettingVO;
import com.ruoyi.web.modules.order.constant.OrderConstants;
import com.ruoyi.web.modules.order.domain.BizOrder;
import com.ruoyi.web.modules.order.dto.OrderDeliveredDTO;
import com.ruoyi.web.modules.order.dto.OrderLogisticsDTO;
import com.ruoyi.web.modules.order.dto.OrderShipDTO;

/**
 * 订单状态与参数校验
 */
public final class OrderSupport
{
    private OrderSupport()
    {
    }

    public static BizOrder assertOrderBelongsToShop(BizOrder order, Long shopId)
    {
        if (order == null || shopId == null || !shopId.equals(order.getShopId()))
        {
            throw new ServiceException(OrderConstants.MSG_NOT_FOUND);
        }
        return order;
    }

    public static boolean isShipModuleStatus(String orderStatus)
    {
        return OrderConstants.STATUS_PENDING_SHIP.equals(orderStatus)
                || OrderConstants.STATUS_SHIPPED.equals(orderStatus)
                || OrderConstants.STATUS_COMPLETED.equals(orderStatus);
    }

    public static void assertShipModuleVisible(BizOrder order)
    {
        if (order == null || !isShipModuleStatus(order.getOrderStatus()))
        {
            throw new ServiceException(OrderConstants.MSG_NOT_FOUND);
        }
    }

    public static void assertShipTab(String orderStatus)
    {
        if (!isShipModuleStatus(orderStatus))
        {
            throw new ServiceException(OrderConstants.MSG_INVALID_SHIP_TAB);
        }
    }

    public static void assertPendingShip(BizOrder order)
    {
        if (order == null || !OrderConstants.STATUS_PENDING_SHIP.equals(order.getOrderStatus()))
        {
            throw new ServiceException(OrderConstants.MSG_CANNOT_SHIP);
        }
    }

    public static void assertShipped(BizOrder order)
    {
        if (order == null || !OrderConstants.STATUS_SHIPPED.equals(order.getOrderStatus()))
        {
            throw new ServiceException(OrderConstants.MSG_CANNOT_LOGISTICS);
        }
    }

    public static void assertClosed(BizOrder order)
    {
        if (order == null || !OrderConstants.STATUS_CLOSED.equals(order.getOrderStatus()))
        {
            throw new ServiceException(OrderConstants.MSG_CANNOT_DELETE);
        }
    }

    public static void validateShipDto(OrderShipDTO dto)
    {
        if (dto == null || dto.getShipTime() == null)
        {
            throw new ServiceException("请填写发货时间");
        }
        if (StringUtils.isEmpty(dto.getDeliveryType()))
        {
            throw new ServiceException("请选择配送方式");
        }
        if (OrderConstants.DELIVERY_LOGISTICS.equals(dto.getDeliveryType()))
        {
            if (StringUtils.isEmpty(dto.getLogisticsCompany()) || StringUtils.isEmpty(dto.getTrackingNo()))
            {
                throw new ServiceException(OrderConstants.MSG_SHIP_LOGISTICS_REQUIRED);
            }
            return;
        }
        if (OrderConstants.DELIVERY_MERCHANT.equals(dto.getDeliveryType()))
        {
            if (StringUtils.isEmpty(dto.getVehicleNo()) || StringUtils.isEmpty(dto.getCourierName())
                    || StringUtils.isEmpty(dto.getCourierMobile()))
            {
                throw new ServiceException(OrderConstants.MSG_SHIP_MERCHANT_REQUIRED);
            }
            return;
        }
        throw new ServiceException("配送方式无效");
    }

    public static void validateTraceDto(OrderLogisticsDTO dto)
    {
        if (dto == null || dto.getTraceTime() == null)
        {
            throw new ServiceException(OrderConstants.MSG_TRACE_TIME_REQUIRED);
        }
        if (StringUtils.isEmpty(dto.getContent()))
        {
            throw new ServiceException(OrderConstants.MSG_TRACE_CONTENT_REQUIRED);
        }
    }

    public static void validateDeliveredDto(OrderDeliveredDTO dto)
    {
        if (dto == null || dto.getTraceTime() == null)
        {
            throw new ServiceException(OrderConstants.MSG_TRACE_TIME_REQUIRED);
        }
        if (StringUtils.isEmpty(dto.getContent()))
        {
            throw new ServiceException(OrderConstants.MSG_TRACE_CONTENT_REQUIRED);
        }
    }

    public static String buildShipTraceContent(OrderShipDTO dto)
    {
        if (OrderConstants.DELIVERY_LOGISTICS.equals(dto.getDeliveryType()))
        {
            return dto.getLogisticsCompany() + " " + dto.getTrackingNo();
        }
        return "车辆" + dto.getVehicleNo() + " 配送员" + dto.getCourierName() + " " + dto.getCourierMobile();
    }

    /** 发货时将店铺配送设置快照写入订单更新对象 */
    public static void applyShipperSnapshot(BizOrder update, ShopDeliverySettingVO delivery)
    {
        if (delivery == null || StringUtils.isEmpty(StringUtils.trim(delivery.getShipperName())))
        {
            throw new ServiceException(OrderConstants.MSG_SHIP_DELIVERY_SETTING_REQUIRED);
        }
        if (StringUtils.isEmpty(StringUtils.trim(delivery.getShipperMobile())))
        {
            throw new ServiceException(OrderConstants.MSG_SHIP_DELIVERY_SETTING_REQUIRED);
        }
        if (delivery.getRegion() == null || StringUtils.isEmpty(StringUtils.trim(delivery.getRegion().getRegionCode())))
        {
            throw new ServiceException(OrderConstants.MSG_SHIP_DELIVERY_SETTING_REQUIRED);
        }
        if (StringUtils.isEmpty(StringUtils.trim(delivery.getDetailAddress())))
        {
            throw new ServiceException(OrderConstants.MSG_SHIP_DELIVERY_SETTING_REQUIRED);
        }
        update.setShipperName(delivery.getShipperName().trim());
        update.setShipperMobile(delivery.getShipperMobile().trim());
        update.setShipRegionCode(delivery.getRegion().getRegionCode().trim());
        update.setShipRegionName(formatShipRegionName(delivery.getRegion()));
        update.setShipDetailAddress(delivery.getDetailAddress().trim());
    }

    public static String formatShipRegionName(DeliveryRegionVO region)
    {
        if (region == null)
        {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        appendRegionPart(sb, region.getProvinceName());
        appendRegionPart(sb, region.getCityName());
        appendRegionPart(sb, region.getDistrictName());
        return sb.toString();
    }

    private static void appendRegionPart(StringBuilder sb, String part)
    {
        if (StringUtils.isEmpty(part))
        {
            return;
        }
        if (sb.length() > 0)
        {
            sb.append(" / ");
        }
        sb.append(part.trim());
    }
}
