StoreOrderTakeServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\services\BaseServices;
  14. use app\services\coupon\StoreCouponIssueServices;
  15. use app\services\message\service\StoreServiceServices;
  16. use app\services\message\sms\SmsSendServices;
  17. use app\services\user\MemberCardServices;
  18. use app\services\user\UserBillServices;
  19. use app\services\user\UserBrokerageFrozenServices;
  20. use app\services\user\UserLevelServices;
  21. use app\services\user\UserServices;
  22. use app\services\wechat\WechatUserServices;
  23. use app\jobs\RoutineTemplateJob;
  24. use app\jobs\SmsAdminJob;
  25. use app\jobs\WechatTemplateJob as TemplateJob;
  26. use crmeb\services\SystemConfigService;
  27. use crmeb\utils\Str;
  28. use think\exception\ValidateException;
  29. use think\facade\Log;
  30. /**
  31. * 订单收货
  32. * Class StoreOrderTakeServices
  33. * @package app\services\order
  34. * @method get(int $id, ?array $field = []) 获取一条
  35. */
  36. class StoreOrderTakeServices extends BaseServices
  37. {
  38. /**
  39. * 构造方法
  40. * StoreOrderTakeServices constructor.
  41. * @param StoreOrderDao $dao
  42. */
  43. public function __construct(StoreOrderDao $dao)
  44. {
  45. $this->dao = $dao;
  46. }
  47. /**
  48. * 用户订单收货
  49. * @param $uni
  50. * @param $uid
  51. * @return bool
  52. */
  53. public function takeOrder(string $uni, int $uid)
  54. {
  55. $order = $this->dao->getUserOrderDetail($uni, $uid);
  56. if (!$order) {
  57. throw new ValidateException('订单不存在!');
  58. }
  59. /** @var StoreOrderServices $orderServices */
  60. $orderServices = app()->make(StoreOrderServices::class);
  61. $order = $orderServices->tidyOrder($order);
  62. if ($order['_status']['_type'] != 2) {
  63. throw new ValidateException('订单状态错误!');
  64. }
  65. $order->status = 2;
  66. /** @var StoreOrderStatusServices $statusService */
  67. $statusService = app()->make(StoreOrderStatusServices::class);
  68. $res = $order->save() && $statusService->save([
  69. 'oid' => $order['id'],
  70. 'change_type' => 'user_take_delivery',
  71. 'change_message' => '用户已收货',
  72. 'change_time' => time()
  73. ]);
  74. $res = $res && $this->storeProductOrderUserTakeDelivery($order);
  75. if (!$res) {
  76. throw new ValidateException('收货失败');
  77. }
  78. return $order;
  79. }
  80. /**
  81. * 订单确认收货
  82. * @param $order
  83. * @return bool
  84. */
  85. public function storeProductOrderUserTakeDelivery($order, bool $isTran = true)
  86. {
  87. /** @var UserServices $userServices */
  88. $userServices = app()->make(UserServices::class);
  89. $userInfo = $userServices->get((int)$order['uid']);
  90. //获取购物车内的商品标题
  91. /** @var StoreOrderCartInfoServices $orderInfoServices */
  92. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  93. $storeName = $orderInfoServices->getCarIdByProductTitle($order['cart_id']);
  94. $storeTitle = Str::substrUTf8($storeName, 20, 'UTF-8', '');
  95. $res = $this->transaction(function () use ($order, $userInfo, $storeTitle) {
  96. //赠送积分
  97. $res1 = $this->gainUserIntegral($order, $userInfo, $storeTitle);
  98. //返佣
  99. $res2 = $this->backOrderBrokerage($order, $userInfo);
  100. //经验
  101. $res3 = $this->gainUserExp($order, $userInfo);
  102. if (!($res1 && $res2 && $res3)) {
  103. throw new ValidateException('收货失败!');
  104. }
  105. return true;
  106. }, $isTran);
  107. if ($res) {
  108. try {
  109. // 收货成功后置队列
  110. event('order.orderTake', [$order, $userInfo, $storeTitle]);
  111. } catch (\Throwable $e) {
  112. }
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * 赠送积分
  120. * @param $order
  121. * @param $userInfo
  122. * @param $storeTitle
  123. * @return bool
  124. */
  125. public function gainUserIntegral($order, $userInfo, $storeTitle)
  126. {
  127. $res2 = true;
  128. if (!$userInfo) {
  129. return true;
  130. }
  131. // 营销产品送积分
  132. if (isset($order['combination_id']) && $order['combination_id']) {
  133. return true;
  134. }
  135. if (isset($order['seckill_id']) && $order['seckill_id']) {
  136. return true;
  137. }
  138. if (isset($order['bargain_id']) && $order['bargain_id']) {
  139. return true;
  140. }
  141. /** @var UserBillServices $userBillServices */
  142. $userBillServices = app()->make(UserBillServices::class);
  143. if ($order['gain_integral'] > 0) {
  144. $res2 = false != $userBillServices->income('pay_give_integral', $order['uid'], $order['gain_integral'], $userInfo['integral'], $order['id']);
  145. }
  146. $order_integral = 0;
  147. $res3 = true;
  148. $order_give_integral = sys_config('order_give_integral');
  149. if ($order['pay_price'] && $order_give_integral) {
  150. $order_integral = bcmul($order_give_integral, (string)$order['pay_price'], 0);
  151. $res3 = false != $userBillServices->income('order_give_integral', $order['uid'], $order_integral, $userInfo['integral'], $order['id']);
  152. }
  153. $give_integral = $order_integral + $order['gain_integral'];
  154. if ($give_integral > 0) {
  155. $integral = $userInfo['integral'] + $give_integral;
  156. $userInfo->integral = $integral;
  157. $res1 = false != $userInfo->save();
  158. $res = $res1 && $res2 && $res3;
  159. $this->sendUserIntegral($order, $give_integral, $integral, $storeTitle);
  160. return $res;
  161. }
  162. return true;
  163. }
  164. /**
  165. * 一级返佣
  166. * @param $orderInfo
  167. * @param $userInfo
  168. * @return bool
  169. */
  170. public function backOrderBrokerage($orderInfo, $userInfo)
  171. {
  172. //商城分销功能是否开启 0关闭1开启
  173. if (!sys_config('brokerage_func_status')) return true;
  174. // 营销产品不返佣金
  175. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) {
  176. return true;
  177. }
  178. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) {
  179. return true;
  180. }
  181. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) {
  182. return true;
  183. }
  184. // 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  185. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) {
  186. return true;
  187. }
  188. /** @var UserServices $userServices */
  189. $userServices = app()->make(UserServices::class);
  190. if (!$userServices->checkUserPromoter($userInfo['spread_uid'])) {
  191. return $this->backOrderBrokerageTwo($orderInfo, $userInfo);
  192. }
  193. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  194. /** @var StoreOrderCartInfoServices $cartServices */
  195. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  196. $brokeragePrice = $cartServices->getProductBrokerage($cartId);
  197. // 返佣金额小于等于0 直接返回不返佣金
  198. if ($brokeragePrice <= 0) {
  199. return true;
  200. }
  201. //TODO 获取上级推广员信息
  202. $spreadPrice = $userServices->value(['uid' => $userInfo['spread_uid']], 'brokerage_price');
  203. // 上级推广员返佣之后的金额
  204. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  205. // 添加推广记录
  206. /** @var UserBillServices $userBillServices */
  207. $userBillServices = app()->make(UserBillServices::class);
  208. $res1 = $userBillServices->income('get_brokerage', $userInfo['spread_uid'], [
  209. 'nickname' => $userInfo['nickname'],
  210. 'pay_price' => floatval($orderInfo['pay_price']),
  211. 'number' => floatval($brokeragePrice)
  212. ], $balance, $orderInfo['id']);
  213. // 添加用户余额
  214. $res2 = $userServices->bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  215. if ($res2) {
  216. /** @var UserBrokerageFrozenServices $frozenService */
  217. $frozenService = app()->make(UserBrokerageFrozenServices::class);
  218. $res2 = $frozenService->saveBrokage($userInfo['spread_uid'], $brokeragePrice, $res1->id, $orderInfo['order_id']);
  219. //给上级发送获得佣金的模板消息
  220. $this->sendBackOrderBrokerage($orderInfo, $userInfo['spread_uid'], $brokeragePrice);
  221. }
  222. // 一级返佣成功 跳转二级返佣
  223. $res = $res1 && $res2 && $this->backOrderBrokerageTwo($orderInfo, $userInfo);
  224. return $res;
  225. }
  226. /**
  227. * 二级推广返佣
  228. * @param $orderInfo
  229. * @param $userInfo
  230. * @return bool
  231. */
  232. public function backOrderBrokerageTwo($orderInfo, $userInfo)
  233. {
  234. /** @var UserServices $userServices */
  235. $userServices = app()->make(UserServices::class);
  236. // 获取上推广人
  237. $userInfoTwo = $userServices->get((int)$userInfo['spread_uid']);
  238. // 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  239. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) {
  240. return true;
  241. }
  242. // 获取后台分销类型 1 指定分销 2 人人分销
  243. if (!$userServices->checkUserPromoter($userInfoTwo['spread_uid'])) {
  244. return true;
  245. }
  246. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  247. /** @var StoreOrderCartInfoServices $cartServices */
  248. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  249. $brokeragePrice = $cartServices->getProductBrokerage($cartId, false);
  250. // 返佣金额小于等于0 直接返回不返佣金
  251. if ($brokeragePrice <= 0) {
  252. return true;
  253. }
  254. // 获取上上级推广员信息
  255. $spreadPrice = $userServices->value(['uid' => $userInfoTwo['spread_uid']], 'brokerage_price');
  256. // 获取上上级推广员返佣之后余额
  257. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  258. // 添加返佣记录
  259. /** @var UserBillServices $userBillServices */
  260. $userBillServices = app()->make(UserBillServices::class);
  261. $res1 = $userBillServices->income('get_two_brokerage', $userInfoTwo['spread_uid'], [
  262. 'nickname' => $userInfo['nickname'],
  263. 'pay_price' => floatval($orderInfo['pay_price']),
  264. 'number' => floatval($brokeragePrice)
  265. ], $balance, $orderInfo['id']);
  266. if ($res1) {
  267. /** @var UserBrokerageFrozenServices $frozenService */
  268. $frozenService = app()->make(UserBrokerageFrozenServices::class);
  269. $res1 = $frozenService->saveBrokage($userInfoTwo['spread_uid'], $brokeragePrice, $res1->id, $orderInfo['order_id']);
  270. }
  271. // 添加用户余额
  272. $res2 = $userServices->bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  273. //给上级发送获得佣金的模板消息
  274. $this->sendBackOrderBrokerage($orderInfo, $userInfoTwo['spread_uid'], $brokeragePrice);
  275. return $res1 && $res2;
  276. }
  277. /**佣金到账发送模板消息
  278. * @param $orderInfo
  279. * @param $spread_uid
  280. * @param $brokeragePrice
  281. */
  282. public function sendBackOrderBrokerage($orderInfo, $spread_uid, $brokeragePrice)
  283. {
  284. /** @var UserServices $userServices */
  285. $userServices = app()->make(UserServices::class);
  286. /** @var WechatUserServices $wechatServices */
  287. $wechatServices = app()->make(WechatUserServices::class);
  288. $userType = $userServices->value(['uid' => $spread_uid], 'user_type');
  289. /** @var StoreOrderCartInfoServices $storeOrderCartInfoService */
  290. $storeOrderCartInfoService = app()->make(StoreOrderCartInfoServices::class);
  291. $cartInfo = $storeOrderCartInfoService->getOrderCartInfo($orderInfo['id']);
  292. if ($cartInfo) {
  293. $cartInfo = array_column($cartInfo, 'cart_info');
  294. $goodsPrice = 0;
  295. $goodsName = "";
  296. foreach ($cartInfo as $k => $v) {
  297. $goodsName .= $v['productInfo']['store_name'];
  298. $goodsPrice += $v['productInfo']['price'];
  299. }
  300. }
  301. if (strtolower($userType) == 'routine') {
  302. $openid = $wechatServices->uidToOpenid($spread_uid, 'routine');
  303. if ($openid) RoutineTemplateJob::dispatchDo('sendOrderBrokerageSuccess', [$openid, $brokeragePrice, $goodsName]);
  304. } else {
  305. $openid = $wechatServices->uidToOpenid($spread_uid, 'wechat');
  306. if ($openid) TemplateJob::dispatchDo('sendOrderBrokerageSuccess', [$openid, $brokeragePrice, $goodsName, $goodsPrice, $orderInfo['add_time']]);
  307. }
  308. }
  309. /**
  310. * 赠送积分发送模板消息
  311. * @param $order
  312. * @param $give_integral
  313. * @param $integral
  314. */
  315. public function sendUserIntegral($order, $give_integral, $integral, $storeTitle)
  316. {
  317. /** @var WechatUserServices $wechatServices */
  318. $wechatServices = app()->make(WechatUserServices::class);
  319. //小程序
  320. $openid = $wechatServices->uidToOpenid($order['uid'], 'routine');
  321. RoutineTemplateJob::dispatchDo('sendUserIntegral', [$openid, $order, $storeTitle, $give_integral, $integral]);
  322. }
  323. /**
  324. * 赠送经验
  325. * @param $order
  326. * @param $userInfo
  327. * @return bool
  328. */
  329. public function gainUserExp($order, $userInfo)
  330. {
  331. if (!$userInfo) {
  332. return true;
  333. }
  334. /** @var UserBillServices $userBillServices */
  335. $userBillServices = app()->make(UserBillServices::class);
  336. $order_exp = 0;
  337. $res3 = true;
  338. $order_give_exp = sys_config('order_give_exp');
  339. if ($order['pay_price'] && $order_give_exp) {
  340. $order_exp = bcmul($order_give_exp, (string)$order['pay_price'], 2);
  341. $res3 = false != $userBillServices->income('order_give_exp', $order['uid'], $order_exp, $userInfo['exp'], $order['id']);
  342. }
  343. $res = true;
  344. if ($order_exp > 0) {
  345. $exp = $userInfo['exp'] + $order_exp;
  346. $userInfo->exp = $exp;
  347. $res1 = false != $userInfo->save();
  348. $res = $res1 && $res3;
  349. }
  350. //用户升级事件
  351. event('user.userLevel', [$order['uid']]);
  352. return $res;
  353. }
  354. /**
  355. * 自动收货
  356. * @return bool
  357. */
  358. public function autoTakeOrder()
  359. {
  360. //7天前时间戳
  361. $systemDeliveryTime = (int)sys_config('system_delivery_time', 0);
  362. //0为取消自动收货功能
  363. if ($systemDeliveryTime == 0) {
  364. return true;
  365. }
  366. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
  367. /** @var StoreOrderStoreOrderStatusServices $service */
  368. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  369. $orderList = $service->getTakeOrderIds([
  370. 'change_time' => $sevenDay,
  371. 'is_del' => 0,
  372. 'paid' => 1,
  373. 'status' => 1,
  374. 'change_type' => ['delivery_goods', 'delivery_fictitious', 'delivery']
  375. ]);
  376. foreach ($orderList as $order) {
  377. if ($order['status'] == 2) {
  378. continue;
  379. }
  380. if ($order['paid'] == 1 && $order['status'] == 1) {
  381. $data['status'] = 2;
  382. } else if ($order['pay_type'] == 'offline') {
  383. $data['status'] = 2;
  384. } else {
  385. continue;
  386. }
  387. try {
  388. $this->transaction(function () use ($order, $data) {
  389. /** @var StoreOrderStatusServices $statusService */
  390. $statusService = app()->make(StoreOrderStatusServices::class);
  391. $res = $this->dao->update($order['id'], $data) && $statusService->save([
  392. 'oid' => $order['id'],
  393. 'change_type' => 'take_delivery',
  394. 'change_message' => '已收货[自动收货]',
  395. 'change_time' => time()
  396. ]);
  397. $res = $res && $this->storeProductOrderUserTakeDelivery($order, false);
  398. if (!$res) {
  399. throw new ValidateException('订单号' . $order['order_id'] . '自动收货失败');
  400. }
  401. });
  402. } catch (\Throwable $e) {
  403. Log::error('自动收货失败,失败原因:' . $e->getMessage());
  404. }
  405. }
  406. }
  407. }