SharedProsperityUserRepository.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. <?php
  2. namespace app\common\repositories\shared;
  3. use app\common\dao\shared\SharedProsperityGroupUserDao;
  4. use app\common\dao\shared\SharedProsperityRewardRecordDao;
  5. use app\common\dao\shared\SharedProsperityUserDao;
  6. use app\common\dao\shared\SharedProsperityUserGroupLogDao;
  7. use app\common\dao\shared\SharedProsperityUserLevelLogDao;
  8. use app\common\dao\shared\SharedProsperityUserParentLogDao;
  9. use app\common\dao\user\UserDao;
  10. use app\common\dao\user\UserWithdrawalLimitRecordDao;
  11. use app\common\enum\CommonEnum;
  12. use app\common\enum\shared\SharedProsperityRewardRecordEnum;
  13. use app\common\enum\shared\SharedProsperityUserEnum;
  14. use app\common\model\shared\SharedProsperityGroupUser;
  15. use app\common\model\shared\SharedProsperityUserParentLog;
  16. use app\common\repositories\BaseRepository;
  17. use app\common\repositories\store\order\StoreCartRepository;
  18. use app\common\repositories\store\order\StoreOrderRepository;
  19. use app\entity\data\shared\SharedProsperityGroupUserEntity;
  20. use app\entity\data\shared\SharedProsperityRecommendConfigEntity;
  21. use app\entity\data\shared\SharedProsperityUserEntity;
  22. use think\db\exception\DbException;
  23. use think\facade\Db;
  24. class SharedProsperityUserRepository extends BaseRepository
  25. {
  26. protected $dao;
  27. /**
  28. * SharedProsperityUserRepository constructor.
  29. * @param SharedProsperityUserDao $dao
  30. */
  31. public function __construct(SharedProsperityUserDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * @param int $id
  37. * @return SharedProsperityUserEntity
  38. */
  39. public function getSharedProsperityUserEntityById(int $id): SharedProsperityUserEntity
  40. {
  41. $entityList = $this->getSharedProsperityUserEntityListByIdList([$id]);
  42. $entity = array_shift($entityList);
  43. if ($entity instanceof SharedProsperityUserEntity) {
  44. return $entity;
  45. } else {
  46. /** @var SharedProsperityUserEntity */
  47. return SharedProsperityUserEntity::newInstance();
  48. }
  49. }
  50. /**
  51. * @param array $idList
  52. * @return SharedProsperityUserEntity[]
  53. */
  54. public function getSharedProsperityUserEntityListByIdList(array $idList): array
  55. {
  56. return $this->dao->getSharedProsperityUserEntityListByIdList($idList);
  57. }
  58. /**
  59. * @param int $userId
  60. * @return SharedProsperityUserEntity
  61. */
  62. public function getSharedProsperityUserEntityByUserId(int $userId): SharedProsperityUserEntity
  63. {
  64. $entityList = $this->getSharedProsperityUserEntityListByUserIdList([$userId]);
  65. $entity = array_shift($entityList);
  66. if ($entity instanceof SharedProsperityUserEntity) {
  67. return $entity;
  68. } else {
  69. /** @var SharedProsperityUserEntity */
  70. return SharedProsperityUserEntity::newInstance();
  71. }
  72. }
  73. /**
  74. * @param array $userIdList
  75. * @return SharedProsperityUserEntity[]
  76. */
  77. public function getSharedProsperityUserEntityListByUserIdList(array $userIdList): array
  78. {
  79. return $this->dao->getSharedProsperityUserEntityListByUserIdList($userIdList);
  80. }
  81. /**
  82. * 获取直接下级用户ID列表
  83. * @param int $parentId 用户ID
  84. * @return SharedProsperityUserEntity[] 直接下级用户ID列表
  85. */
  86. public function getSharedProsperityUserEntityListByParentId(int $parentId): array
  87. {
  88. return $this->dao->getSharedProsperityUserEntityListByParentId($parentId);
  89. }
  90. /**
  91. * @param array $userIdList
  92. * @return SharedProsperityUserEntity[]
  93. */
  94. public function getSharedProsperityUserEntityMapByUserIdList(array $userIdList): array
  95. {
  96. $sharedProsperityUserEntityList = $this->dao->getSharedProsperityUserEntityListByUserIdList($userIdList);
  97. /** @var SharedProsperityUserEntity[] $sharedProsperityUserEntityMapByUserId */
  98. $sharedProsperityUserEntityMapByUserId = array_column($sharedProsperityUserEntityList, null, 'userId');
  99. return $sharedProsperityUserEntityMapByUserId;
  100. }
  101. public function create($data)
  102. {
  103. return $this->dao->create($data);
  104. }
  105. /**
  106. * @param SharedProsperityUserEntity $sharedProsperityUserEntity
  107. * @return SharedProsperityUserEntity
  108. */
  109. public function createByEntity(SharedProsperityUserEntity $sharedProsperityUserEntity): SharedProsperityUserEntity
  110. {
  111. $result = $this->create($sharedProsperityUserEntity->toUnderlineArray())->toArray();
  112. /** @var SharedProsperityUserEntity $entity */
  113. $entity = SharedProsperityUserEntity::newInstance($result);
  114. return $entity;
  115. }
  116. /**
  117. * @param $id
  118. * @param $data
  119. * @return int
  120. */
  121. public function update($id, $data): int
  122. {
  123. try {
  124. return $this->dao->update($id, $data);
  125. } catch (DbException $e) {
  126. return 0;
  127. }
  128. }
  129. public function updateByUserId($userId, $data): int
  130. {
  131. try {
  132. return $this->dao->updateByUserId($userId, $data);
  133. } catch (DbException $e) {
  134. return 0;
  135. }
  136. }
  137. /**
  138. * @param SharedProsperityUserEntity $sharedProsperityUserEntity
  139. * @return SharedProsperityUserEntity
  140. */
  141. public function updateByEntity(SharedProsperityUserEntity $sharedProsperityUserEntity): SharedProsperityUserEntity
  142. {
  143. if (empty($sharedProsperityUserEntity->getId())) {
  144. return SharedProsperityUserEntity::newInstance();
  145. }
  146. $result = $this->update($sharedProsperityUserEntity->getId(), $sharedProsperityUserEntity->toUnderlineArray());
  147. if ($result > 0) {
  148. return $this->getSharedProsperityUserEntityById($sharedProsperityUserEntity->getId());
  149. }
  150. return SharedProsperityUserEntity::newInstance();
  151. }
  152. /**
  153. * 绑定 上级
  154. * @param int $userId
  155. * @param int $parentId
  156. * @return bool
  157. */
  158. public function bindParentId(int $userId, int $parentId): bool
  159. {
  160. $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($userId);
  161. if (empty($sharedProsperityUserEntity->getUserId())) {
  162. return false;
  163. }
  164. $sharedProsperityUserEntityByParentId = $this->getSharedProsperityUserEntityByUserId($parentId);
  165. if (empty($sharedProsperityUserEntityByParentId->getUserId())) {
  166. return false;
  167. }
  168. $sharedProsperityUserEntity = $this->updateByEntity($sharedProsperityUserEntity->setParentId($sharedProsperityUserEntityByParentId->getUserId()));
  169. if (empty($sharedProsperityUserEntity->getId())) {
  170. return false;
  171. }
  172. return true;
  173. }
  174. /**
  175. * 获取直推用户列表,并计算每个直推用户的团队总人数和团队总业绩(包含自己)
  176. * 返回三个列表:全部直推、大部门(团队业绩最大的直推用户)、小部门(其余直推用户)
  177. * 同时返回统计数据:我的部门总人数、总业绩、小部门业绩总和、大部门业绩总和
  178. * @param int $userId
  179. * @return array
  180. */
  181. public function getTeam(int $userId): array
  182. {
  183. // 获取直推用户
  184. $list = $this->dao->getDirectByUserId($userId);
  185. if (empty($list)) {
  186. // 没有直推用户,仍然返回统计数据(我的部门总人数和总业绩)
  187. $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId);
  188. $myTeamTotalCount = count($myTeamUserIds);
  189. $myTeamPvSum = $this->dao->getTeamPvSumWithSelf($userId);
  190. return [
  191. 'all_direct' => [],
  192. 'big_department' => [],
  193. 'small_department' => [],
  194. 'stats' => [
  195. 'my_team_total_count' => $myTeamTotalCount,
  196. 'my_team_pv_sum' => $myTeamPvSum,
  197. 'small_department_pv_sum' => 0,
  198. 'big_department_pv_sum' => 0,
  199. ]
  200. ];
  201. }
  202. // 收集所有直推用户ID
  203. $userIds = array_column($list, 'user_id');
  204. // 获取用户详细信息(昵称、头像、注册时间)
  205. /** @var UserDao $userDao */
  206. $userDao = app()->make(UserDao::class);
  207. $userEntities = $userDao->getUserEntityListByUidList($userIds);
  208. $userMap = [];
  209. foreach ($userEntities as $userEntity) {
  210. $userMap[$userEntity->getUid()] = [
  211. 'nickname' => $userEntity->getNickname(),
  212. 'avatar' => $userEntity->getAvatar(),
  213. 'create_time' => $userEntity->getCreateTime(),
  214. ];
  215. }
  216. // 计算每个直推用户的团队总人数和团队总业绩
  217. $maxPv = 0;
  218. $maxPvIndex = -1;
  219. foreach ($list as $index => &$item) {
  220. $teamUserIds = $this->dao->getAllSubordinateUserIds($item['user_id'], false, 4);
  221. $teamTotalCount = count($teamUserIds);
  222. $teamPvSum = $this->dao->getTeamPvSum($item['user_id'], 4, true);
  223. $item['team_total_count'] = $teamTotalCount;
  224. $item['team_pv_sum'] = $teamPvSum;
  225. // 补充用户信息
  226. if (isset($userMap[$item['user_id']])) {
  227. $item['nickname'] = $userMap[$item['user_id']]['nickname'];
  228. $item['avatar'] = $userMap[$item['user_id']]['avatar'];
  229. $item['create_time'] = $userMap[$item['user_id']]['create_time'];
  230. } else {
  231. $item['nickname'] = '';
  232. $item['avatar'] = '';
  233. $item['create_time'] = '';
  234. }
  235. // 找出团队业绩最大的直推用户
  236. if ($teamPvSum > $maxPv) {
  237. $maxPv = $teamPvSum;
  238. $maxPvIndex = $index;
  239. }
  240. }
  241. unset($item);
  242. // 分割大部门和小部门
  243. $bigDepartment = [];
  244. $smallDepartment = [];
  245. if ($maxPvIndex >= 0) {
  246. $bigDepartment[] = $list[$maxPvIndex];
  247. foreach ($list as $index => $item) {
  248. if ($index !== $maxPvIndex) {
  249. $smallDepartment[] = $item;
  250. }
  251. }
  252. } else {
  253. // 没有直推用户(理论上不会发生)
  254. $smallDepartment = $list;
  255. }
  256. // 计算统计数据
  257. $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId);
  258. $myTeamTotalCount = count($myTeamUserIds);
  259. $myTeamPvSum = $this->dao->getTeamPvSum($userId);
  260. $smallDepartmentPvSum = 0;
  261. foreach ($smallDepartment as $item) {
  262. $smallDepartmentPvSum += $item['team_pv_sum'];
  263. }
  264. $bigDepartmentPvSum = 0;
  265. foreach ($bigDepartment as $item) {
  266. $bigDepartmentPvSum += $item['team_pv_sum'];
  267. }
  268. return [
  269. 'all_direct' => $list,
  270. 'big_department' => $bigDepartment,
  271. 'small_department' => $smallDepartment,
  272. 'stats' => [
  273. 'my_team_total_count' => $myTeamTotalCount,
  274. 'my_team_pv_sum' => $myTeamPvSum,
  275. 'small_department_pv_sum' => floor($smallDepartmentPvSum * 100) / 100,
  276. 'big_department_pv_sum' => $bigDepartmentPvSum,
  277. ]
  278. ];
  279. }
  280. /**
  281. * 批量更新
  282. * @param $dataList
  283. * @return array
  284. */
  285. public function batchUpdateSharedProsperityUserDataByDataList($dataList): array
  286. {
  287. return $this->dao->saveAll($dataList);
  288. }
  289. /**
  290. * 用户身份升级
  291. * @param array $userIdList
  292. * @return void
  293. */
  294. public function identityUpgrade(array $userIdList)
  295. {
  296. if (empty($userIdList)) {
  297. return;
  298. }
  299. /** @var SharedProsperityRecommendConfigRepository $sharedProsperityRecommendConfigRepository */
  300. $sharedProsperityRecommendConfigRepository = app()->make(SharedProsperityRecommendConfigRepository::class);
  301. $sharedProsperityRecommendConfigEntityList = $sharedProsperityRecommendConfigRepository->getList();
  302. usort($sharedProsperityRecommendConfigEntityList, function (SharedProsperityRecommendConfigEntity $a, SharedProsperityRecommendConfigEntity $b) {
  303. return $a->getPv() <=> $b->getPv(); // 从小到大排序
  304. });
  305. $sharedProsperityUserEntityList = $this->getSharedProsperityUserEntityListByUserIdList($userIdList);
  306. // 批量更新用户等级
  307. $this->batchUpgradeUsers($sharedProsperityUserEntityList, $sharedProsperityRecommendConfigEntityList);
  308. // foreach ($sharedProsperityUserEntityList as $sharedProsperityUserEntity) {
  309. // $partnerLevel = 0;
  310. // foreach ($sharedProsperityRecommendConfigEntityList as $sharedProsperityRecommendConfigEntity) {
  311. // if ($sharedProsperityUserEntity->getPv() >= $sharedProsperityRecommendConfigEntity->getPv()) {
  312. // $partnerLevel = $sharedProsperityRecommendConfigEntity->getLevel();
  313. // } else {
  314. // // 如果本次验证的等级结果大于 之前的等级,说明用户升级了 需要更新等级
  315. // if ($partnerLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
  316. // $this->updateByEntity(
  317. // SharedProsperityUserEntity::newInstance()
  318. // ->setId($sharedProsperityUserEntity->getId())
  319. // ->setPartnerLevel($partnerLevel)
  320. // );
  321. // }
  322. // break;
  323. // }
  324. // }
  325. // }
  326. }
  327. /**
  328. * 批量更新用户等级
  329. * @param SharedProsperityUserEntity[] $sharedProsperityUserEntityList 用户实体列表
  330. * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
  331. * @return void
  332. */
  333. private function batchUpgradeUsers(array $sharedProsperityUserEntityList, array $sharedProsperityRecommendConfigEntityList)
  334. {
  335. foreach ($sharedProsperityUserEntityList as $sharedProsperityUserEntity) {
  336. // // 计算用户的团队业绩(向下5级)
  337. // $teamPv = $this->calculateTeamPv($sharedProsperityUserEntity->getId());
  338. // 根据团队业绩确定等级
  339. // $newLevel = $this->calculateLevelByTeamPv($teamPv, $sharedProsperityRecommendConfigEntityList);
  340. //
  341. // // 如果新等级大于当前等级,则更新
  342. // if ($newLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
  343. // $this->updateByEntity(
  344. // SharedProsperityUserEntity::newInstance()
  345. // ->setId($sharedProsperityUserEntity->getId())
  346. // ->setPartnerLevel($newLevel)
  347. // );
  348. // }
  349. if ($sharedProsperityUserEntity->getPartnerLevel() == 0) {
  350. // 记录用户当前的 用户等级
  351. $partnerLevel = $sharedProsperityUserEntity->getPartnerLevel();
  352. // 新增需求 凡是 共富区,购买过商品ID 58635 的 ‘普通用户’ 升级为 ‘1星合伙人’
  353. /** @var StoreCartRepository $storeCartRepository */
  354. $storeCartRepository = app()->make(StoreCartRepository::class);
  355. $storeCartEntityList = $storeCartRepository->getCartEntityListByUidAndProductIdListAndIsPay($sharedProsperityUserEntity->getUserId(), [58635]);
  356. if (!empty($storeCartEntityList)) {
  357. $cartIdList = array_column($storeCartEntityList, 'cartId');
  358. if (!empty($cartIdList)) {
  359. /** @var StoreOrderRepository $storeOrderRepository */
  360. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  361. $storeOrderEntityList = $storeOrderRepository->getStoreOrderEntityListByCartIdListAndIsPay($cartIdList);
  362. if (!empty($storeOrderEntityList)) {
  363. // 修改用户的等级
  364. $sharedProsperityUserEntity->setPartnerLevel(1);
  365. }
  366. }
  367. }
  368. // 暂时新增加逻辑 用户购买 298 商品后,自动升级为1星合伙人,目前只有消费者 能够获得PV值,在奖励结算时,判断订单金额 是否大于 298 并且 用户身份为 用户,则升级为一星合伙人
  369. // 逻辑修改,如果用户的身份 是普通用户,则统计他在共富区内 所有消费的金额,是否超 每天自增的 升级限制金额,如果超过则升级为1星合伙人
  370. /** @var StoreOrderRepository $storeOrderRepository */
  371. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  372. $sumTotalPrice = $storeOrderRepository->getSumTotalPriceByUidAndMerId($sharedProsperityUserEntity->getUserId(), CommonEnum::DESIGN_MERCHANT_ID['SharedProsperity']['code']);
  373. if (!empty($sumTotalPrice)) {
  374. /** @var SharedProsperityRecommendConfigRepository $sharedProsperityRecommendConfigRepository */
  375. $sharedProsperityRecommendConfigRepository = app()->make(SharedProsperityRecommendConfigRepository::class);
  376. $sharedProsperityRecommendConfigEntityList = $sharedProsperityRecommendConfigRepository->getList();
  377. usort($sharedProsperityRecommendConfigEntityList, function (SharedProsperityRecommendConfigEntity $a, SharedProsperityRecommendConfigEntity $b) {
  378. return $a->getLevel() <=> $b->getLevel(); // 从小到大排序
  379. });
  380. foreach ($sharedProsperityRecommendConfigEntityList as $sharedProsperityRecommendConfigEntity) {
  381. if (is_null($sharedProsperityRecommendConfigEntity->getConsume())) {
  382. continue;
  383. }
  384. if ($sumTotalPrice >= $sharedProsperityRecommendConfigEntity->getConsume() && $sharedProsperityUserEntity->getPartnerLevel() < $sharedProsperityRecommendConfigEntity->getLevel()) {
  385. // 修改用户的等级
  386. $sharedProsperityUserEntity->setPartnerLevel($sharedProsperityRecommendConfigEntity->getLevel());
  387. }
  388. }
  389. }
  390. // 也就是说 升级了 保存到数据库
  391. if ($partnerLevel != $sharedProsperityUserEntity->getPartnerLevel()) {
  392. $this->updateByEntity(
  393. SharedProsperityUserEntity::newInstance()
  394. ->setId($sharedProsperityUserEntity->getId())
  395. ->setPartnerLevel($sharedProsperityUserEntity->getPartnerLevel())
  396. );
  397. }
  398. }
  399. // 根据原来的团队升级规则,去升级推荐人等级
  400. $this->upgradeUpperLevels($sharedProsperityUserEntity->getUserId(), $sharedProsperityRecommendConfigEntityList);
  401. // 新的升级规则
  402. // 如果当前用户升级了 那要看一下他的上级 是否也需要升级
  403. if (!empty($sharedProsperityUserEntity->getParentId())) {
  404. $sharedProsperityUserEntityByParent = $this->getSharedProsperityUserEntityByUserId($sharedProsperityUserEntity->getParentId());
  405. if (!empty($sharedProsperityUserEntityByParent->getId()) && $sharedProsperityUserEntityByParent->getPartnerLevel() < 2) {
  406. $this->upgradeUpperLevelsByPromotionOne($sharedProsperityUserEntityByParent);
  407. }
  408. }
  409. }
  410. }
  411. /**
  412. * 向上追溯更新上级身份等级(最多向上5层)
  413. * @param int $userId 当前用户ID
  414. * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
  415. * @return void
  416. */
  417. private function upgradeUpperLevels(int $userId, array $sharedProsperityRecommendConfigEntityList)
  418. {
  419. $maxLevels = SharedProsperityUserEnum::TEAM_DEPTH;
  420. $currentUserId = $userId;
  421. for ($i = 0; $i < $maxLevels; $i++) {
  422. // 获取上级用户ID
  423. $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($currentUserId);
  424. $parentId = $sharedProsperityUserEntity->getParentId();
  425. if (empty($parentId)) {
  426. break; // 没有上级,停止追溯
  427. }
  428. // 重新计算上级的团队业绩和等级
  429. $this->upgradeSingleUser($parentId, $sharedProsperityRecommendConfigEntityList);
  430. // 继续向上追溯
  431. $currentUserId = $parentId;
  432. }
  433. }
  434. /**
  435. * 升级单个用户身份
  436. * @param int $userId 用户ID
  437. * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
  438. * @return void
  439. */
  440. private function upgradeSingleUser(int $userId, array $sharedProsperityRecommendConfigEntityList)
  441. {
  442. // 获取用户实体
  443. $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($userId);
  444. if (empty($sharedProsperityUserEntity->getId())) {
  445. return;
  446. }
  447. // 计算团队业绩
  448. $teamPvMap = $this->calculateTeamPv($userId);
  449. // 当前只计算团队业绩的 小团队业绩
  450. $maxKey = array_keys($teamPvMap, max($teamPvMap))[0];
  451. unset($teamPvMap[$maxKey]);
  452. $teamPvBySmallSum = array_sum($teamPvMap);
  453. // 计算新等级
  454. $newLevel = $this->calculateLevelByTeamPv($teamPvBySmallSum, $sharedProsperityRecommendConfigEntityList);
  455. // 如果新等级大于当前等级,则更新
  456. if ($newLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
  457. $this->updateByEntity(
  458. SharedProsperityUserEntity::newInstance()
  459. ->setId($sharedProsperityUserEntity->getId())
  460. ->setPartnerLevel($newLevel)
  461. );
  462. }
  463. }
  464. /**
  465. * 计算用户的团队业绩(向下5级)
  466. * @param int $userId 用户ID
  467. * @return array 团队业绩
  468. */
  469. private function calculateTeamPv(int $userId): array
  470. {
  471. $teamPvMap = [];
  472. // 获取所有下级用户ID(向下5级)
  473. // 第一层级在这里查询 因为要统计大小区
  474. $sharedProsperityUserEntityListByParentId = $this->getSharedProsperityUserEntityListByParentId($userId);
  475. // 循环 直属下级
  476. foreach ($sharedProsperityUserEntityListByParentId as $sharedProsperityUserEntityByUnderling) {
  477. $departmentTeamUserIdList = $this->getUserIdListByParentIdForDepth($sharedProsperityUserEntityByUnderling->getUserId(), SharedProsperityUserEnum::TEAM_DEPTH - 1);
  478. $departmentTeamUserIdList[] = $sharedProsperityUserEntityByUnderling->getUserId();
  479. // 直属团队的 业绩总和
  480. $teamPv = '0.00';
  481. if (!empty($departmentTeamUserIdList)) {
  482. // 批量获取下级用户的个人业绩并求和
  483. // 假设有方法批量获取用户的PV
  484. $sharedProsperityUserEntityListByTeam = $this->getSharedProsperityUserEntityListByUserIdList($departmentTeamUserIdList);
  485. foreach ($sharedProsperityUserEntityListByTeam as $sharedProsperityUserEntityByTeam) {
  486. $teamPv = bcadd($teamPv, (string)$sharedProsperityUserEntityByTeam->getPv(), 2);
  487. }
  488. }
  489. $teamPvMap[$sharedProsperityUserEntityByUnderling->getUserId()] = $teamPv;
  490. }
  491. return $teamPvMap;
  492. }
  493. /**
  494. * 根据团队业绩计算等级
  495. * @param float $teamPv 团队业绩
  496. * @param array $configEntityList 等级配置列表
  497. * @return int 等级
  498. */
  499. private function calculateLevelByTeamPv(float $teamPv, array $configEntityList): int
  500. {
  501. $level = 0;
  502. foreach ($configEntityList as $configEntity) {
  503. if ($teamPv >= $configEntity->getPv()) {
  504. $level = $configEntity->getLevel();
  505. } else {
  506. break;
  507. }
  508. }
  509. return $level;
  510. }
  511. /**
  512. * 获取下级用户ID列表(向下指定层级)
  513. * @param int $parentId 用户ID
  514. * @param int $maxLevel 最大层级数
  515. * @param int $currentLevel 当前层级(内部递归使用)
  516. * @return array 下级用户ID列表
  517. */
  518. private function getUserIdListByParentIdForDepth(int $parentId, int $maxLevel, int $currentLevel = 1): array
  519. {
  520. if ($currentLevel > $maxLevel) {
  521. return [];
  522. }
  523. $subordinateIds = [];
  524. // 获取直接下级用户ID列表(假设有方法获取直接下级)
  525. $sharedProsperityUserEntityListByParentId = $this->getSharedProsperityUserEntityListByParentId($parentId);
  526. if (empty($sharedProsperityUserEntityListByParentId)) {
  527. return [];
  528. }
  529. // 添加直接下级
  530. $subordinateIds = array_merge($subordinateIds, array_column($sharedProsperityUserEntityListByParentId, 'userId'));
  531. // 递归获取下级的下级
  532. foreach ($sharedProsperityUserEntityListByParentId as $sharedProsperityUserEntity) {
  533. $nestedSubordinateIds = $this->getUserIdListByParentIdForDepth($sharedProsperityUserEntity->getUserId(), $maxLevel, $currentLevel + 1);
  534. if (!empty($nestedSubordinateIds)) {
  535. $subordinateIds = array_merge($subordinateIds, $nestedSubordinateIds);
  536. }
  537. }
  538. return array_unique($subordinateIds);
  539. }
  540. /**
  541. * 获取用户树
  542. * @param int $userId 用户ID
  543. * @return array 用户树,格式:[{name: '张三', parent: null, children: [...]}]
  544. */
  545. public function getUserTree($userId)
  546. {
  547. /** @var SharedProsperityUserDao $shareUserDao */
  548. $shareUserDao = app()->make(SharedProsperityUserDao::class);
  549. // 获取所有下级用户ID(包括自己)
  550. $subordinateIds = $shareUserDao->getAllSubordinateUserIds($userId, true);
  551. if (empty($subordinateIds)) {
  552. return [];
  553. }
  554. // 获取这些共富用户的基本信息(包含parent_id)
  555. $sharedUsers = $shareUserDao->getSharedProsperityUserEntityListByUserIdList($subordinateIds);
  556. if (empty($sharedUsers)) {
  557. return [];
  558. }
  559. // 用户身份
  560. $partnerLevel = [
  561. 0 => '用户',
  562. 1 => '一星合伙人',
  563. 2 => '二星合伙人',
  564. 3 => '三星合伙人',
  565. 4 => '钻石合伙人',
  566. 5 => '总裁合伙人',
  567. 6 => '联席主席团',
  568. ];
  569. // 获取用户昵称映射
  570. $userIds = array_map(function ($entity) {
  571. return $entity->getUserId();
  572. }, $sharedUsers);
  573. /** @var UserDao $userDao */
  574. $userDao = app()->make(UserDao::class);
  575. $userInfos = $userDao->getUserEntityListByUidList($userIds);
  576. $idToName = [];
  577. foreach ($userInfos as $userInfo) {
  578. $sharedUserInfo = $shareUserDao->getSharedProsperityUserEntityListByUserIdList([$userInfo->getUid()]);
  579. $idToName[$userInfo->getUid()] = $userInfo->getNickname() . '(' . $userInfo->account . ')(身份:' . $partnerLevel[$sharedUserInfo[0]->getPartnerLevel()] . ')' ?? ('用户' . $userInfo->getUid());
  580. }
  581. // 构建节点数组
  582. $nodes = [];
  583. foreach ($sharedUsers as $sharedUser) {
  584. $nodes[] = [
  585. 'user_id' => $sharedUser->getUserId(),
  586. 'parent_id' => $sharedUser->getParentId(),
  587. 'name' => $idToName[$sharedUser->getUserId()] ?? ('用户' . $sharedUser->getUserId()),
  588. ];
  589. }
  590. // 构建树,以传入的用户为根节点
  591. $rootNode = $this->findNode($nodes, $userId);
  592. if (!$rootNode) {
  593. return [];
  594. }
  595. $tree = [
  596. 'name' => $rootNode['name'],
  597. 'parent' => null,
  598. 'children' => $this->buildSharedTree($nodes, $userId)
  599. ];
  600. return [$tree];
  601. }
  602. /**
  603. * 递归构建共富用户树
  604. * @param array $nodes 节点数组,每个节点包含 user_id, parent_id, name
  605. * @param int $parentId 父ID
  606. * @return array
  607. */
  608. private function buildSharedTree($nodes, $parentId)
  609. {
  610. $branch = [];
  611. foreach ($nodes as $node) {
  612. if ($node['parent_id'] == $parentId) {
  613. $children = $this->buildSharedTree($nodes, $node['user_id']);
  614. $branch[] = [
  615. 'name' => $node['name'],
  616. 'parent' => $this->findNodeName($nodes, $parentId) ?? null,
  617. 'children' => $children
  618. ];
  619. }
  620. }
  621. return $branch;
  622. }
  623. /**
  624. * 根据user_id查找节点名称
  625. * @param array $nodes
  626. * @param int $userId
  627. * @return string|null
  628. */
  629. private function findNodeName($nodes, $userId)
  630. {
  631. foreach ($nodes as $node) {
  632. if ($node['user_id'] == $userId) {
  633. return $node['name'];
  634. }
  635. }
  636. return null;
  637. }
  638. /**
  639. * 根据user_id查找节点
  640. * @param array $nodes
  641. * @param int $userId
  642. * @return array|null
  643. */
  644. private function findNode($nodes, $userId)
  645. {
  646. foreach ($nodes as $node) {
  647. if ($node['user_id'] == $userId) {
  648. return $node;
  649. }
  650. }
  651. return null;
  652. }
  653. public function adminList($params)
  654. {
  655. $page = $params['page'] ?? 1;
  656. $where = [];
  657. /** @var UserDao $userDao */
  658. $userDao = app()->make(UserDao::class);
  659. // 手机号搜索
  660. if (isset($params['account']) && $params['account']) {
  661. $params['account'] = '%' . $params['account'] . '%';
  662. $userData = $userDao->accountLikeByUser($params['account']);
  663. $uids = [];
  664. if ($userData) {
  665. $uids = array_column($userData, 'uid');
  666. }
  667. $where[] = ['a.user_id', 'in', $uids];
  668. }
  669. // 昵称搜索
  670. if (isset($params['nickname']) && $params['nickname']) {
  671. $params['nickname'] = '%' . $params['nickname'] . '%';
  672. $userData = $userDao->nicknameLikeByUser($params['nickname']);
  673. $uids = [];
  674. if ($userData) {
  675. $uids = array_column($userData, 'uid');
  676. }
  677. $where[] = ['a.user_id', 'in', $uids];
  678. }
  679. // 合伙人等级搜索
  680. if (isset($params['partner_level']) && $params['partner_level']) {
  681. $where[] = ['a.partner_level', '=', $params['partner_level']];
  682. }
  683. // 共富团等级搜索
  684. if (isset($params['user_group']) && $params['user_group']) {
  685. // /** @var SharedProsperityGroupUserDao $groupUserDao */
  686. // $groupUserDao = app()->make(SharedProsperityGroupUserDao::class);
  687. // $groupUserData = $groupUserDao->getUserByLevel($params['user_group']);
  688. // $uids = array_column($groupUserData['data'], 'user_id');
  689. // $where[] = ['user_id', 'in', $uids];
  690. // $page = 1;
  691. $where[] = ['b.group_id', '=', $params['user_group']];
  692. }
  693. $list = $this->dao->adminList($where, $page);
  694. return $list;
  695. }
  696. /**
  697. * @param $userId
  698. * @param $level
  699. * @return int
  700. * @author 史晨
  701. * @date 2026/2/27 14:23
  702. * 更新用户等级
  703. */
  704. public function updateLevel($userId, $level, $operator, $remark)
  705. {
  706. $userInfo = $this->dao->getWhere(['user_id' => $userId]);
  707. if ($level == $userInfo->partner_level) {
  708. return '等级未发生变化';
  709. }
  710. Db::startTrans();
  711. try {
  712. // 变更等级
  713. $this->dao->updateByUserId($userId, ['partner_level' => $level]);
  714. // 记录日志
  715. /** @var SharedProsperityUserLevelLogDao $logDao */
  716. $logDao = app()->make(SharedProsperityUserLevelLogDao::class);
  717. $logDao->create([
  718. 'user_id' => $userId,
  719. 'old_level' => $userInfo->partner_level,
  720. 'new_level' => $level,
  721. 'operator' => $operator,
  722. 'remark' => $remark
  723. ]);
  724. Db::commit();
  725. } catch (\Exception $e) {
  726. Db::rollback();
  727. return $e->getMessage();
  728. }
  729. }
  730. /**
  731. * @param $userId
  732. * @param $parentId
  733. * @param $remark
  734. * @param $operator
  735. * @return string|void
  736. * @throws DbException
  737. * @throws \think\db\exception\DataNotFoundException
  738. * @throws \think\db\exception\ModelNotFoundException
  739. * @author 史晨
  740. * @date 2026/3/16 09:35
  741. * 更新用户上级
  742. */
  743. public function updateParent($userId, $parentId, $remark, $operator)
  744. {
  745. if ($userId == $parentId) {
  746. return '不能设置自己为上级';
  747. }
  748. $userInfo = $this->dao->getWhere(['user_id' => $userId]);
  749. if ($parentId == $userInfo->parent_id) {
  750. return '上级未发生变化';
  751. }
  752. Db::startTrans();
  753. try {
  754. // 变更等级
  755. $this->dao->updateByUserId($userId, ['parent_id' => $parentId]);
  756. // 记录日志
  757. /** @var SharedProsperityUserParentLogDao $logDao */
  758. $logDao = app()->make(SharedProsperityUserParentLogDao::class);
  759. $logDao->create([
  760. 'user_id' => $userId,
  761. 'old_parent' => $userInfo->parent_id,
  762. 'new_parent' => $parentId,
  763. 'operator' => $operator,
  764. 'remark' => $remark
  765. ]);
  766. Db::commit();
  767. } catch (\Exception $e) {
  768. Db::rollback();
  769. return $e->getMessage();
  770. }
  771. }
  772. /**
  773. * @param $userId
  774. * @param $groupId
  775. * @param $remark
  776. * @param $operator
  777. * @return string|void
  778. * @author 史晨
  779. * @date 2026/3/17 17:19
  780. * 更新用户分组
  781. */
  782. public function updateGroup($userId, $groupId, $remark, $operator)
  783. {
  784. $userGroup = SharedProsperityGroupUser::where(['user_id' => $userId, 'status' => 1])->find();
  785. $userGroupId = $userGroup->group_id??0;
  786. Db::startTrans();
  787. try {
  788. // 变更等级
  789. if ($userGroup) {
  790. $update = [
  791. 'group_id' => $groupId,
  792. 'status' => 1
  793. ];
  794. SharedProsperityGroupUser::update($update, ['user_id' => $userId]);
  795. }else{
  796. $insert = [
  797. 'user_id' => $userId,
  798. 'group_id' => $groupId,
  799. 'status' => 1
  800. ];
  801. SharedProsperityGroupUser::create($insert);
  802. }
  803. // 记录日志
  804. /** @var SharedProsperityUserGroupLogDao $logDao */
  805. $logDao = app()->make(SharedProsperityUserGroupLogDao::class);
  806. $logDao->create([
  807. 'user_id' => $userId,
  808. 'old_group' => $userGroupId,
  809. 'new_group' => $groupId,
  810. 'operator' => $operator,
  811. 'remark' => $remark
  812. ]);
  813. Db::commit();
  814. } catch (\Exception $e) {
  815. Db::rollback();
  816. return $e->getMessage();
  817. }
  818. }
  819. /**
  820. * @param $userId
  821. * @param $type
  822. * @param $number
  823. * @return bool
  824. * @author 史晨
  825. * @date 2026/2/27 15:16
  826. * 更新用户奖励
  827. */
  828. public function updateReward($userId, $type, $number, $remark)
  829. {
  830. // 使用枚举常量代替硬编码数字
  831. $typeConstants = SharedProsperityRewardRecordEnum::TYPE;
  832. $CONSUMER_STOCKS_TYPE = $typeConstants['CONSUMER_STOCKS']['code'] ?? 1001;
  833. $WITHDRAWAL_LIMIT_TYPE = $typeConstants['WITHDRAWAL_LIMIT']['code'] ?? 1002;
  834. $PV_TYPE = $typeConstants['PV']['code'] ?? 1003;
  835. $map = [
  836. $CONSUMER_STOCKS_TYPE => 'consumer_stocks',
  837. $WITHDRAWAL_LIMIT_TYPE => 'withdrawal_limit',
  838. $PV_TYPE => 'pv'
  839. ];
  840. try {
  841. Db::startTrans();
  842. // 计算收支类型 (pm)
  843. $pm = $number >= 0 ? 1 : 0;
  844. $absNumber = abs($number);
  845. $currentTime = date('Y-m-d H:i:s');
  846. if (isset($map[$type])) {
  847. if ($type == $WITHDRAWAL_LIMIT_TYPE) {
  848. // 处理提现额度
  849. $this->handleWithdrawalLimit($userId, $number, $pm, $absNumber, $remark);
  850. } else {
  851. // 处理消费股或PV值
  852. $this->handleSharedProsperityReward($userId, $type, $map[$type], $number, $pm, $absNumber, $remark, $currentTime);
  853. }
  854. } else {
  855. // 处理佣金
  856. $this->handleBrokerage($userId, $number, $pm, $absNumber, $remark, $currentTime);
  857. }
  858. Db::commit();
  859. return true;
  860. } catch (\Exception $e) {
  861. Db::rollback();
  862. return false;
  863. }
  864. }
  865. /**
  866. * 处理提现额度更新
  867. * @param int $userId 用户ID
  868. * @param float $number 变动金额
  869. * @param int $pm 收支类型 (1:收入, 0:支出)
  870. * @param float $absNumber 变动金额绝对值
  871. * @param string $remark 备注
  872. * @return void
  873. */
  874. private function handleWithdrawalLimit($userId, $number, $pm, $absNumber, $remark)
  875. {
  876. /** @var UserDao $userDao */
  877. $userDao = app()->make(UserDao::class);
  878. // 更新提现额度
  879. $userDao->updateWithdrawalLimitByUid($userId, $number);
  880. // 获取更新后的余额
  881. $userData = $userDao->getWhere(['uid' => $userId]);
  882. $balance = $userData->withdrawal_limit;
  883. // 记录日志
  884. $insertData = [
  885. 'user_id' => $userId,
  886. 'pm' => $pm,
  887. 'number' => $absNumber,
  888. 'balance' => $balance,
  889. 'status' => 1,
  890. 'type_shop' => 0,
  891. 'take_time' => date('Y-m-d H:i:s'),
  892. 'remark' => $remark
  893. ];
  894. /** @var UserWithdrawalLimitRecordDao $withdrawalRecordDao */
  895. $withdrawalRecordDao = app()->make(UserWithdrawalLimitRecordDao::class);
  896. $withdrawalRecordDao->create($insertData);
  897. }
  898. /**
  899. * 处理共富体系奖励(消费股/PV值)
  900. * @param int $userId 用户ID
  901. * @param int $type 奖励类型
  902. * @param string $field 数据库字段名
  903. * @param float $number 变动金额
  904. * @param int $pm 收支类型 (1:收入, 0:支出)
  905. * @param float $absNumber 变动金额绝对值
  906. * @param string $remark 备注
  907. * @param string $currentTime 当前时间
  908. * @return void
  909. */
  910. private function handleSharedProsperityReward($userId, $type, $field, $number, $pm, $absNumber, $remark, $currentTime)
  911. {
  912. // 使用枚举常量
  913. $typeConstants = SharedProsperityRewardRecordEnum::TYPE;
  914. $CONSUMER_STOCKS_TYPE = $typeConstants['CONSUMER_STOCKS']['code'] ?? 1001;
  915. $PV_TYPE = $typeConstants['PV']['code'] ?? 1003;
  916. // 更新数据
  917. $this->dao->updateRewardByUserId($userId, $field, $number);
  918. // 获取更新后的余额
  919. $userData = $this->dao->getWhere(['user_id' => $userId]);
  920. // 根据类型确定余额字段
  921. if ($type == $CONSUMER_STOCKS_TYPE) {
  922. $balance = $userData->consumer_stocks;
  923. } elseif ($type == $PV_TYPE) {
  924. $balance = $userData->pv;
  925. } else {
  926. $balance = 0;
  927. }
  928. // 记录日志
  929. $insertData = [
  930. 'user_id' => $userId,
  931. 'type' => $type,
  932. 'pm' => $pm,
  933. 'number' => $absNumber,
  934. 'balance' => $balance,
  935. 'status' => SharedProsperityRewardRecordEnum::STATUS['VALID']['code'],
  936. 'type_shop' => SharedProsperityRewardRecordEnum::TYPE_SHOP['Platform']['code'],
  937. 'order_sn' => 0,
  938. 'take_time' => $currentTime,
  939. 'remark' => $remark,
  940. 'create_time' => $currentTime,
  941. 'update_time' => $currentTime,
  942. ];
  943. /** @var SharedProsperityRewardRecordDao $rewardRecordDao */
  944. $rewardRecordDao = app()->make(SharedProsperityRewardRecordDao::class);
  945. $rewardRecordDao->insertAll([$insertData]);
  946. }
  947. /**
  948. * 处理佣金更新
  949. * @param int $userId 用户ID
  950. * @param float $number 变动金额
  951. * @param int $pm 收支类型 (1:收入, 0:支出)
  952. * @param float $absNumber 变动金额绝对值
  953. * @param string $remark 备注
  954. * @param string $currentTime 当前时间
  955. * @return void
  956. */
  957. private function handleBrokerage($userId, $number, $pm, $absNumber, $remark, $currentTime)
  958. {
  959. /** @var UserDao $userDao */
  960. $userDao = app()->make(UserDao::class);
  961. // 更新佣金
  962. $userDao->updateBrokeragePriceByUid($userId, $number);
  963. // 获取更新后的余额
  964. $userData = $userDao->getWhere(['uid' => $userId]);
  965. $balance = $userData->brokerage_price;
  966. // 记录账单
  967. $bill = [
  968. 'uid' => $userId,
  969. 'link_id' => 0,
  970. 'pm' => $pm,
  971. 'title' => $remark,
  972. 'category' => 'now_money',
  973. 'type' => 'commission',
  974. 'number' => $absNumber,
  975. 'balance' => $balance,
  976. 'mark' => $remark,
  977. 'create_time' => $currentTime,
  978. 'status' => 1,
  979. 'commission_type' => CommonEnum::COMMISSION_TYPE['SHARED_PROSPERITY_PARTNER_COMMISSION']['code'],
  980. 'order_sn' => 0,
  981. 'tripartite' => 0,
  982. 'type_shop' => 0,
  983. 'mer_id' => 0,
  984. 'source' => 0,
  985. 'order_type' => 1,
  986. 'is_red_brokerage' => 0,
  987. 'gzc' => 3,
  988. 'take_time' => time(),
  989. 'district_id' => '',
  990. 'street_id' => '',
  991. 'month' => '',
  992. ];
  993. Db::name('user_bill')->insert($bill);
  994. }
  995. /**
  996. * 第一次促销的 身份 升级方法
  997. * @param SharedProsperityUserEntity $sharedProsperityUserEntity
  998. * @return void
  999. */
  1000. public function upgradeUpperLevelsByPromotionOne(SharedProsperityUserEntity $sharedProsperityUserEntity)
  1001. {
  1002. // 判断当前用户 是否在共富区的消费值 > 298,如果不满足则不能升级
  1003. /** @var StoreOrderRepository $storeOrderRepository */
  1004. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  1005. $sumTotalPrice = $storeOrderRepository->getSumTotalPriceByUidAndMerId($sharedProsperityUserEntity->getUserId(), CommonEnum::DESIGN_MERCHANT_ID['SharedProsperity']['code']);
  1006. if ($sumTotalPrice < 298) {
  1007. return;
  1008. }
  1009. // 查询当前用户的直属下级
  1010. $sharedProsperityUserEntityListByChild = $this->getSharedProsperityUserEntityListByParentId($sharedProsperityUserEntity->getUserId());
  1011. // 一星合伙人数量
  1012. $oneStarPartner = 0;
  1013. // 二星合伙人数量
  1014. $twoStarPartner = 0;
  1015. foreach ($sharedProsperityUserEntityListByChild as $sharedProsperityUserEntityByChild) {
  1016. if ($sharedProsperityUserEntityByChild->getPartnerLevel() >= 1) {
  1017. $oneStarPartner++;
  1018. if ($sharedProsperityUserEntityByChild->getPartnerLevel() >= 2) {
  1019. $twoStarPartner++;
  1020. }
  1021. }
  1022. }
  1023. // 推荐 6 个 2星以上的合伙人 增加万人团身份
  1024. if ($twoStarPartner >= 6) {
  1025. /** @var SharedProsperityGroupUserRepository $sharedProsperityGroupUserRepository */
  1026. $sharedProsperityGroupUserRepository = app()->make(SharedProsperityGroupUserRepository::class);
  1027. $sharedProsperityGroupUserEntity = $sharedProsperityGroupUserRepository->getSharedProsperityGroupUserEntityByUserId($sharedProsperityUserEntity->getUserId());
  1028. if (empty($sharedProsperityGroupUserEntity->getId())) {
  1029. // 如果没有共富团身份 则增加共富团 万人团身份
  1030. $sharedProsperityGroupUserRepository->createByEntity(
  1031. SharedProsperityGroupUserEntity::newInstance()
  1032. ->setUserId($sharedProsperityUserEntity->getUserId())
  1033. ->setGroupId(1)
  1034. ->setStatus(1)
  1035. );
  1036. }
  1037. }
  1038. // 推荐 6 个 1星以上的合伙人 则升级为 2 星合伙人
  1039. if ($sharedProsperityUserEntity->getPartnerLevel() < 2 && $oneStarPartner >= 6) {
  1040. $this->updateByEntity(
  1041. SharedProsperityUserEntity::newInstance()
  1042. ->setId($sharedProsperityUserEntity->getId())
  1043. ->setPartnerLevel(2)
  1044. );
  1045. // 如果当前用户升级了 那要看一下他的上级 是否也需要升级
  1046. if (!empty($sharedProsperityUserEntity->getParentId())) {
  1047. $sharedProsperityUserEntityByParent = $this->getSharedProsperityUserEntityByUserId($sharedProsperityUserEntity->getParentId());
  1048. if (!empty($sharedProsperityUserEntityByParent->getId()) && $sharedProsperityUserEntityByParent->getPartnerLevel() < 2) {
  1049. $this->upgradeUpperLevelsByPromotionOne($sharedProsperityUserEntityByParent);
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }