OrderJob.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs;
  12. use app\services\activity\bargain\StoreBargainServices;
  13. use app\services\activity\combination\StoreCombinationServices;
  14. use app\services\activity\seckill\StoreSeckillServices;
  15. use app\services\activity\coupon\StoreCouponUserServices;
  16. use app\services\kefu\service\StoreServiceServices;
  17. use app\services\message\notice\SmsService;
  18. use app\services\order\OutStoreOrderServices;
  19. use app\services\order\StoreOrderCartInfoServices;
  20. use app\services\order\StoreOrderEconomizeServices;
  21. use app\services\order\StoreOrderServices;
  22. use app\services\product\product\StoreProductServices;
  23. use app\services\user\member\MemberCardServices;
  24. use app\services\user\UserLabelRelationServices;
  25. use app\services\user\UserLevelServices;
  26. use app\services\user\UserServices;
  27. use app\services\wechat\WechatUserServices;
  28. use crmeb\basic\BaseJobs;
  29. use crmeb\services\app\WechatService;
  30. use crmeb\services\workerman\ChannelService;
  31. use crmeb\traits\QueueTrait;
  32. use think\exception\ValidateException;
  33. use think\facade\Log;
  34. /**
  35. * 订单消息队列
  36. * Class OrderJob
  37. * @package crmeb\jobs
  38. */
  39. class OrderJob extends BaseJobs
  40. {
  41. use QueueTrait;
  42. /**
  43. * 执行订单支付成功发送消息
  44. * @param $order
  45. * @return bool
  46. */
  47. public function doJob($order)
  48. {
  49. //计算商品节省金额
  50. try {
  51. $this->setEconomizeMoney($order);
  52. } catch (\Throwable $e) {
  53. Log::error('计算节省金额,失败原因:' . $e->getMessage());
  54. }
  55. //更新用户支付订单数量
  56. try {
  57. $this->setUserPayCountAndPromoter($order);
  58. } catch (\Throwable $e) {
  59. Log::error('更新用户订单数失败,失败原因:' . $e->getMessage());
  60. }
  61. //增加用户标签
  62. try {
  63. $this->setUserLabel($order);
  64. } catch (\Throwable $e) {
  65. Log::error('用户标签添加失败,失败原因:' . $e->getMessage());
  66. }
  67. try {
  68. if (in_array($order['is_channel'], [0, 2])) {//公众号发送模板消息
  69. $this->sendOrderPaySuccessCustomerService($order, 1);
  70. } else if (in_array($order['is_channel'], [1, 2])) {//小程序发送模板消息
  71. $this->sendOrderPaySuccessCustomerService($order, 0);
  72. }
  73. } catch (\Exception $e) {
  74. throw new ValidateException('发送客服消息,短信消息失败,失败原因:' . $e->getMessage());
  75. }
  76. //打印小票
  77. // $switch = sys_config('pay_success_printing_switch') ? true : false;
  78. // if ($switch) {
  79. // try {
  80. // /** @var StoreOrderServices $orderServices */
  81. // $orderServices = app()->make(StoreOrderServices::class);
  82. // $orderServices->orderPrint($order, $order['cart_id']);
  83. // } catch (\Throwable $e) {
  84. // Log::error('打印小票发生错误,错误原因:' . $e->getMessage());
  85. // }
  86. // }
  87. //检测会员等级
  88. try {
  89. /** @var UserLevelServices $levelServices */
  90. $levelServices = app()->make(UserLevelServices::class);
  91. $levelServices->detection((int)$order['uid']);
  92. } catch (\Throwable $e) {
  93. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  94. }
  95. //向后台发送新订单消息
  96. try {
  97. ChannelService::instance()->send('NEW_ORDER', ['order_id' => $order['order_id']]);
  98. } catch (\Throwable $e) {
  99. Log::error('向后台发送新订单消息失败,失败原因:' . $e->getMessage());
  100. }
  101. return true;
  102. }
  103. /**
  104. * 设置用户购买次数和检测时候成为推广人
  105. * @param $order
  106. */
  107. public function setUserPayCountAndPromoter($order)
  108. {
  109. /** @var UserServices $userServices */
  110. $userServices = app()->make(UserServices::class);
  111. $userInfo = $userServices->get($order['uid']);
  112. if ($userInfo) {
  113. $userInfo->pay_count = $userInfo->pay_count + 1;
  114. if (!$userInfo->is_promoter) {
  115. /** @var StoreOrderServices $orderServices */
  116. $orderServices = app()->make(StoreOrderServices::class);
  117. $price = $orderServices->sum(['paid' => 1, 'refund_status' => 0, 'uid' => $userInfo['uid']], 'pay_price');
  118. $status = is_brokerage_statu($price);
  119. if ($status) {
  120. $userInfo->is_promoter = 1;
  121. }
  122. }
  123. $userInfo->save();
  124. }
  125. }
  126. /**
  127. * 设置用户购买的标签
  128. * @param $order
  129. */
  130. public function setUserLabel($order)
  131. {
  132. /** @var StoreOrderCartInfoServices $cartInfoServices */
  133. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  134. $productIds = $cartInfoServices->getCartColunm(['oid' => $order['id']], 'product_id', '');
  135. /** @var StoreProductServices $productServices */
  136. $productServices = app()->make(StoreProductServices::class);
  137. $label = $productServices->getColumn([['id', 'in', $productIds]], 'label_id');
  138. $labelIds = array_unique(explode(',', implode(',', $label)));
  139. /** @var UserLabelRelationServices $labelServices */
  140. $labelServices = app()->make(UserLabelRelationServices::class);
  141. $where = [
  142. ['label_id', 'in', $labelIds],
  143. ['uid', '=', $order['uid']]
  144. ];
  145. $data = [];
  146. $userLabel = $labelServices->getColumn($where, 'label_id');
  147. foreach ($labelIds as $item) {
  148. if (!in_array($item, $userLabel)) {
  149. $data[] = ['uid' => $order['uid'], 'label_id' => $item];
  150. }
  151. }
  152. $re = true;
  153. if ($data) {
  154. $re = $labelServices->saveAll($data);
  155. }
  156. return $re;
  157. }
  158. /**
  159. * 订单支付成功后给客服发送客服消息
  160. * @param $order
  161. * @param int $type 1 公众号 0 小程序
  162. * @return string
  163. */
  164. public function sendOrderPaySuccessCustomerService($order, $type = 0)
  165. {
  166. /** @var StoreServiceServices $services */
  167. $services = app()->make(StoreServiceServices::class);
  168. /** @var WechatUserServices $wechatUserServices */
  169. $wechatUserServices = app()->make(WechatUserServices::class);
  170. $serviceOrderNotice = $services->getStoreServiceOrderNotice();
  171. if (count($serviceOrderNotice)) {
  172. /** @var StoreProductServices $services */
  173. $services = app()->make(StoreProductServices::class);
  174. /** @var StoreSeckillServices $seckillServices */
  175. $seckillServices = app()->make(StoreSeckillServices::class);
  176. /** @var StoreCombinationServices $pinkServices */
  177. $pinkServices = app()->make(StoreCombinationServices::class);
  178. /** @var StoreBargainServices $bargainServices */
  179. $bargainServices = app()->make(StoreBargainServices::class);
  180. /** @var StoreOrderCartInfoServices $cartInfoServices */
  181. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  182. foreach ($serviceOrderNotice as $item) {
  183. $userInfo = $wechatUserServices->getOne(['uid' => $item['uid'], 'user_type' => 'wechat']);
  184. if ($userInfo) {
  185. $userInfo = $userInfo->toArray();
  186. if ($userInfo['subscribe'] && $userInfo['openid']) {
  187. if ($item['customer']) {
  188. // 统计管理开启 推送图文消息
  189. $head = '订单提醒 订单号:' . $order['order_id'];
  190. $url = sys_config('site_url') . '/pages/admin/orderDetail/index?id=' . $order['order_id'];
  191. $description = '';
  192. $image = sys_config('site_logo');
  193. if (isset($order['seckill_id']) && $order['seckill_id'] > 0) {
  194. $description .= '秒杀商品:' . $seckillServices->value(['id' => $order['seckill_id']], 'title');
  195. $image = $seckillServices->value(['id' => $order['seckill_id']], 'image');
  196. } else if (isset($order['combination_id']) && $order['combination_id'] > 0) {
  197. $description .= '拼团商品:' . $pinkServices->value(['id' => $order['combination_id']], 'title');
  198. $image = $pinkServices->value(['id' => $order['combination_id']], 'image');
  199. } else if (isset($order['bargain_id']) && $order['bargain_id'] > 0) {
  200. $title = $bargainServices->value(['id' => $order['bargain_id']], 'title');
  201. $description .= '砍价商品:' . $title;
  202. $image = $bargainServices->value(['id' => $order['bargain_id']], 'image');
  203. } else {
  204. $productIds = $cartInfoServices->getCartIdsProduct($order['id']);
  205. $storeProduct = $services->getProductArray([['id', 'in', $productIds]], 'image,store_name', 'id');
  206. if (count($storeProduct)) {
  207. foreach ($storeProduct as $value) {
  208. $description .= $value['store_name'] . ' ';
  209. $image = $value['image'];
  210. }
  211. }
  212. }
  213. $message = WechatService::newsMessage($head, $description, $url, $image);
  214. try {
  215. WechatService::staffService()->message($message)->to($userInfo['openid'])->send();
  216. } catch (\Exception $e) {
  217. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  218. }
  219. } else {
  220. // 推送文字消息
  221. $head = "客服提醒:亲,您有一个新订单 \r\n订单单号:{$order['order_id']}\r\n支付金额:¥{$order['pay_price']}\r\n备注信息:{$order['mark']}\r\n订单来源:小程序";
  222. if ($type) $head = "客服提醒:亲,您有一个新订单 \r\n订单单号:{$order['order_id']}\r\n支付金额:¥{$order['pay_price']}\r\n备注信息:{$order['mark']}\r\n订单来源:公众号";
  223. try {
  224. WechatService::staffService()->message($head)->to($userInfo['openid'])->send();
  225. } catch (\Exception $e) {
  226. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. /**
  235. * 计算节约金额
  236. * @param $order
  237. * @return false|mixed
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\DbException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. */
  242. public function setEconomizeMoney($order)
  243. {
  244. /** @var UserServices $userService */
  245. $userService = app()->make(UserServices::class);
  246. /** @var StoreOrderCartInfoServices $cartInfoService */
  247. $cartInfoService = app()->make(StoreOrderCartInfoServices::class);
  248. /** @var StoreCouponUserServices $couponService */
  249. $couponService = app()->make(StoreCouponUserServices::class);
  250. /** @var StoreOrderEconomizeServices $economizeService */
  251. $economizeService = app()->make(StoreOrderEconomizeServices::class);
  252. /** @var MemberCardServices $memberCardService */
  253. $memberCardService = app()->make(MemberCardServices::class);
  254. $getOne = $economizeService->getOne(['order_id' => $order['order_id']]);
  255. if ($getOne) return false;
  256. //看是否是会员
  257. $userInfo = $userService->getUserInfo($order['uid']);
  258. if ($userInfo && $userInfo['is_money_level'] > 0) {
  259. $save = [];
  260. $save['order_type'] = 1;
  261. $save['add_time'] = time();
  262. $save['pay_price'] = $order['pay_price'];
  263. $save['order_id'] = $order['order_id'];
  264. $save['uid'] = $order['uid'];
  265. //计算商品节约金额
  266. $isOpenVipPrice = $memberCardService->isOpenMemberCard('vip_price');
  267. if ($isOpenVipPrice) {
  268. $cartInfo = $cartInfoService->getOrderCartInfo($order['id']);
  269. $memberPrice = 0.00;
  270. if ($cartInfo) {
  271. foreach ($cartInfo as $k => $item) {
  272. foreach ($item as $value) {
  273. if (isset($value['price_type']) && $value['price_type'] == 'member') $memberPrice += bcmul($value['vip_truePrice'], $value['cart_num'] ?: 1, 2);
  274. }
  275. }
  276. }
  277. $save['member_price'] = $memberPrice;
  278. }
  279. //计算邮费节约金额
  280. $isOpenExpress = $memberCardService->isOpenMemberCard('express');
  281. if ($isOpenExpress) {
  282. $expressTotalMoney = bcdiv($order['total_postage'], bcdiv($isOpenExpress, 100, 2), 2);
  283. $save['postage_price'] = bcsub($expressTotalMoney, $order['total_postage'], 2);
  284. }
  285. //计算会员券节省金额
  286. if ($order['coupon_id']) {
  287. $couponMoney = $couponService->get($order['coupon_id'], ['*'], ['issue']);
  288. if ($couponMoney && $couponMoney['receive_type']) {
  289. $save['coupon_price'] = $couponMoney['coupon_price'];
  290. }
  291. }
  292. return $economizeService->addEconomize($save);
  293. }
  294. return false;
  295. }
  296. }