SharedProsperityUserRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\common\repositories\shared;
  3. use app\common\dao\shared\SharedProsperityUserDao;
  4. use app\common\dao\user\UserDao;
  5. use app\common\enum\CommonEnum;
  6. use app\common\repositories\BaseRepository;
  7. use app\entity\data\shared\SharedProsperityRecommendConfigEntity;
  8. use app\entity\data\shared\SharedProsperityUserEntity;
  9. use think\db\exception\DbException;
  10. class SharedProsperityUserRepository extends BaseRepository
  11. {
  12. protected $dao;
  13. /**
  14. * SharedProsperityUserRepository constructor.
  15. * @param SharedProsperityUserDao $dao
  16. */
  17. public function __construct(SharedProsperityUserDao $dao)
  18. {
  19. $this->dao = $dao;
  20. }
  21. /**
  22. * @param int $id
  23. * @return SharedProsperityUserEntity
  24. */
  25. public function getSharedProsperityUserEntityById(int $id): SharedProsperityUserEntity
  26. {
  27. $entityList = $this->getSharedProsperityUserEntityListByIdList([$id]);
  28. $entity = array_shift($entityList);
  29. if ($entity instanceof SharedProsperityUserEntity) {
  30. return $entity;
  31. } else {
  32. /** @var SharedProsperityUserEntity */
  33. return SharedProsperityUserEntity::newInstance();
  34. }
  35. }
  36. /**
  37. * @param array $idList
  38. * @return SharedProsperityUserEntity[]
  39. */
  40. public function getSharedProsperityUserEntityListByIdList(array $idList): array
  41. {
  42. return $this->dao->getSharedProsperityUserEntityListByIdList($idList);
  43. }
  44. /**
  45. * @param int $userId
  46. * @return SharedProsperityUserEntity
  47. */
  48. public function getSharedProsperityUserEntityByUserId(int $userId): SharedProsperityUserEntity
  49. {
  50. $entityList = $this->getSharedProsperityUserEntityListByUserIdList([$userId]);
  51. $entity = array_shift($entityList);
  52. if ($entity instanceof SharedProsperityUserEntity) {
  53. return $entity;
  54. } else {
  55. /** @var SharedProsperityUserEntity */
  56. return SharedProsperityUserEntity::newInstance();
  57. }
  58. }
  59. /**
  60. * @param array $userIdList
  61. * @return SharedProsperityUserEntity[]
  62. */
  63. public function getSharedProsperityUserEntityListByUserIdList(array $userIdList): array
  64. {
  65. return $this->dao->getSharedProsperityUserEntityListByUserIdList($userIdList);
  66. }
  67. /**
  68. * @param array $userIdList
  69. * @return SharedProsperityUserEntity[]
  70. */
  71. public function getSharedProsperityUserEntityMapByUserIdList(array $userIdList): array
  72. {
  73. $sharedProsperityUserEntityList = $this->dao->getSharedProsperityUserEntityListByUserIdList($userIdList);
  74. /** @var SharedProsperityUserEntity[] $sharedProsperityUserEntityMapByUserId */
  75. $sharedProsperityUserEntityMapByUserId = array_column($sharedProsperityUserEntityList, null, 'userId');
  76. return $sharedProsperityUserEntityMapByUserId;
  77. }
  78. public function create($data)
  79. {
  80. return $this->dao->create($data);
  81. }
  82. /**
  83. * @param SharedProsperityUserEntity $sharedProsperityUserEntity
  84. * @return SharedProsperityUserEntity
  85. */
  86. public function createByEntity(SharedProsperityUserEntity $sharedProsperityUserEntity): SharedProsperityUserEntity
  87. {
  88. $result = $this->create($sharedProsperityUserEntity->toUnderlineArray())->toArray();
  89. /** @var SharedProsperityUserEntity $entity */
  90. $entity = SharedProsperityUserEntity::newInstance($result);
  91. return $entity;
  92. }
  93. /**
  94. * @param $id
  95. * @param $data
  96. * @return int
  97. */
  98. public function update($id, $data): int
  99. {
  100. try {
  101. return $this->dao->update($id, $data);
  102. } catch (DbException $e) {
  103. return 0;
  104. }
  105. }
  106. public function updateByUserId($userId,$data)
  107. {
  108. try {
  109. return $this->dao->updateByUserId($userId, $data);
  110. } catch (DbException $e) {
  111. return 0;
  112. }
  113. }
  114. /**
  115. * @param SharedProsperityUserEntity $sharedProsperityUserEntity
  116. * @return SharedProsperityUserEntity
  117. */
  118. public function updateByEntity(SharedProsperityUserEntity $sharedProsperityUserEntity): SharedProsperityUserEntity
  119. {
  120. if (empty($sharedProsperityUserEntity->getId())) {
  121. return SharedProsperityUserEntity::newInstance();
  122. }
  123. $result = $this->update($sharedProsperityUserEntity->getId(), $sharedProsperityUserEntity->toUnderlineArray());
  124. if ($result > 0) {
  125. return $this->getSharedProsperityUserEntityById($sharedProsperityUserEntity->getId());
  126. }
  127. return SharedProsperityUserEntity::newInstance();
  128. }
  129. /**
  130. * 绑定 上级
  131. * @param int $userId
  132. * @param int $parentId
  133. * @return bool
  134. */
  135. public function bindParentId(int $userId, int $parentId): bool
  136. {
  137. $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($userId);
  138. if (empty($sharedProsperityUserEntity->getUserId())) {
  139. return false;
  140. }
  141. $sharedProsperityUserEntityByParentId = $this->getSharedProsperityUserEntityByUserId($parentId);
  142. if (empty($sharedProsperityUserEntityByParentId->getUserId())) {
  143. return false;
  144. }
  145. $sharedProsperityUserEntity = $this->updateByEntity($sharedProsperityUserEntity->setParentId($sharedProsperityUserEntityByParentId->getUserId()));
  146. if (empty($sharedProsperityUserEntity->getId())) {
  147. return false;
  148. }
  149. return true;
  150. }
  151. /**
  152. * 获取直推用户列表,并计算每个直推用户的团队总人数和团队总业绩(包含自己)
  153. * 返回三个列表:全部直推、大部门(团队业绩最大的直推用户)、小部门(其余直推用户)
  154. * 同时返回统计数据:我的部门总人数、总业绩、小部门业绩总和、大部门业绩总和
  155. * @param int $userId
  156. * @return array
  157. */
  158. public function getTeam(int $userId): array
  159. {
  160. // 获取直推用户
  161. $list = $this->dao->getDirectByUserId($userId);
  162. if (empty($list)) {
  163. // 没有直推用户,仍然返回统计数据(我的部门总人数和总业绩)
  164. $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId, true);
  165. $myTeamTotalCount = count($myTeamUserIds);
  166. $myTeamPvSum = $this->dao->getTeamPvSumWithSelf($userId);
  167. return [
  168. 'all_direct' => [],
  169. 'big_department' => [],
  170. 'small_department' => [],
  171. 'stats' => [
  172. 'my_team_total_count' => $myTeamTotalCount,
  173. 'my_team_pv_sum' => $myTeamPvSum,
  174. 'small_department_pv_sum' => 0,
  175. 'big_department_pv_sum' => 0,
  176. ]
  177. ];
  178. }
  179. // 收集所有直推用户ID
  180. $userIds = array_column($list, 'user_id');
  181. // 获取用户详细信息(昵称、头像、注册时间)
  182. /** @var UserDao $userDao */
  183. $userDao = app()->make(UserDao::class);
  184. $userEntities = $userDao->getUserEntityListByUidList($userIds);
  185. $userMap = [];
  186. foreach ($userEntities as $userEntity) {
  187. $userMap[$userEntity->getUid()] = [
  188. 'nickname' => $userEntity->getNickname(),
  189. 'avatar' => $userEntity->getAvatar(),
  190. 'create_time' => $userEntity->getCreateTime(),
  191. ];
  192. }
  193. // 计算每个直推用户的团队总人数和团队总业绩
  194. $maxPv = 0;
  195. $maxPvIndex = -1;
  196. foreach ($list as $index => &$item) {
  197. $teamUserIds = $this->dao->getAllSubordinateUserIds($item['user_id'], true);
  198. $teamTotalCount = count($teamUserIds);
  199. $teamPvSum = $this->dao->getTeamPvSumWithSelf($item['user_id']);
  200. $item['team_total_count'] = $teamTotalCount;
  201. $item['team_pv_sum'] = $teamPvSum;
  202. // 补充用户信息
  203. if (isset($userMap[$item['user_id']])) {
  204. $item['nickname'] = $userMap[$item['user_id']]['nickname'];
  205. $item['avatar'] = $userMap[$item['user_id']]['avatar'];
  206. $item['create_time'] = $userMap[$item['user_id']]['create_time'];
  207. } else {
  208. $item['nickname'] = '';
  209. $item['avatar'] = '';
  210. $item['create_time'] = '';
  211. }
  212. // 找出团队业绩最大的直推用户
  213. if ($teamPvSum > $maxPv) {
  214. $maxPv = $teamPvSum;
  215. $maxPvIndex = $index;
  216. }
  217. }
  218. unset($item);
  219. // 分割大部门和小部门
  220. $bigDepartment = [];
  221. $smallDepartment = [];
  222. if ($maxPvIndex >= 0) {
  223. $bigDepartment[] = $list[$maxPvIndex];
  224. foreach ($list as $index => $item) {
  225. if ($index !== $maxPvIndex) {
  226. $smallDepartment[] = $item;
  227. }
  228. }
  229. } else {
  230. // 没有直推用户(理论上不会发生)
  231. $smallDepartment = $list;
  232. }
  233. // 计算统计数据
  234. $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId, true);
  235. $myTeamTotalCount = count($myTeamUserIds);
  236. $myTeamPvSum = $this->dao->getTeamPvSumWithSelf($userId);
  237. $smallDepartmentPvSum = 0;
  238. foreach ($smallDepartment as $item) {
  239. $smallDepartmentPvSum += $item['team_pv_sum'];
  240. }
  241. $bigDepartmentPvSum = 0;
  242. foreach ($bigDepartment as $item) {
  243. $bigDepartmentPvSum += $item['team_pv_sum'];
  244. }
  245. return [
  246. 'all_direct' => $list,
  247. 'big_department' => $bigDepartment,
  248. 'small_department' => $smallDepartment,
  249. 'stats' => [
  250. 'my_team_total_count' => $myTeamTotalCount,
  251. 'my_team_pv_sum' => $myTeamPvSum,
  252. 'small_department_pv_sum' => $smallDepartmentPvSum,
  253. 'big_department_pv_sum' => $bigDepartmentPvSum,
  254. ]
  255. ];
  256. }
  257. /**
  258. * 批量更新
  259. * @param $dataList
  260. * @return array
  261. */
  262. public function batchUpdateSharedProsperityUserDataByDataList($dataList): array
  263. {
  264. return $this->dao->saveAll($dataList);
  265. }
  266. /**
  267. * 用户身份升级
  268. * @param array $userIdList
  269. * @return void
  270. */
  271. public function identityUpgrade(array $userIdList)
  272. {
  273. $sharedProsperityUserEntityList = $this->getSharedProsperityUserEntityListByUserIdList($userIdList);
  274. /** @var SharedProsperityRecommendConfigRepository $sharedProsperityRecommendConfigRepository */
  275. $sharedProsperityRecommendConfigRepository = app()->make(SharedProsperityRecommendConfigRepository::class);
  276. $sharedProsperityRecommendConfigEntityList = $sharedProsperityRecommendConfigRepository->getList();
  277. usort($sharedProsperityRecommendConfigEntityList, function (SharedProsperityRecommendConfigEntity $a, SharedProsperityRecommendConfigEntity $b) {
  278. return $a->getPv() <=> $b->getPv(); // 从小到大排序
  279. });
  280. foreach ($sharedProsperityUserEntityList as $sharedProsperityUserEntity) {
  281. $partnerLevel = 0;
  282. foreach ($sharedProsperityRecommendConfigEntityList as $sharedProsperityRecommendConfigEntity) {
  283. if ($sharedProsperityUserEntity->getPv() >= $sharedProsperityRecommendConfigEntity->getPv()) {
  284. $partnerLevel = $sharedProsperityRecommendConfigEntity->getLevel();
  285. } else {
  286. // 如果本次验证的等级结果大于 之前的等级,说明用户升级了 需要更新等级
  287. if ($partnerLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
  288. $this->updateByEntity(
  289. SharedProsperityUserEntity::newInstance()
  290. ->setId($sharedProsperityUserEntity->getId())
  291. ->setPartnerLevel($partnerLevel)
  292. );
  293. }
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. }