OrderOper.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace addons\shopro\service\order;
  3. use app\admin\model\shopro\user\User;
  4. use app\admin\model\shopro\order\Order;
  5. use app\admin\model\shopro\order\OrderItem;
  6. use app\admin\model\shopro\order\Action;
  7. use app\admin\model\shopro\goods\Comment;
  8. use app\admin\model\shopro\activity\GiftLog;
  9. use app\admin\model\shopro\activity\Order as ActivityOrderModel;
  10. class OrderOper
  11. {
  12. public function __construct()
  13. {
  14. }
  15. /**
  16. * 订单自动关闭`
  17. *
  18. * @param \think\Model $order
  19. * @param \think\Model|null $user
  20. * @param string $type
  21. * @return void
  22. */
  23. public function close($order, $user = null, $type = 'user', $msg = '', $ext = [])
  24. {
  25. $order->status = Order::STATUS_CLOSED;
  26. $order->ext = array_merge($order->ext, $ext, ['closed_time' => time()]); // 关闭时间
  27. $order->allowField(true)->save();
  28. Action::add($order, null, $user, $type, ($msg ? : $this->getOperText($type) . '关闭订单'));
  29. // 订单自动关闭之后 行为 返还用户优惠券,积分
  30. $data = ['order' => $order];
  31. \think\Hook::listen('order_close_after', $data);
  32. return $order;
  33. }
  34. /**
  35. * 订单取消
  36. *
  37. * @param object $order
  38. * @param object $user
  39. * @param string $type
  40. * @return object
  41. */
  42. public function cancel($order, $user = null, $type = 'user', $msg = '')
  43. {
  44. $order->status = Order::STATUS_CANCEL; // 取消订单
  45. $order->ext = array_merge($order->ext, ['cancel_time' => time()]); // 取消时间
  46. $order->allowField(true)->save();
  47. Action::add($order, null, $user, $type, ($msg ?: $this->getOperText($type) . '取消订单'));
  48. // 订单取消,退回库存,退回优惠券积分,等
  49. $data = ['order' => $order];
  50. \think\Hook::listen('order_cancel_after', $data);
  51. return $order;
  52. }
  53. /**
  54. * 申请全额退款
  55. *
  56. * @param object $order
  57. * @param object $user
  58. * @param string $type
  59. * @return object
  60. */
  61. public function applyRefund($order, $user, $type = 'user',$reason = '')
  62. {
  63. $items = OrderItem::where('order_id', $order->id)->lock(true)->select();
  64. foreach ($items as $key => $item) {
  65. if (in_array($item['refund_status'], [
  66. OrderItem::REFUND_STATUS_AGREE,
  67. OrderItem::REFUND_STATUS_COMPLETED,
  68. ])) {
  69. error_stop('订单有退款,不可申请');
  70. }
  71. if ($item['dispatch_status'] != OrderItem::DISPATCH_STATUS_NOSEND) {
  72. error_stop('订单已发货,不可申请');
  73. }
  74. }
  75. $order->apply_refund_status = Order::APPLY_REFUND_STATUS_APPLY; // 申请退款
  76. $order->ext = array_merge($order->ext, ['apply_refund_time' => time()]); // 申请时间
  77. $order->save();
  78. Action::add($order, null, $user, $type, $this->getOperText($type) . '申请全额退款。' . $reason);
  79. // 订单申请全额退款 orderApplyRefundAfter
  80. $data = ['order' => $order, 'user' => $user];
  81. \think\Hook::listen('order_apply_refund_after', $data);
  82. return $order;
  83. }
  84. /**
  85. * 确认收货
  86. *
  87. * @param object $order
  88. * @param array $itemIds
  89. * @param object|null $user
  90. * @param string $type
  91. * @return object
  92. */
  93. public function confirm($order, $itemIds = [], $user = null, $type = 'user')
  94. {
  95. $items = OrderItem::canConfirm()->where('order_id', $order->id)->where(function ($query) use ($itemIds) {
  96. if ($itemIds) {
  97. // 只确认收货传入的 ids
  98. $query->whereIn('id', $itemIds);
  99. }
  100. })->lock(true)->select();
  101. if (!$items) {
  102. error_stop('订单已确认收货,请不要重复确认收货');
  103. }
  104. foreach ($items as $item) {
  105. $item->ext = array_merge($item->ext, ['confirm_time' => time()]);
  106. $item->dispatch_status = OrderItem::DISPATCH_STATUS_GETED; // 确认收货
  107. $item->save();
  108. Action::add($order, $item, $user, $type, $this->getOperText($type) . '确认收货');
  109. // 订单确认收货后
  110. $data = ['order' => $order, 'item' => $item];
  111. \think\Hook::listen('order_confirm_after', $data);
  112. }
  113. return $order;
  114. }
  115. /**
  116. * 拒收收货
  117. *
  118. * @param object $order
  119. * @param array $itemIds
  120. * @param object|null $user
  121. * @param string $type
  122. * @return object
  123. */
  124. public function refuse($order, $user = null, $type = 'user')
  125. {
  126. $items = OrderItem::canConfirm()->where('order_id', $order->id)->lock(true)->select();
  127. if (!$items) {
  128. error_stop('没有可拒收的商品');
  129. }
  130. foreach ($items as $item) {
  131. $item->ext = array_merge($item->ext, ['refuse_time' => time()]);
  132. $item->dispatch_status = OrderItem::DISPATCH_STATUS_REFUSE; // 拒收
  133. $item->save();
  134. Action::add($order, $item, $user, $type, $this->getOperText($type) . '操作,用户拒绝收货');
  135. }
  136. // 订单拒收后事件
  137. $data = ['order' => $order];
  138. \think\Hook::listen('order_refuse_after', $data);
  139. return $order;
  140. }
  141. /**
  142. * 评价 (根据 comments 中的数据进行评价, 可以之评价一个(系统自动评价时候))
  143. *
  144. * @param object $order
  145. * @param object|null $user
  146. * @param string $type
  147. * @return object
  148. */
  149. public function comment($order, $comments, $user = null, $type = 'user')
  150. {
  151. // 评价的orderItem id
  152. $comments = array_column($comments, null, 'item_id');
  153. $itemIds = array_keys($comments);
  154. $items = OrderItem::canComment()->where('order_id', $order->id)->lock(true)->select();
  155. if (!$items) {
  156. if ($type == 'system') {
  157. return $order; // 系统自动评价时检测到用户已经自己评价,这里直接返回,不抛出异常
  158. }
  159. error_stop('订单已评价,请不要重复评价');
  160. }
  161. $orderConfig = sheep_config('shop.order');
  162. $orderUser = User::get($order->user_id);
  163. foreach ($items as $item) {
  164. if (!in_array($item['id'], $itemIds)) {
  165. // 不在本次评价列表
  166. continue;
  167. }
  168. $comment = $comments[$item['id']] ?? [];
  169. $status = 'normal';
  170. if (isset($orderConfig['comment_check']) && $orderConfig['comment_check'] && $type != 'system') {
  171. // 需要检查,并且不是系统自动评价
  172. $status = 'hidden';
  173. }
  174. Comment::create([
  175. 'goods_id' => $item->goods_id,
  176. 'order_id' => $order->id,
  177. 'order_item_id' => $item->id,
  178. 'user_id' => $order->user_id,
  179. 'user_nickname' => $orderUser ? $orderUser->nickname : null,
  180. 'user_avatar' => $orderUser ? $orderUser->avatar : null,
  181. 'level' => $comment['level'] ?? 5,
  182. 'content' => $comment['content'] ?? ($orderConfig['auto_comment_content'] ?? '用户默认好评'),
  183. 'images' => $comment['images'] ?? [],
  184. 'status' => $status
  185. ]);
  186. $item->ext = array_merge($item->ext, ['comment_time' => time()]);
  187. $item->comment_status = OrderItem::COMMENT_STATUS_OK; // 评价
  188. $item->save();
  189. Action::add($order, $item, $user, $type, $this->getOperText($type) . '评价完成');
  190. // 订单评价后
  191. $data = ['order' => $order, 'item' => $item, 'user' => $user];
  192. \think\Hook::listen('order_comment_after', $data);
  193. }
  194. return $order;
  195. }
  196. /**
  197. * 删除订单
  198. *
  199. * @param object $order
  200. * @param object|null $user
  201. * @param string $type
  202. * @return void
  203. */
  204. public function delete($order, $user = null, $type = 'user')
  205. {
  206. $order->delete(); // 删除订单
  207. Action::add($order, null, $user, 'user', '用户删除订单');
  208. }
  209. /**
  210. * 存储活动信息
  211. *
  212. * @param \think\Model $order
  213. * @param array $result
  214. * @return void
  215. */
  216. public function addActivityOrder($order)
  217. {
  218. $ext = $order->ext;
  219. $items = $order->items;
  220. $goodsIds = array_column($items, 'goods_id');
  221. $model = new ActivityOrderModel();
  222. $model->order_id = $order->id;
  223. $model->user_id = $order->user_id;
  224. $model->activity_id = $order->activity_id;
  225. $model->activity_title = $ext['activity_title'] ?? null;
  226. $model->activity_type = $ext['activity_type'] ?? null;
  227. $model->pay_fee = $order->pay_fee;
  228. $model->goods_amount = $order->goods_amount;
  229. $model->discount_fee = $ext['activity_discount_amount'] ?? 0; // 普通商品总额和活动时商品总额的差
  230. $model->goods_ids = join(',', $goodsIds);
  231. $model->save();
  232. }
  233. /**
  234. * 存储促销信息
  235. *
  236. * @param \think\Model $order
  237. * @param array $result
  238. * @return void
  239. */
  240. public function addPromosOrder($order)
  241. {
  242. $ext = $order->ext;
  243. $promoInfos = $ext['promo_infos'] ?? [];
  244. foreach ($promoInfos as $key => $info) {
  245. $model = new ActivityOrderModel();
  246. $model->order_id = $order->id;
  247. $model->user_id = $order->user_id;
  248. $model->activity_id = $info['activity_id'];
  249. $model->activity_title = $info['activity_title'];
  250. $model->activity_type = $info['activity_type'];
  251. $model->pay_fee = $order->pay_fee;
  252. $model->goods_amount = $info['promo_goods_amount'];
  253. $model->discount_fee = 0;
  254. if (in_array($info['activity_type'], ['full_reduce', 'full_discount', 'free_shipping'])) {
  255. $model->discount_fee = $info['promo_discount_money'];
  256. } else if ($info['activity_type'] == 'full_gift') {
  257. // 这里设置为 0,等支付成功之后补充
  258. $model->discount_fee = 0;
  259. }
  260. $model->goods_ids = join(',', $info['goods_ids']);
  261. $rules = [
  262. 'rule_type' => $info['rule_type'],
  263. 'discount_rule' => $info['discount_rule'],
  264. ];
  265. if ($info['activity_type'] == 'full_gift') {
  266. $rules['limit_num'] = $info['limit_num'] ?? 0;
  267. $rules['event'] = $info['event'] ?? 0;
  268. }
  269. $currentExt['rules'] = $rules;
  270. $model->ext = $currentExt;
  271. $model->save();
  272. }
  273. }
  274. /**
  275. * 将活动订单标记为已支付
  276. *
  277. * @param [type] $order
  278. * @return void
  279. */
  280. public function activityOrderPaid($order)
  281. {
  282. $activityOrders = ActivityOrderModel::where('order_id', $order->id)->select();
  283. foreach ($activityOrders as $activityOrder) {
  284. if ($activityOrder->activity_type == 'full_gift') {
  285. $value_money = GiftLog::where('activity_id', $activityOrder->activity_id)
  286. ->where('order_id', $activityOrder->order_id)
  287. ->where('user_id', $activityOrder->user_id)
  288. ->whereIn('type', ['money', 'coupon']) // 这里只算 赠送的余额,和优惠券(不算积分,和赠送商品的价值)
  289. ->sum('value');
  290. $activityOrder->discount_fee = $value_money; // 补充赠送的价值
  291. } else if (in_array($activityOrder->activity_type, ['groupon', 'groupon_ladder'])) {
  292. $ext = $order->ext;
  293. $currentExt['buy_type'] = $ext['buy_type'] ?? '';
  294. $currentExt['groupon_id'] = $ext['groupon_id'] ?? 0; // 开团时候,支付之后才会有 groupon_id
  295. $activityOrder->ext = $currentExt;
  296. }
  297. $activityOrder->status = ActivityOrderModel::STATUS_PAID;
  298. $activityOrder->save();
  299. }
  300. }
  301. /**
  302. * 根据 oper_type 获取对应的用户
  303. */
  304. private function getOperText($oper_type)
  305. {
  306. switch($oper_type) {
  307. case 'user':
  308. $oper_text = '用户';
  309. break;
  310. case 'admin':
  311. $oper_text = '管理员';
  312. break;
  313. case 'system':
  314. $oper_text = '系统自动';
  315. break;
  316. default :
  317. $oper_text = '系统自动';
  318. break;
  319. }
  320. return $oper_text;
  321. }
  322. }