Order.php 7.2 KB

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