Order.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\common\service;
  3. use app\common\enum\OrderType as OrderTypeEnum;
  4. /**
  5. * 订单服务类
  6. * Class Order
  7. * @package app\common\service
  8. */
  9. class Order
  10. {
  11. /**
  12. * 订单模型类
  13. * @var array
  14. */
  15. private static $orderModelClass = [
  16. OrderTypeEnum::MASTER => 'app\common\model\Order',
  17. OrderTypeEnum::SHARING => 'app\common\model\sharing\Order'
  18. ];
  19. /**
  20. * 生成订单号
  21. * @return string
  22. */
  23. public static function createOrderNo()
  24. {
  25. return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  26. }
  27. /**
  28. * 整理订单列表 (根据order_type获取不同类型的订单记录)
  29. * @param \think\Collection|\think\Paginator $data 数据源
  30. * @param string $orderIndex 订单记录的索引
  31. * @param array $with 关联查询
  32. * @return mixed
  33. */
  34. public static function getOrderList($data, $orderIndex = 'order', $with = [])
  35. {
  36. // 整理订单id
  37. $orderIds = [];
  38. foreach ($data as &$item) {
  39. $orderIds[$item['order_type']['value']][] = $item['order_id'];
  40. }
  41. // 获取订单列表
  42. $orderList = [];
  43. foreach ($orderIds as $orderType => $values) {
  44. $model = self::model($orderType);
  45. $orderList[$orderType] = $model->getListByIds($values, $with);
  46. }
  47. // 格式化到数据源
  48. foreach ($data as $key => &$item) {
  49. if (!isset($orderList[$item['order_type']['value']][$item['order_id']])) {
  50. // todo: 兼容错误数据
  51. $item->delete();
  52. unset($data[$key]);
  53. continue;
  54. }
  55. $item[$orderIndex] = $orderList[$item['order_type']['value']][$item['order_id']];
  56. }
  57. return $data;
  58. }
  59. /**
  60. * 获取订单详情 (根据order_type获取不同类型的订单详情)
  61. * @param $orderId
  62. * @param int $orderType
  63. * @return mixed
  64. */
  65. public static function getOrderDetail($orderId, $orderType = OrderTypeEnum::MASTER)
  66. {
  67. $model = self::model($orderType);
  68. return $model::detail($orderId);
  69. }
  70. /**
  71. * 根据订单类型获取对应的订单模型类
  72. * @param int $orderType
  73. * @return mixed
  74. */
  75. public static function model($orderType = OrderTypeEnum::MASTER)
  76. {
  77. static $models = [];
  78. if (!isset($models[$orderType])) {
  79. $models[$orderType] = new self::$orderModelClass[$orderType];
  80. }
  81. return $models[$orderType];
  82. }
  83. }