Order.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\service\Order as OrderService;
  15. use app\common\enum\order\PayStatus as PayStatusEnum;
  16. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  17. use app\common\enum\order\ReceiptStatus as ReceiptStatusEnum;
  18. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  19. use app\common\library\helper;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\model\relation\HasOne;
  24. use think\model\relation\HasMany;
  25. use think\model\relation\BelongsTo;
  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 string $tableName = 'order';
  37. // 定义主键
  38. protected $pk = 'order_id';
  39. // 定义别名
  40. protected string $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 hasMany
  60. */
  61. public function delivery(): HasMany
  62. {
  63. $module = self::getCalledModule();
  64. return $this->hasMany("app\\{$module}\\model\\order\\Delivery");
  65. }
  66. /**
  67. * 关联订单收货地址表
  68. * @return HasOne
  69. */
  70. public function address(): HasOne
  71. {
  72. $module = self::getCalledModule();
  73. return $this->hasOne("app\\{$module}\\model\\OrderAddress");
  74. }
  75. /**
  76. * 关联用户表
  77. * @return BelongsTo
  78. */
  79. public function user(): BelongsTo
  80. {
  81. $module = self::getCalledModule();
  82. return $this->belongsTo("app\\{$module}\\model\\User");
  83. }
  84. /**
  85. * 关联物流公司表 (仅用于兼容旧物流数据)
  86. * @return BelongsTo
  87. */
  88. public function express(): BelongsTo
  89. {
  90. $module = self::getCalledModule();
  91. return $this->belongsTo("app\\{$module}\\model\\Express");
  92. }
  93. /**
  94. * 关联模型:第三方交易记录
  95. * @return BelongsTo
  96. */
  97. public function trade(): BelongsTo
  98. {
  99. $module = self::getCalledModule();
  100. return $this->belongsTo("app\\{$module}\\model\\PaymentTrade", 'trade_id', 'trade_id');
  101. }
  102. /**
  103. * 获取器:订单状态文字描述
  104. * @param $value
  105. * @param $data
  106. * @return string
  107. */
  108. public function getStateTextAttr($value, $data): string
  109. {
  110. // 订单状态
  111. if ($data['order_status'] != OrderStatusEnum::NORMAL) {
  112. return OrderStatusEnum::data()[$data['order_status']]['name'];
  113. }
  114. // 付款状态
  115. if ($data['pay_status'] == PayStatusEnum::PENDING) {
  116. return '待支付';
  117. }
  118. // 发货状态
  119. if ($data['delivery_status'] != DeliveryStatusEnum::DELIVERED) {
  120. $enum = [DeliveryStatusEnum::NOT_DELIVERED => '待发货', DeliveryStatusEnum::PART_DELIVERED => '部分发货'];
  121. return $enum[$data['delivery_status']];
  122. }
  123. // 收货状态
  124. if ($data['receipt_status'] == ReceiptStatusEnum::NOT_RECEIVED) {
  125. return '待收货';
  126. }
  127. return $value;
  128. }
  129. /**
  130. * 获取器:订单金额(含优惠折扣)
  131. * @param $value
  132. * @param $data
  133. * @return string
  134. */
  135. public function getOrderPriceAttr($value, $data): string
  136. {
  137. // 兼容旧数据:订单金额
  138. if ($value == 0) {
  139. return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']);
  140. }
  141. return $value;
  142. }
  143. /**
  144. * 获取器:改价金额(差价)
  145. * @param $value
  146. * @return array
  147. */
  148. public function getUpdatePriceAttr($value): array
  149. {
  150. return [
  151. 'symbol' => $value < 0 ? '-' : '+',
  152. 'value' => sprintf('%.2f', abs((float)$value))
  153. ];
  154. }
  155. /**
  156. * 获取器:付款时间
  157. * @param $value
  158. * @return false|string
  159. */
  160. public function getPayTimeAttr($value)
  161. {
  162. return format_time($value);
  163. }
  164. /**
  165. * 获取器:发货时间
  166. * @param $value
  167. * @return false|string
  168. */
  169. public function getDeliveryTimeAttr($value)
  170. {
  171. return format_time($value);
  172. }
  173. /**
  174. * 获取器:收货时间
  175. * @param $value
  176. * @return false|string
  177. */
  178. public function getReceiptTimeAttr($value)
  179. {
  180. return format_time($value);
  181. }
  182. /**
  183. * 获取器:来源记录的参数
  184. * @param $json
  185. * @return array
  186. */
  187. public function getOrderSourceDataAttr($json): array
  188. {
  189. return $json ? helper::jsonDecode($json) : [];
  190. }
  191. /**
  192. * 修改器:来源记录的参数
  193. * @param array $data
  194. * @return string
  195. */
  196. public function setOrderSourceDataAttr(array $data): string
  197. {
  198. return helper::jsonEncode($data);
  199. }
  200. /**
  201. * 生成订单号
  202. * @return string
  203. */
  204. public function orderNo(): string
  205. {
  206. return OrderService::createOrderNo();
  207. }
  208. /**
  209. * 订单详情
  210. * @param $where
  211. * @param array $with
  212. * @return static|array|null
  213. */
  214. public static function detail($where, array $with = [])
  215. {
  216. is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
  217. return self::get($filter, $with);
  218. }
  219. /**
  220. * 批量获取订单列表
  221. * @param array $orderIds
  222. * @param array $with
  223. * @return array
  224. * @throws DataNotFoundException
  225. * @throws DbException
  226. * @throws ModelNotFoundException
  227. */
  228. public function getListByIds(array $orderIds, array $with = []): array
  229. {
  230. $data = $this->getListByInArray('order_id', $orderIds, $with);
  231. return helper::arrayColumn2Key($data, 'order_id');
  232. }
  233. /**
  234. * 批量获取订单列表
  235. * @param $field
  236. * @param $data
  237. * @param array $with
  238. * @return \think\Collection
  239. * @throws \think\db\exception\DataNotFoundException
  240. * @throws \think\db\exception\DbException
  241. * @throws \think\db\exception\ModelNotFoundException
  242. */
  243. private function getListByInArray($field, $data, array $with = []): \think\Collection
  244. {
  245. return $this->with($with)
  246. ->where($field, 'in', $data)
  247. ->where('is_delete', '=', 0)
  248. ->select();
  249. }
  250. /**
  251. * 根据订单号批量查询
  252. * @param $orderNos
  253. * @param array $with
  254. * @return \think\Collection
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function getListByOrderNos($orderNos, array $with = []): \think\Collection
  260. {
  261. return $this->getListByInArray('order_no', $orderNos, $with);
  262. }
  263. /**
  264. * 批量更新订单
  265. * @param $orderIds
  266. * @param $data
  267. * @return bool|false
  268. */
  269. public function onBatchUpdate($orderIds, $data): bool
  270. {
  271. return static::updateBase($data, [['order_id', 'in', $orderIds]]);
  272. }
  273. }