OrderStatusUtil.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace common\components\delivery\platform\shansong;
  3. /**
  4. * Class SystemStatusUtil
  5. * 闪送配送平台状态转换工具类 (转换为主订单 status)
  6. * @package common\components\delivery\platform\shansong
  7. */
  8. class OrderStatusUtil
  9. {
  10. /**
  11. * 闪送平台订单状态到系统 status 的转换映射
  12. *
  13. * 闪送平台状态码说明:
  14. * 20 - 派单中(转单改派中)
  15. * 30 - 待取货(已就位)
  16. * 40 - 闪送中(申请取消中、物品送回中、取消单客服介入中)
  17. * 50 - 已完成(已退款)
  18. * 60 - 已取消
  19. *
  20. * 系统内 status 状态码说明:
  21. * 1 - 待付款
  22. * 2 - 待配送
  23. * 3 - 配送中
  24. * 4 - 已完成
  25. * 5 - 已取消
  26. */
  27. const STATUS_MAPPING = [
  28. 20 => 3, // 派单中 -> 配送中
  29. 30 => 3, // 待取货 -> 配送中
  30. 40 => 3, // 闪送中 -> 配送中
  31. 50 => 4, // 已完成 -> 已完成
  32. 60 => 2, // 已取消 -> 待配送(配送的取消不等于订单的取消,由于 status 带有配送状态,所以改为:待配送)
  33. ];
  34. /**
  35. * 将闪送平台的订单状态转换为系统内的 status
  36. *
  37. * @param int $shansongStatus 闪送平台的订单状态码
  38. * @param int|null $subStatus 闪送平台的订单子状态码(可选)
  39. * @return int 转换后的系统 status 状态码
  40. * @throws \Exception 当状态码无法转换时抛出异常
  41. */
  42. public static function convertStatus($shansongStatus, $subStatus = null)
  43. {
  44. $shansongStatus = (int)$shansongStatus;
  45. // 此处不需要子状态映射,因为系统 status 状态较粗粒度
  46. // 20(派单中), 30(待取货) 都对应 3(配送中)
  47. // 40(闪送中) 对应 3(配送中)
  48. if (!isset(self::STATUS_MAPPING[$shansongStatus])) {
  49. throw new \Exception("无效的闪送订单状态码:{$shansongStatus}");
  50. }
  51. return self::STATUS_MAPPING[$shansongStatus];
  52. }
  53. /**
  54. * 获取闪送平台的状态描述
  55. *
  56. * @param int $shansongStatus 闪送平台的订单状态码
  57. * @param int|null $subStatus 闪送平台的订单子状态码(可选)
  58. * @return string 状态描述
  59. */
  60. public static function getStatusDesc($shansongStatus, $subStatus = null)
  61. {
  62. $mainStatusDescMap = [
  63. 20 => '派单中',
  64. 30 => '待取货',
  65. 40 => '闪送中',
  66. 50 => '已完成',
  67. 60 => '已取消',
  68. ];
  69. $subStatusDescMap = [
  70. 20 => [1 => '派单中', 2 => '转单改派中'],
  71. 30 => [1 => '待取货', 2 => '已就位'],
  72. 40 => [1 => '闪送中', 2 => '申请取消中', 3 => '物品送回中', 4 => '取消单客服介入中'],
  73. 50 => [1 => '已完成', 2 => '已退款'],
  74. ];
  75. $mainDesc = $mainStatusDescMap[$shansongStatus] ?? '未知状态';
  76. if ($subStatus !== null && isset($subStatusDescMap[$shansongStatus][$subStatus])) {
  77. return $subStatusDescMap[$shansongStatus][$subStatus];
  78. }
  79. return $mainDesc;
  80. }
  81. /**
  82. * 获取系统 status 的状态描述
  83. *
  84. * @param int $status 系统的 status 状态码
  85. * @return string 状态描述
  86. */
  87. public static function getSystemStatusDesc($status)
  88. {
  89. $statusDescMap = [
  90. 1 => '待付款',
  91. 2 => '待配送',
  92. 3 => '配送中',
  93. 4 => '已完成',
  94. 5 => '已取消',
  95. ];
  96. return $statusDescMap[$status] ?? '未知状态';
  97. }
  98. }