Order.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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\payment\Method as PaymentMethodEnum;
  17. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  18. use app\common\enum\order\ReceiptStatus as ReceiptStatusEnum;
  19. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  20. use app\common\library\helper;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. use think\model\relation\HasOne;
  25. use think\model\relation\HasMany;
  26. use think\model\relation\BelongsTo;
  27. /**
  28. * 订单模型
  29. * Class Order
  30. * @package app\common\model
  31. */
  32. class Order extends BaseModel
  33. {
  34. // 定义表名
  35. protected $name = 'order';
  36. // 定义表名(外部引用)
  37. public static string $tableName = 'order';
  38. // 定义主键
  39. protected $pk = 'order_id';
  40. // 定义别名
  41. protected string $alias = 'order';
  42. /**
  43. * 追加字段
  44. * @var array
  45. */
  46. protected $append = [
  47. 'state_text', // 售后单状态文字描述
  48. ];
  49. /**
  50. * 订单商品列表
  51. * @return HasMany
  52. */
  53. public function goods(): HasMany
  54. {
  55. $module = self::getCalledModule();
  56. return $this->hasMany("app\\{$module}\\model\\OrderGoods")->withoutField('content');
  57. }
  58. /**
  59. * 关联订单发货单
  60. * @return hasMany
  61. */
  62. public function delivery(): HasMany
  63. {
  64. $module = self::getCalledModule();
  65. return $this->hasMany("app\\{$module}\\model\\order\\Delivery");
  66. }
  67. /**
  68. * 关联订单收货地址表
  69. * @return HasOne
  70. */
  71. public function address(): HasOne
  72. {
  73. $module = self::getCalledModule();
  74. return $this->hasOne("app\\{$module}\\model\\OrderAddress");
  75. }
  76. /**
  77. * 关联用户表
  78. * @return BelongsTo
  79. */
  80. public function user(): BelongsTo
  81. {
  82. $module = self::getCalledModule();
  83. return $this->belongsTo("app\\{$module}\\model\\User");
  84. }
  85. /**
  86. * 关联物流公司表 (仅用于兼容旧物流数据)
  87. * @return BelongsTo
  88. */
  89. public function express(): BelongsTo
  90. {
  91. $module = self::getCalledModule();
  92. return $this->belongsTo("app\\{$module}\\model\\Express");
  93. }
  94. /**
  95. * 关联模型:第三方交易记录
  96. * @return BelongsTo
  97. */
  98. public function trade(): BelongsTo
  99. {
  100. $module = self::getCalledModule();
  101. return $this->belongsTo("app\\{$module}\\model\\PaymentTrade", 'trade_id', 'trade_id');
  102. }
  103. /**
  104. * 获取器:订单状态文字描述
  105. * @param $value
  106. * @param $data
  107. * @return string
  108. */
  109. public function getStateTextAttr($value, $data): string
  110. {
  111. // 订单状态
  112. if ($data['order_status'] != OrderStatusEnum::NORMAL) {
  113. return OrderStatusEnum::data()[$data['order_status']]['name'];
  114. }
  115. // 付款状态
  116. if ($data['pay_status'] == PayStatusEnum::PENDING) {
  117. return '待支付';
  118. }
  119. // 发货状态
  120. if ($data['delivery_status'] != DeliveryStatusEnum::DELIVERED) {
  121. $enum = [DeliveryStatusEnum::NOT_DELIVERED => '待发货', DeliveryStatusEnum::PART_DELIVERED => '部分发货'];
  122. return $enum[$data['delivery_status']];
  123. }
  124. // 收货状态
  125. if ($data['receipt_status'] == ReceiptStatusEnum::NOT_RECEIVED) {
  126. return '待收货';
  127. }
  128. return $value;
  129. }
  130. /**
  131. * 获取器:订单金额(含优惠折扣)
  132. * @param $value
  133. * @param $data
  134. * @return string
  135. */
  136. public function getOrderPriceAttr($value, $data): string
  137. {
  138. // 兼容旧数据:订单金额
  139. if ($value == 0) {
  140. return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']);
  141. }
  142. return $value;
  143. }
  144. /**
  145. * 获取器:改价金额(差价)
  146. * @param $value
  147. * @return array
  148. */
  149. public function getUpdatePriceAttr($value): array
  150. {
  151. return [
  152. 'symbol' => $value < 0 ? '-' : '+',
  153. 'value' => sprintf('%.2f', abs((float)$value))
  154. ];
  155. }
  156. /**
  157. * 获取器:付款时间
  158. * @param $value
  159. * @return false|string
  160. */
  161. public function getPayTimeAttr($value)
  162. {
  163. return format_time($value);
  164. }
  165. /**
  166. * 获取器:发货时间
  167. * @param $value
  168. * @return false|string
  169. */
  170. public function getDeliveryTimeAttr($value)
  171. {
  172. return format_time($value);
  173. }
  174. /**
  175. * 获取器:收货时间
  176. * @param $value
  177. * @return false|string
  178. */
  179. public function getReceiptTimeAttr($value)
  180. {
  181. return format_time($value);
  182. }
  183. /**
  184. * 获取器:来源记录的参数
  185. * @param $json
  186. * @return array
  187. */
  188. public function getOrderSourceDataAttr($json): array
  189. {
  190. return $json ? helper::jsonDecode($json) : [];
  191. }
  192. /**
  193. * 修改器:来源记录的参数
  194. * @param array $data
  195. * @return string
  196. */
  197. public function setOrderSourceDataAttr(array $data): string
  198. {
  199. return helper::jsonEncode($data);
  200. }
  201. /**
  202. * 生成订单号
  203. * @return string
  204. */
  205. public function orderNo(): string
  206. {
  207. return OrderService::createOrderNo();
  208. }
  209. /**
  210. * 订单详情
  211. * @param $where
  212. * @param array $with
  213. * @return static|array|null
  214. */
  215. public static function detail($where, array $with = [])
  216. {
  217. is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
  218. return self::get($filter, $with);
  219. }
  220. /**
  221. * 待支付订单详情
  222. * @param string $orderNo 订单号
  223. * @return null|static
  224. */
  225. public static function getPayDetail(string $orderNo): ?Order
  226. {
  227. $where = ['order_no' => $orderNo, 'is_delete' => 0];
  228. return static::detail($where, ['goods', 'user']);
  229. }
  230. /**
  231. * 批量获取订单列表
  232. * @param array $orderIds
  233. * @param array $with
  234. * @return array
  235. * @throws DataNotFoundException
  236. * @throws DbException
  237. * @throws ModelNotFoundException
  238. */
  239. public function getListByIds(array $orderIds, array $with = []): array
  240. {
  241. $data = $this->getListByInArray('order_id', $orderIds, $with);
  242. return helper::arrayColumn2Key($data, 'order_id');
  243. }
  244. /**
  245. * 批量获取订单列表
  246. * @param $field
  247. * @param $data
  248. * @param array $with
  249. * @return \think\Collection
  250. * @throws \think\db\exception\DataNotFoundException
  251. * @throws \think\db\exception\DbException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. */
  254. private function getListByInArray($field, $data, array $with = []): \think\Collection
  255. {
  256. return $this->with($with)
  257. ->where($field, 'in', $data)
  258. ->where('is_delete', '=', 0)
  259. ->select();
  260. }
  261. /**
  262. * 根据订单号批量查询
  263. * @param $orderNos
  264. * @param array $with
  265. * @return \think\Collection
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\DbException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. */
  270. public function getListByOrderNos($orderNos, array $with = []): \think\Collection
  271. {
  272. return $this->getListByInArray('order_no', $orderNos, $with);
  273. }
  274. /**
  275. * 批量更新订单
  276. * @param $orderIds
  277. * @param $data
  278. * @return bool|false
  279. */
  280. public function onBatchUpdate($orderIds, $data): bool
  281. {
  282. return static::updateBase($data, [['order_id', 'in', $orderIds]]);
  283. }
  284. /**
  285. * 更新订单来源记录ID
  286. * @param int $orderId
  287. * @param int $soureId
  288. * @return bool
  289. */
  290. public static function updateOrderSourceId(int $orderId, int $soureId): bool
  291. {
  292. // updateOne
  293. return static::updateBase(['order_source_id' => $soureId], $orderId);
  294. }
  295. /**
  296. * 记录是否已同步微信小程序发货信息管理
  297. * @param int $orderId
  298. * @param bool $status
  299. * @return bool
  300. */
  301. public static function updateSyncWeixinShipping(int $orderId, bool $status): bool
  302. {
  303. // updateOne
  304. return static::updateBase(['sync_weixin_shipping' => (int)$status], $orderId);
  305. }
  306. }