StoreOrderSuccessServices.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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\services\order;
  12. use app\adminapi\controller\v1\marketing\StoreCouponUser;
  13. use app\dao\order\StoreOrderDao;
  14. use app\model\user\User;
  15. use app\services\activity\coupon\StoreCouponIssueServices;
  16. use app\services\activity\lottery\LuckLotteryServices;
  17. use app\services\activity\combination\StorePinkServices;
  18. use app\services\BaseServices;
  19. use app\services\pay\PayServices;
  20. use app\services\user\UserBrokerageServices;
  21. use app\services\user\UserServices;
  22. use crmeb\exceptions\ApiException;
  23. /**
  24. * Class StoreOrderSuccessServices
  25. * @package app\services\order
  26. * @method getOne(array $where, ?string $field = '*', array $with = []) 获取去一条数据
  27. */
  28. class StoreOrderSuccessServices extends BaseServices
  29. {
  30. /**
  31. *
  32. * StoreOrderSuccessServices constructor.
  33. * @param StoreOrderDao $dao
  34. */
  35. public function __construct(StoreOrderDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * 0元支付
  41. * @param array $orderInfo
  42. * @param int $uid
  43. * @return bool
  44. * @throws \think\Exception
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function zeroYuanPayment(array $orderInfo, int $uid, string $payType = PayServices::YUE_PAY)
  50. {
  51. if ($orderInfo['paid']) {
  52. throw new ApiException(410265);
  53. }
  54. return $this->paySuccess($orderInfo, $payType);//余额支付成功
  55. }
  56. /**
  57. * 支付成功
  58. * @param array $orderInfo
  59. * @param string $paytype
  60. * @param array $other
  61. * @return bool
  62. * @throws \Psr\SimpleCache\InvalidArgumentException
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function paySuccess(array $orderInfo, string $paytype = PayServices::WEIXIN_PAY, array $other = [])
  68. {
  69. $updata = ['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()];
  70. $orderInfo['pay_time'] = $updata['pay_time'];
  71. $orderInfo['pay_type'] = $paytype;
  72. if ($other && isset($other['trade_no'])) {
  73. $updata['trade_no'] = $other['trade_no'];
  74. }
  75. /** @var StoreOrderCartInfoServices $orderInfoServices */
  76. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  77. $orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  78. $res1 = $this->dao->update($orderInfo['id'], $updata);
  79. $resPink = true;
  80. if ($orderInfo['combination_id'] && $res1 && !$orderInfo['refund_status']) {
  81. /** @var StorePinkServices $pinkServices */
  82. $pinkServices = app()->make(StorePinkServices::class);
  83. /** @var StoreOrderServices $orderServices */
  84. $orderServices = app()->make(StoreOrderServices::class);
  85. $resPink = $pinkServices->createPink($orderServices->tidyOrder($orderInfo, true));//创建拼团
  86. }
  87. //缓存抽奖次数 除过线下支付
  88. if (isset($orderInfo['pay_type']) && $orderInfo['pay_type'] != 'offline') {
  89. /** @var LuckLotteryServices $luckLotteryServices */
  90. $luckLotteryServices = app()->make(LuckLotteryServices::class);
  91. $luckLotteryServices->setCacheLotteryNum((int)$orderInfo['uid'], 'order');
  92. }
  93. $orderInfo['send_name'] = $orderInfo['real_name'];
  94. //订单支付成功后置事件
  95. event('OrderPaySuccessListener', [$orderInfo]);
  96. //用户推送消息事件
  97. event('NoticeListener', [$orderInfo, 'order_pay_success']);
  98. //支付成功给客服发送消息
  99. event('NoticeListener', [$orderInfo, 'admin_pay_success_code']);
  100. // 推送订单
  101. event('OutPushListener', ['order_pay_push', ['order_id' => (int)$orderInfo['id']]]);
  102. // 小程序订单管理 (自提商品)
  103. if ($orderInfo['shipping_type'] == 2) {
  104. event('OrderShipping', ['product', $orderInfo, 4, '', '']);
  105. }
  106. $res = $res1 && $resPink;
  107. if ($orderInfo['order_type']==1){ //礼包商品订单赠送复购商品优惠卷
  108. $grantNum = sys_config('repeat_discount_num',0);
  109. @file_put_contents("quanju.txt", json_encode(['uid'=>$orderInfo['uid'],'grantNum'=>$grantNum])."-礼包订单赠送复购折扣次数\r\n", 8);
  110. if ($grantNum > 0){
  111. /** @var UserServices $userServices */
  112. $userServices = app()->make(UserServices::class);
  113. $userServices->bcInc($orderInfo['uid'], 'repeat_discount_num', $grantNum, 'uid');
  114. @file_put_contents("quanju.txt", json_encode(['uid'=>$orderInfo['uid'],'grantNum'=>$grantNum])."-礼包订单赠送复购折扣次数\r\n", 8);
  115. }
  116. }
  117. $spread_id = User::where('uid',$orderInfo['uid'])->value('spread_uid');
  118. if ($spread_id>0){ //上级分销奖励
  119. self::spreadReward($spread_id,$orderInfo);
  120. self::issueServiceFee($orderInfo['uid'],$orderInfo);
  121. }
  122. return false !== $res;
  123. }
  124. /**
  125. * 发放优惠券到指定个人
  126. * @return mixed
  127. */
  128. public function grantUser($id,$uid)
  129. {
  130. $data['id'] = $id;
  131. $data['uid'] = $uid;
  132. if (!$data['id']) return app('json')->fail(100100);
  133. /** @var StoreCouponIssueServices $issueService */
  134. $issueService = app()->make(StoreCouponIssueServices::class);
  135. $coupon = $issueService->get($data['id']);
  136. if (!$coupon) {
  137. return app('json')->fail(100026);
  138. } else {
  139. $coupon = $coupon->toArray();
  140. }
  141. $user = explode(',', $data['uid']);
  142. $issueService->setCoupon($coupon, $user);
  143. return true;
  144. }
  145. // 发放分销奖励()
  146. public function spreadReward($spread_uid, $orderInfo){
  147. try {
  148. // 获取上级的分销等级
  149. $agent_level = User::where('uid', $spread_uid)->value('agent_level');
  150. if (!$agent_level) {
  151. return false;
  152. }
  153. // 获取分销等级详细信息
  154. $agentLevelInfo = \app\model\agent\AgentLevel::where('id', $agent_level)->find();
  155. if (!$agentLevelInfo) {
  156. return false;
  157. }
  158. $agentLevelInfo = $agentLevelInfo->toArray();
  159. // 发放优惠券奖励
  160. if ($agentLevelInfo['free_issue'] > 0) {
  161. $this->grantUser($agentLevelInfo['free_issue'], $spread_uid);
  162. }
  163. // 发放佣金奖励
  164. if ($agentLevelInfo['spread_brokerage'] > 0) {
  165. // 计算佣金金额:订单价格 * spread_brokerage / 100
  166. $brokerageAmount = bcmul((string)$orderInfo['total_price'], (string)($agentLevelInfo['spread_brokerage'] / 100), 2);
  167. if ($brokerageAmount > 0) {
  168. /** @var UserServices $userServices */
  169. $userServices = app()->make(UserServices::class);
  170. /** @var UserBrokerageServices $userBrokerageServices */
  171. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  172. $balance = $userServices->value(['uid' => $spread_uid], 'brokerage_price');
  173. $nickname = User::where('uid',$orderInfo['uid'])->value('nickname');
  174. $userBrokerageServices->income('get_brokerage', $spread_uid, [
  175. 'nickname' => $nickname,
  176. 'pay_price' => $orderInfo['total_price'],
  177. 'number' => $brokerageAmount,
  178. ], bcadd((string)$balance, (string)$brokerageAmount, 2), $orderInfo['id'] ?? 0);
  179. $userServices->bcInc($spread_uid, 'brokerage_price', $brokerageAmount, 'uid');
  180. // 发放平级奖励
  181. $this->issueSameLevelReward($spread_uid, $brokerageAmount);
  182. }
  183. }
  184. return true;
  185. } catch (\Exception $e) {
  186. @file_put_contents("quanju.txt", json_encode($e->getMessage())."-spreadReward报错内容\r\n", 8);
  187. @file_put_contents("quanju.txt", json_encode($e->getLine())."-spreadReward报错位置\r\n", 8);
  188. @file_put_contents("quanju.txt", json_encode($e->getFile())."-spreadReward报错文件\r\n", 8);
  189. throw $e;
  190. }
  191. }
  192. /**
  193. * 发放礼包/复购服务费
  194. * 下级用户购买礼包或复购商品后,向上查找分销等级为V2或大于V2的用户,发放服务费
  195. * @param int $uid 购买用户的UID
  196. * @param array $orderInfo 订单信息
  197. * @return bool
  198. */
  199. public function issueServiceFee($uid, $orderInfo){
  200. try {
  201. // 根据订单类型获取服务费金额
  202. $orderType = isset($orderInfo['order_type']) ? (int)$orderInfo['order_type'] : 0;
  203. $serviceAmount = 0;
  204. if ($orderType === 1) {
  205. // 礼包订单:使用系统配置的服务费金额
  206. $serviceAmount = floatval(sys_config('service_gift', 0));
  207. } else {
  208. // 复购订单:根据订单金额计算服务费(订单金额 ÷ 50)
  209. $orderPrice = isset($orderInfo['pay_price']) ? floatval($orderInfo['pay_price']) : 0;
  210. if ($orderPrice > 0) {
  211. $basePrice = floatval(sys_config('service_repeat', 5)); // 单个50元对应的服务费,默认5元
  212. $units = intval($orderPrice / 50); // 计算包含多少个50元
  213. $serviceAmount = $units * $basePrice;
  214. @file_put_contents("quanju.txt", json_encode(['orderPrice'=>$orderPrice,'units'=>$units,'basePrice'=>$basePrice,'serviceAmount'=>$serviceAmount])."-复购服务费计算\r\n", 8);
  215. }
  216. }
  217. if ($serviceAmount <= 0) {
  218. return false;
  219. }
  220. // 获取购买用户的昵称
  221. $nickname = User::where('uid', $uid)->value('nickname');
  222. // 获取分销等级表中grade >= 2的等级ID(V2及以上)
  223. $qualifiedLevels = \app\model\agent\AgentLevel::where('grade', '>=', 3)
  224. ->where('is_del', 0)
  225. ->where('status', 1)
  226. ->column('id');
  227. // 调试日志:记录符合条件的等级ID
  228. @file_put_contents("quanju.txt", json_encode($qualifiedLevels)."-issueServiceFee符合条件等级ID列表\r\n", 8);
  229. if (empty($qualifiedLevels)) {
  230. return false;
  231. }
  232. // 从购买用户开始,向上查找第一个V2或以上等级的用户
  233. $currentUid = $uid;
  234. while ($currentUid > 0) {
  235. $spreadUid = User::where('uid', $currentUid)->value('spread_uid');
  236. if (!$spreadUid || $spreadUid <= 0) {
  237. break;
  238. }
  239. $spreadAgentLevelId = User::where('uid', $spreadUid)->value('agent_level');
  240. // 调试日志:记录检查的用户和等级
  241. @file_put_contents("quanju.txt", json_encode(['spreadUid'=>$spreadUid,'agent_level'=>$spreadAgentLevelId])."-issueServiceFee检查用户等级\r\n", 8);
  242. if (!$spreadAgentLevelId) {
  243. $currentUid = $spreadUid;
  244. continue;
  245. }
  246. // 检查上级的分销等级是否在V2及以上
  247. if (in_array($spreadAgentLevelId, $qualifiedLevels)) {
  248. // 找到了,发放服务费
  249. @file_put_contents("quanju.txt", json_encode(['spreadUid'=>$spreadUid,'agent_level'=>$spreadAgentLevelId,'amount'=>$serviceAmount])."-issueServiceFee发放服务费\r\n", 8);
  250. /** @var UserServices $userServices */
  251. $userServices = app()->make(UserServices::class);
  252. /** @var UserBrokerageServices $userBrokerageServices */
  253. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  254. $incomeType = $orderType === 1 ? 'service_gift' : 'service_repeat';
  255. $balance = $userServices->value(['uid' => $spreadUid], 'brokerage_price');
  256. $userBrokerageServices->income($incomeType, $spreadUid, [
  257. 'nickname' => $nickname,
  258. 'pay_price' => $orderInfo['pay_price'] ?? 0,
  259. 'number' => $serviceAmount,
  260. ], bcadd((string)$balance, (string)$serviceAmount, 2), $orderInfo['id'] ?? 0);
  261. $userServices->bcInc($spreadUid, 'brokerage_price', $serviceAmount, 'uid');
  262. // 找到后停止查找
  263. break;
  264. }
  265. $currentUid = $spreadUid;
  266. }
  267. return true;
  268. } catch (\Exception $e) {
  269. @file_put_contents("quanju.txt", json_encode($e->getMessage())."-issueServiceFee报错内容\r\n", 8);
  270. @file_put_contents("quanju.txt", json_encode($e->getLine())."-issueServiceFee报错位置\r\n", 8);
  271. @file_put_contents("quanju.txt", json_encode($e->getFile())."-issueServiceFee报错文件\r\n", 8);
  272. throw $e;
  273. }
  274. }
  275. /**
  276. * 发放核销订单线路奖励 定时任务
  277. * 针对分销等级为4的用户,统计其直推下级团队中门店核销订单数量
  278. * 当两条线都达到200单时,奖励verific_reward金额
  279. * 当第三条线达到200单时,再奖励more_reward金额
  280. * 第四条及以后达到200单,每次奖励more_reward金额
  281. * @param int $spread_uid 分销等级为4的用户UID
  282. * @return bool
  283. */
  284. public function verifyLineReward($spread_uid){
  285. try {
  286. // 获取用户的分销等级ID
  287. $agent_level_id = User::where('uid', $spread_uid)->value('agent_level');
  288. if (!$agent_level_id) {
  289. return false;
  290. }
  291. // 获取分销等级的grade字段
  292. $agentLevelInfo = \app\model\agent\AgentLevel::where('id', $agent_level_id)->find();
  293. if (!$agentLevelInfo) {
  294. return false;
  295. }
  296. $agentGrade = $agentLevelInfo->grade;
  297. // 只处理分销等级为4的用户
  298. if ($agentGrade != 4) {
  299. return false;
  300. }
  301. // 获取该用户的所有直推下级
  302. $directUsers = User::where('spread_uid', $spread_uid)
  303. ->column('uid');
  304. if (empty($directUsers)) {
  305. return false;
  306. }
  307. // 收集所有需要统计的用户(包括直推下级及其所有下级)
  308. $allLineUsers = [];
  309. foreach ($directUsers as $directUid) {
  310. // 包含直推下级本人及其所有下级
  311. $lineUsers = array_merge([$directUid], $this->getAllTeamUsers($directUid));
  312. $allLineUsers[] = $lineUsers;
  313. }
  314. @file_put_contents("quanju.txt", json_encode($allLineUsers) . "下级团队统计\r\n", 8);
  315. if (empty($allLineUsers)) {
  316. return false;
  317. }
  318. // 统计每条线的核销订单数量
  319. $lineVerifyCounts = [];
  320. foreach ($allLineUsers as $lineUsers) {
  321. if (empty($lineUsers)) {
  322. $lineVerifyCounts[] = 0;
  323. continue;
  324. }
  325. // 查询这些用户拥有的门店的核销订单总量
  326. $totalVerifyNum = \app\model\system\store\SystemStore::whereIn('uid', $lineUsers)
  327. ->sum('verify_num');
  328. $lineVerifyCounts[] = (int)$totalVerifyNum;
  329. }
  330. @file_put_contents("quanju.txt", json_encode($lineVerifyCounts) . "统计达到200单的线路数量\r\n", 8);
  331. // 统计达到200单的线路数量
  332. $qualifiedLines = 0;
  333. foreach ($lineVerifyCounts as $count) {
  334. if ($count >= 200) {
  335. $qualifiedLines++;
  336. }
  337. }
  338. if ($qualifiedLines < 2) {
  339. return false;
  340. }
  341. // 获取奖励配置
  342. $verificReward = sys_config('verific_reward', 0);
  343. $moreReward = sys_config('more_reward', 0);
  344. if ($verificReward <= 0 && $moreReward <= 0) {
  345. return false;
  346. }
  347. // 查询已发放的 verific_reward 次数(line_two_reward)
  348. $issuedVerificCount = \app\model\user\UserBrokerage::where('uid', $spread_uid)
  349. ->where('type', 'line_two_reward')
  350. ->count();
  351. // 查询已发放的 more_reward 次数(line_more_reward)
  352. $issuedMoreCount = \app\model\user\UserBrokerage::where('uid', $spread_uid)
  353. ->where('type', 'line_more_reward')
  354. ->count();
  355. // 计算应该发放的奖励
  356. $verificRewardCount = 0; // verific_reward应该发放的次数(最多1次)
  357. $moreRewardCount = 0; // more_reward应该发放的次数
  358. if ($qualifiedLines >= 2) {
  359. $verificRewardCount = 1;
  360. // 超过2条线,每多一条线发放一次more_reward
  361. $moreRewardCount = $qualifiedLines - 2;
  362. }
  363. // 判断是否需要发放奖励
  364. $needVerificReward = $verificRewardCount > $issuedVerificCount;
  365. $needMoreReward = $moreRewardCount > $issuedMoreCount;
  366. if (!$needVerificReward && !$needMoreReward) {
  367. return false;
  368. }
  369. /** @var UserServices $userServices */
  370. $userServices = app()->make(UserServices::class);
  371. /** @var UserBrokerageServices $userBrokerageServices */
  372. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  373. $balance = $userServices->value(['uid' => $spread_uid], 'brokerage_price');
  374. // 发放verific_reward(两条线达标奖励)
  375. if ($needVerificReward && $verificReward > 0) {
  376. $userBrokerageServices->income('line_two_reward', $spread_uid, [
  377. // 'pay_price' => 0,
  378. 'number' => $verificReward,
  379. ], bcadd((string)$balance, (string)$verificReward, 2), 0);
  380. $userServices->bcInc($spread_uid, 'brokerage_price', $verificReward, 'uid');
  381. $balance = bcadd((string)$balance, (string)$verificReward, 2);
  382. // 发放平级奖励
  383. $this->issueSameLevelReward($spread_uid, $verificReward);
  384. }
  385. // 发放more_reward(第三条及以上线达标,可能多次)
  386. if ($needMoreReward && $moreReward > 0) {
  387. $needIssueMoreCount = $moreRewardCount - $issuedMoreCount;
  388. for ($i = 0; $i < $needIssueMoreCount; $i++) {
  389. $userBrokerageServices->income('line_more_reward', $spread_uid, [
  390. // 'pay_price' => 0,
  391. 'number' => $moreReward,
  392. ], bcadd((string)$balance, (string)$moreReward, 2), 0);
  393. $userServices->bcInc($spread_uid, 'brokerage_price', $moreReward, 'uid');
  394. $balance = bcadd((string)$balance, (string)$moreReward, 2);
  395. // 发放平级奖励
  396. $this->issueSameLevelReward($spread_uid, $moreReward);
  397. }
  398. }
  399. return true;
  400. } catch (\Exception $e) {
  401. @file_put_contents("quanju.txt", json_encode($e->getMessage())."-verifyLineReward报错内容\r\n", 8);
  402. @file_put_contents("quanju.txt", json_encode($e->getLine())."-verifyLineReward报错位置\r\n", 8);
  403. @file_put_contents("quanju.txt", json_encode($e->getFile())."-verifyLineReward报错文件\r\n", 8);
  404. throw $e;
  405. }
  406. }
  407. /**
  408. * 获取用户的所有下级(递归查询团队)
  409. * @param int $uid 用户UID
  410. * @return array 所有下级UID数组
  411. */
  412. private function getAllTeamUsers($uid){
  413. $allUsers = [];
  414. $this->getTeamUsersRecursive($uid, $allUsers);
  415. return $allUsers;
  416. }
  417. /**
  418. * 递归获取用户的所有下级
  419. * @param int $uid 当前用户UID
  420. * @param array &$allUsers 已收集的用户数组(引用传递)
  421. */
  422. private function getTeamUsersRecursive($uid, &$allUsers){
  423. // 查询该用户的直接下级
  424. $directUsers = User::where('spread_uid', $uid)
  425. ->column('uid');
  426. if (empty($directUsers)) {
  427. return;
  428. }
  429. foreach ($directUsers as $userUid) {
  430. $allUsers[] = $userUid;
  431. // 递归查询该用户的下级
  432. $this->getTeamUsersRecursive($userUid, $allUsers);
  433. }
  434. }
  435. /**
  436. * 发放平级奖励
  437. * 只查找第一级上级,如果上级的分销等级等于指定等级,发放平级奖励
  438. * @param int $rewardUid 获得奖励的用户UID
  439. * @param float $rewardAmount 奖励金额
  440. * @param int $agentLevelId 分销等级ID
  441. * @param float $sameLevelRatio 平级奖励比例
  442. * @return bool
  443. */
  444. public function sameLevelReward($rewardUid, $rewardAmount, $agentLevelId, $sameLevelRatio){
  445. try {
  446. if ($rewardAmount <= 0 || $sameLevelRatio <= 0) {
  447. return false;
  448. }
  449. // 只查找第一级上级
  450. $spreadUid = User::where('uid', $rewardUid)->value('spread_uid');
  451. if (!$spreadUid || $spreadUid <= 0) {
  452. return false;
  453. }
  454. // 查询上级的分销等级
  455. $spreadAgentLevelId = User::where('uid', $spreadUid)->value('agent_level');
  456. if (!$spreadAgentLevelId || $spreadAgentLevelId != $agentLevelId) {
  457. return false;
  458. }
  459. /** @var UserServices $userServices */
  460. $userServices = app()->make(UserServices::class);
  461. /** @var UserBrokerageServices $userBrokerageServices */
  462. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  463. // 计算平级奖励金额:奖励金额 * 平级比例 / 100
  464. $sameLevelAmount = bcmul((string)$rewardAmount, (string)($sameLevelRatio / 100), 2);
  465. if ($sameLevelAmount <= 0) {
  466. return false;
  467. }
  468. // 发放平级奖励
  469. $balance = $userServices->value(['uid' => $spreadUid], 'brokerage_price');
  470. $userBrokerageServices->income('get_same_level_reward', $spreadUid, [
  471. 'pay_price' => 0,
  472. 'number' => $sameLevelAmount,
  473. ], bcadd((string)$balance, (string)$sameLevelAmount, 2), 0);
  474. $userServices->bcInc($spreadUid, 'brokerage_price', $sameLevelAmount, 'uid');
  475. return true;
  476. } catch (\Exception $e) {
  477. @file_put_contents("quanju.txt", json_encode($e->getMessage())."-sameLevelReward报错内容\r\n", 8);
  478. @file_put_contents("quanju.txt", json_encode($e->getLine())."-sameLevelReward报错位置\r\n", 8);
  479. @file_put_contents("quanju.txt", json_encode($e->getFile())."-sameLevelReward报错文件\r\n", 8);
  480. throw $e;
  481. }
  482. }
  483. /**
  484. * 发放平级奖励(内部调用方法)
  485. * 查询所有设置平级奖的等级并发放奖励
  486. * @param int $rewardUid 获得奖励的用户UID
  487. * @param float $rewardAmount 奖励金额
  488. * @return bool
  489. */
  490. private function issueSameLevelReward($rewardUid, $rewardAmount){
  491. try {
  492. // 查询所有设置了平级奖的分销等级(equal_award > 0)
  493. $agentLevelsWithAward = \app\model\agent\AgentLevel::where('equal_award', '>', 0)
  494. ->column('equal_award', 'id');
  495. if (empty($agentLevelsWithAward)) {
  496. return false;
  497. }
  498. // 获取奖励用户的分销等级
  499. $rewardAgentLevelId = \app\model\user\User::where('uid', $rewardUid)->value('agent_level');
  500. if (!$rewardAgentLevelId || !isset($agentLevelsWithAward[$rewardAgentLevelId])) {
  501. return false;
  502. }
  503. // 只发一次:只查询奖励用户上级中分销等级相同的,发放平级奖
  504. $this->sameLevelReward($rewardUid, $rewardAmount, $rewardAgentLevelId, $agentLevelsWithAward[$rewardAgentLevelId]);
  505. return true;
  506. } catch (\Exception $e) {
  507. @file_put_contents("quanju.txt", json_encode($e->getMessage())."-issueSameLevelReward报错内容\r\n", 8);
  508. @file_put_contents("quanju.txt", json_encode($e->getLine())."-issueSameLevelReward报错位置\r\n", 8);
  509. @file_put_contents("quanju.txt", json_encode($e->getFile())."-issueSameLevelReward报错文件\r\n", 8);
  510. throw $e;
  511. }
  512. }
  513. }