Order.php 2.8 KB

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