OrderJob.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\order;
  12. use app\jobs\integral\IntegralJob;
  13. use app\services\activity\discounts\StoreDiscountsServices;
  14. use app\services\activity\bargain\StoreBargainServices;
  15. use app\services\activity\combination\StoreCombinationServices;
  16. use app\services\activity\seckill\StoreSeckillServices;
  17. use app\services\activity\coupon\StoreCouponUserServices;
  18. use app\services\message\service\StoreServiceServices;
  19. use app\services\message\sms\SmsSendServices;
  20. use app\services\order\StoreOrderCartInfoServices;
  21. use app\services\order\StoreOrderEconomizeServices;
  22. use app\services\order\StoreOrderServices;
  23. use app\services\product\branch\StoreBranchProductServices;
  24. use app\services\product\product\StoreProductServices;
  25. use app\services\user\member\MemberCardServices;
  26. use app\services\user\label\UserLabelRelationServices;
  27. use app\services\user\level\UserLevelServices;
  28. use app\services\user\UserServices;
  29. use app\services\wechat\WechatUserServices;
  30. use app\webscoket\SocketPush;
  31. use crmeb\basic\BaseJobs;
  32. use crmeb\services\wechat\Messages;
  33. use crmeb\services\wechat\OfficialAccount;
  34. use crmeb\traits\QueueTrait;
  35. use think\facade\Log;
  36. /**
  37. * 订单消息队列
  38. * Class OrderJob
  39. * @package app\jobs
  40. */
  41. class OrderJob extends BaseJobs
  42. {
  43. use QueueTrait;
  44. /**
  45. * 执行订单支付成功发送消息
  46. * @param $order
  47. * @return bool
  48. */
  49. public function otherTake($order)
  50. {
  51. //检测会员等级
  52. if ($order['uid']) {
  53. try {
  54. /** @var UserLevelServices $levelServices */
  55. $levelServices = app()->make(UserLevelServices::class);
  56. $levelServices->detection((int)$order['uid']);
  57. } catch (\Throwable $e) {
  58. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  59. }
  60. }
  61. if ($order['store_id']) {
  62. //向门店后台发送新订单消息
  63. try {
  64. SocketPush::store()->to($order['store_id'])->type('NEW_ORDER')->data(['order_id' => $order['order_id']])->push();
  65. } catch (\Throwable $e) {
  66. Log::error('向后台发送新订单消息失败,失败原因:' . $e->getMessage());
  67. }
  68. } else {
  69. //向后台发送新订单消息
  70. try {
  71. SocketPush::admin()->type('NEW_ORDER')->data(['order_id' => $order['order_id']])->push();
  72. } catch (\Throwable $e) {
  73. Log::error('向后台发送新订单消息失败,失败原因:' . $e->getMessage());
  74. }
  75. }
  76. return true;
  77. }
  78. /**
  79. * 设置用户购买次数和检测时候成为推广人
  80. * @param $order
  81. */
  82. public function setUserPayCountAndPromoter($order)
  83. {
  84. try {
  85. /** @var UserServices $userServices */
  86. $userServices = app()->make(UserServices::class);
  87. $userInfo = $userServices->get($order['uid']);
  88. if ($userInfo) {
  89. $userInfo->pay_count = $userInfo->pay_count + 1;
  90. if (!$userInfo->is_promoter) {
  91. /** @var StoreOrderServices $orderServices */
  92. $orderServices = app()->make(StoreOrderServices::class);
  93. $price = $orderServices->sum(['pid' => 0, 'paid' => 1, 'refund_status' => 0, 'uid' => $userInfo['uid']], 'pay_price');
  94. $status = is_brokerage_statu($price);
  95. if ($status) {
  96. $userInfo->is_promoter = 1;
  97. }
  98. }
  99. $userInfo->save();
  100. }
  101. } catch (\Throwable $e) {
  102. Log::error('更新用户订单数失败,失败原因:' . $e->getMessage());
  103. }
  104. return true;
  105. }
  106. /**
  107. * 设置用户购买的标签
  108. * @param $order
  109. */
  110. public function setUserLabel($order)
  111. {
  112. try {
  113. /** @var StoreOrderCartInfoServices $cartInfoServices */
  114. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  115. $cartInfos = $cartInfoServices->getCartColunm(['oid' => $order['id']], 'cart_info');
  116. $cartInfo = [];
  117. foreach ($cartInfos as $cart) {
  118. $cartInfo[] = is_string($cart) ? json_decode($cart, true) : $cart;
  119. }
  120. $productIds = array_unique(array_column($cartInfo, 'product_id'));
  121. if (isset($order['store_id']) && $order['store_id']) {
  122. /** @var StoreBranchProductServices $branchProductServices */
  123. $branchProductServices = app()->make(StoreBranchProductServices::class);
  124. $label = $branchProductServices->getColumn([['id', 'in', $productIds], ['type', '=', 1], ['relation_id', '=', $order['store_id']]], 'label_id');
  125. } else {
  126. /** @var StoreProductServices $productServices */
  127. $productServices = app()->make(StoreProductServices::class);
  128. $label = $productServices->getColumn([['id', 'in', $productIds]], 'label_id');
  129. }
  130. $labelIds = [];
  131. if ($label) {
  132. $labelIds = explode(',', implode(',', $label));
  133. }
  134. if ($order['type'] == 5 && $order['activity_id']) {
  135. /** @var StoreDiscountsServices $storeDiscountsServices */
  136. $storeDiscountsServices = app()->make(StoreDiscountsServices::class);
  137. $discounts_label = $storeDiscountsServices->value(['id' => $order['activity_id']], 'link_ids');
  138. if ($discounts_label) {
  139. $labelIds = array_merge($labelIds, explode(',', $discounts_label));
  140. }
  141. }
  142. if (!$labelIds) {
  143. return true;
  144. }
  145. $store_id = $order['store_id'] ?? 0;
  146. $type = $store_id ? 1 : 0;
  147. $labelIds = array_unique($labelIds);
  148. /** @var UserLabelRelationServices $labelServices */
  149. $labelServices = app()->make(UserLabelRelationServices::class);
  150. $labelServices->setUserLable([$order['uid']], $labelIds, $type, $store_id);
  151. } catch (\Throwable $e) {
  152. Log::error('用户标签添加失败,失败原因:' . $e->getMessage());
  153. }
  154. return true;
  155. }
  156. /**
  157. * 发送模板消息和客服消息
  158. * @param $order
  159. * @return bool
  160. */
  161. public function sendServicesAndTemplate($order)
  162. {
  163. try {
  164. if (in_array($order['is_channel'], [1, 2])) {//小程序发送模板消息
  165. //订单支付成功后给客服发送客服消息
  166. $this->sendOrderPaySuccessCustomerService($order, 0);
  167. } else {//公众号发送模板消息
  168. //订单支付成功后给客服发送客服消息
  169. $this->sendOrderPaySuccessCustomerService($order, 1);
  170. }
  171. } catch (\Exception $e) {
  172. Log::error('发送客服消息,短信消息失败,失败原因:' . $e->getMessage());
  173. }
  174. return true;
  175. }
  176. /**
  177. * 订单支付成功后给客服发送客服消息
  178. * @param $order
  179. * @param int $type 1 公众号 0 小程序
  180. * @return string
  181. */
  182. public function sendOrderPaySuccessCustomerService($order, $type = 0)
  183. {
  184. /** @var StoreServiceServices $services */
  185. $services = app()->make(StoreServiceServices::class);
  186. /** @var WechatUserServices $wechatUserServices */
  187. $wechatUserServices = app()->make(WechatUserServices::class);
  188. $serviceOrderNotice = $services->getStoreServiceOrderNotice();
  189. if (count($serviceOrderNotice)) {
  190. /** @var StoreProductServices $services */
  191. $services = app()->make(StoreProductServices::class);
  192. /** @var StoreSeckillServices $seckillServices */
  193. $seckillServices = app()->make(StoreSeckillServices::class);
  194. /** @var StoreCombinationServices $pinkServices */
  195. $pinkServices = app()->make(StoreCombinationServices::class);
  196. /** @var StoreBargainServices $bargainServices */
  197. $bargainServices = app()->make(StoreBargainServices::class);
  198. /** @var StoreOrderCartInfoServices $cartInfoServices */
  199. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  200. /** @var SmsSendServices $smsServices */
  201. // $smsServices = app()->make(SmsSendServices::class);
  202. // $switch = (bool)sys_config('admin_pay_success_switch');
  203. $description = '';
  204. $image = sys_config('site_logo');
  205. switch ($order['type']) {
  206. case 1://秒杀
  207. $description .= '秒杀商品:' . $seckillServices->value(['id' => $order['activity_id']], 'title');
  208. $image = $seckillServices->value(['id' => $order['activity_id']], 'image');
  209. break;
  210. case 2://砍价
  211. $description .= '砍价商品:' . $bargainServices->value(['id' => $order['activity_id']], 'title');
  212. $image = $bargainServices->value(['id' => $order['activity_id']], 'image');
  213. break;
  214. case 3://拼团
  215. $description .= '拼团商品:' . $pinkServices->value(['id' => $order['activity_id']], 'title');
  216. $image = $pinkServices->value(['id' => $order['activity_id']], 'image');
  217. break;
  218. default:
  219. $productIds = $cartInfoServices->getCartIdsProduct((int)$order['id']);
  220. $storeProduct = $services->getProductArray([['id', 'in', $productIds]], 'image,store_name', 'id');
  221. if (count($storeProduct)) {
  222. foreach ($storeProduct as $value) {
  223. $description .= $value['store_name'] . ' ';
  224. $image = $value['image'];
  225. }
  226. }
  227. break;
  228. }
  229. foreach ($serviceOrderNotice as $key => $item) {
  230. $userInfo = $wechatUserServices->getOne(['uid' => $item['uid'], 'user_type' => 'wechat']);
  231. if ($userInfo) {
  232. $userInfo = $userInfo->toArray();
  233. if ($userInfo['subscribe'] && $userInfo['openid']) {
  234. if ($item['customer']) {
  235. // 统计管理开启 推送图文消息
  236. $head = '订单提醒 订单号:' . $order['order_id'];
  237. $url = sys_config('site_url') . '/pages/admin/orderDetail/index?id=' . $order['order_id'];
  238. $message = Messages::newsMessage($head, $description, $url, $image);
  239. try {
  240. OfficialAccount::staffService()->message($message)->to($userInfo['openid'])->send();
  241. } catch (\Exception $e) {
  242. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  243. }
  244. } else {
  245. // 推送文字消息
  246. $head = "客服提醒:亲,您有一个新订单 \r\n订单单号:{$order['order_id']}\r\n支付金额:¥{$order['pay_price']}\r\n备注信息:{$order['mark']}\r\n订单来源:小程序";
  247. if ($type) $head = "客服提醒:亲,您有一个新订单 \r\n订单单号:{$order['order_id']}\r\n支付金额:¥{$order['pay_price']}\r\n备注信息:{$order['mark']}\r\n订单来源:公众号";
  248. try {
  249. OfficialAccount::staffService()->message($head)->to($userInfo['openid'])->send();
  250. } catch (\Exception $e) {
  251. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. return true;
  259. }
  260. /**
  261. * 支付成功短信提醒
  262. * @param string $order_id
  263. */
  264. public function mssageSendPaySuccess($order)
  265. {
  266. $switch = (bool)sys_config('lower_order_switch');
  267. //模板变量
  268. $pay_price = $order['pay_price'];
  269. $order_id = $order['order_id'];
  270. /** @var SmsSendServices $smsServices */
  271. $smsServices = app()->make(SmsSendServices::class);
  272. $smsServices->send($switch, $order['user_phone'], compact('order_id', 'pay_price'), 'PAY_SUCCESS_CODE');
  273. }
  274. /**
  275. * 计算节约金额
  276. * @param $order
  277. */
  278. public function setEconomizeMoney($order)
  279. {
  280. try {
  281. /** @var UserServices $userService */
  282. $userService = app()->make(UserServices::class);
  283. /** @var StoreOrderCartInfoServices $cartInfoService */
  284. $cartInfoService = app()->make(StoreOrderCartInfoServices::class);
  285. /** @var StoreCouponUserServices $couponService */
  286. $couponService = app()->make(StoreCouponUserServices::class);
  287. /** @var StoreOrderEconomizeServices $economizeService */
  288. $economizeService = app()->make(StoreOrderEconomizeServices::class);
  289. /** @var MemberCardServices $memberCardService */
  290. $memberCardService = app()->make(MemberCardServices::class);
  291. $getOne = $economizeService->getOne(['order_id' => $order['order_id']]);
  292. if ($getOne) return false;
  293. //看是否是会员
  294. $userInfo = $userService->getUserInfo($order['uid']);
  295. if ($userInfo && $userInfo['is_money_level'] > 0) {
  296. $save = [];
  297. $save['order_type'] = 1;
  298. $save['add_time'] = time();
  299. $save['pay_price'] = $order['pay_price'];
  300. $save['order_id'] = $order['order_id'];
  301. $save['uid'] = $order['uid'];
  302. //计算商品节约金额
  303. $isOpenVipPrice = $memberCardService->isOpenMemberCardCache('vip_price');
  304. if ($isOpenVipPrice) {
  305. $cartInfo = $cartInfoService->getOrderCartInfo($order['id']);
  306. $memberPrice = 0.00;
  307. if ($cartInfo) {
  308. foreach ($cartInfo as $k => $item) {
  309. foreach ($item as $value) {
  310. if (isset($value['price_type']) && $value['price_type'] == 'member') $memberPrice = bcadd((string)$memberPrice, (string)bcmul((string)$value['vip_truePrice'], (string)$value['cart_num'] ?: '1', 2), 2);
  311. }
  312. }
  313. }
  314. $save['member_price'] = $memberPrice;
  315. }
  316. //计算邮费节约金额
  317. $isOpenExpress = $memberCardService->isOpenMemberCardCache('express');
  318. if ($isOpenExpress) {
  319. $expressTotalMoney = bcdiv($order['total_postage'], bcdiv($isOpenExpress, 100, 2), 2);
  320. $save['postage_price'] = bcsub($expressTotalMoney, $order['total_postage'], 2);
  321. }
  322. //计算会员券节省金额
  323. if ($order['coupon_id'] && $order['coupon_price']) {
  324. $coupon = $couponService->getOne(['id' => $order['coupon_id']], '*', ['issue']);
  325. //是会员券
  326. if ($coupon && $coupon['receive_type'] == 4) {
  327. $save['coupon_price'] = $order['coupon_price'];
  328. }
  329. }
  330. return $economizeService->addEconomize($save);
  331. }
  332. } catch (\Throwable $e) {
  333. Log::error('计算节省金额,失败原因:' . $e->getMessage());
  334. }
  335. return true;
  336. }
  337. public function setWriteValid()
  338. {
  339. }
  340. }