Order.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use app\common\model\store\shop\Order as ShopOrder;
  15. use app\common\service\Order as OrderService;
  16. use app\common\service\order\Complete as OrderCompleteService;
  17. use app\common\enum\OrderType as OrderTypeEnum;
  18. use app\common\enum\order\PayStatus as PayStatusEnum;
  19. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  20. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  21. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  22. use app\common\library\helper;
  23. use think\model\relation\BelongsTo;
  24. use think\model\relation\HasMany;
  25. use think\model\relation\HasOne;
  26. /**
  27. * 订单模型
  28. * Class Order
  29. * @package app\common\model
  30. */
  31. class Order extends BaseModel
  32. {
  33. // 定义表名
  34. protected $name = 'order';
  35. // 定义表名(外部引用)
  36. public static $tableName = 'order';
  37. // 定义主键
  38. protected $pk = 'order_id';
  39. // 定义别名
  40. protected $alias = 'order';
  41. /**
  42. * 追加字段
  43. * @var array
  44. */
  45. protected $append = [
  46. 'state_text', // 售后单状态文字描述
  47. ];
  48. /**
  49. * 订单商品列表
  50. * @return HasMany
  51. */
  52. public function goods(): HasMany
  53. {
  54. $module = self::getCalledModule();
  55. return $this->hasMany("app\\{$module}\\model\\OrderGoods")->withoutField('content');
  56. }
  57. /**
  58. * 关联订单收货地址表
  59. * @return HasOne
  60. */
  61. public function address(): HasOne
  62. {
  63. $module = self::getCalledModule();
  64. return $this->hasOne("app\\{$module}\\model\\OrderAddress");
  65. }
  66. /**
  67. * 关联用户表
  68. * @return BelongsTo
  69. */
  70. public function user(): BelongsTo
  71. {
  72. $module = self::getCalledModule();
  73. return $this->belongsTo("app\\{$module}\\model\\User");
  74. }
  75. /**
  76. * 关联物流公司表
  77. * @return BelongsTo
  78. */
  79. public function express(): BelongsTo
  80. {
  81. $module = self::getCalledModule();
  82. return $this->belongsTo("app\\{$module}\\model\\Express");
  83. }
  84. /**
  85. * 订单状态文字描述
  86. * @param $value
  87. * @param array $data
  88. * @return string
  89. */
  90. public function getStateTextAttr($value, array $data): string
  91. {
  92. // 订单状态
  93. // if (in_array($data['order_status'], [OrderStatusEnum::CANCELLED, OrderStatusEnum::COMPLETED])) {
  94. // $orderStatus = [OrderStatusEnum::CANCELLED => '已取消', OrderStatusEnum::COMPLETED => '已完成'];
  95. // return $orderStatus[$data['order_status']];
  96. // }
  97. // 订单状态
  98. if (isset($data['order_status']) && $data['order_status'] != OrderStatusEnum::NORMAL) {
  99. return OrderStatusEnum::data()[$data['order_status']]['name'];
  100. }
  101. // 付款状态
  102. if (isset($data['pay_status']) && $data['pay_status'] == PayStatusEnum::PENDING) {
  103. return '待支付';
  104. }
  105. // 订单类型:单独购买
  106. if (isset($data['delivery_status']) && $data['delivery_status'] == 10) {
  107. return '待发货';
  108. }
  109. if (isset($data['receipt_status']) && $data['receipt_status'] == 10) {
  110. return '待收货';
  111. }
  112. return strval($value);
  113. }
  114. /**
  115. * 获取器:订单金额(含优惠折扣)
  116. * @param $value
  117. * @param $data
  118. * @return string
  119. */
  120. public function getOrderPriceAttr($value, $data): string
  121. {
  122. // 兼容旧数据:订单金额
  123. if ($value == 0) {
  124. return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']);
  125. }
  126. return $value;
  127. }
  128. /**
  129. * 改价金额(差价)
  130. * @param $value
  131. * @return array
  132. */
  133. public function getUpdatePriceAttr($value): array
  134. {
  135. return [
  136. 'symbol' => $value < 0 ? '-' : '+',
  137. 'value' => sprintf('%.2f', abs((float)$value))
  138. ];
  139. }
  140. /**
  141. * 付款时间
  142. * @param $value
  143. * @return false|string
  144. */
  145. public function getPayTimeAttr($value)
  146. {
  147. return format_time($value);
  148. }
  149. /**
  150. * 发货时间
  151. * @param $value
  152. * @return false|string
  153. */
  154. public function getDeliveryTimeAttr($value)
  155. {
  156. return format_time($value);
  157. }
  158. /**
  159. * 收货时间
  160. * @param $value
  161. * @return false|string
  162. */
  163. public function getReceiptTimeAttr($value)
  164. {
  165. return format_time($value);
  166. }
  167. /**
  168. * 生成订单号
  169. * @return string
  170. */
  171. public function orderNo(): string
  172. {
  173. return OrderService::createOrderNo();
  174. }
  175. /**
  176. * 订单详情
  177. * @param $where
  178. * @param array $with
  179. * @return array|null|static
  180. */
  181. public static function detail($where, array $with = [])
  182. {
  183. is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
  184. return self::get($filter, $with);
  185. }
  186. /**
  187. * 批量获取订单列表
  188. * @param array $orderIds
  189. * @param array $with
  190. * @return array
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. */
  195. public function getListByIds(array $orderIds, array $with = []): array
  196. {
  197. $data = $this->getListByInArray('order_id', $orderIds, $with);
  198. return helper::arrayColumn2Key($data, 'order_id');
  199. }
  200. /**
  201. * 批量获取订单列表
  202. * @param $field
  203. * @param $data
  204. * @param array $with
  205. * @return \think\Collection
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\DbException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. private function getListByInArray($field, $data, array $with = []): \think\Collection
  211. {
  212. return $this->with($with)
  213. ->where($field, 'in', $data)
  214. ->where('is_delete', '=', 0)
  215. ->select();
  216. }
  217. /**
  218. * 根据订单号批量查询
  219. * @param $orderNos
  220. * @param array $with
  221. * @return \think\Collection
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. public function getListByOrderNos($orderNos, array $with = []): \think\Collection
  227. {
  228. return $this->getListByInArray('order_no', $orderNos, $with);
  229. }
  230. /**
  231. * 批量更新订单
  232. * @param $orderIds
  233. * @param $data
  234. * @return bool|false
  235. */
  236. public function onBatchUpdate($orderIds, $data): bool
  237. {
  238. return static::updateBase($data, [['order_id', 'in', $orderIds]]);
  239. }
  240. }