Order.php 7.0 KB

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