MemberClanServer.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Servers;
  3. use App\Jobs\MemberShareJob;
  4. use App\Models\Member;
  5. use App\Models\MemberBoth;
  6. use App\Models\MemberClan;
  7. use Illuminate\Support\Facades\DB;
  8. /**
  9. * 会员
  10. */
  11. class MemberClanServer
  12. {
  13. /**
  14. * 错误信息
  15. * @var string
  16. */
  17. private $errorMsg = '';
  18. static private $server = '';
  19. private function __construct()
  20. {
  21. }
  22. /**
  23. * 创建对象
  24. * @return MemberClanServer
  25. */
  26. static function creatServer()
  27. {
  28. if (empty(self::$server)) {
  29. self::$server = new MemberClanServer();
  30. }
  31. return self::$server;
  32. }
  33. /**
  34. * 创建会员关系
  35. * @param Member $member
  36. * @throws \Exception
  37. */
  38. function creatRelevance(Member $member)
  39. {
  40. $p_clan = MemberClan::where('m_id', $member->{'recom_id'})->first();
  41. $member_clan = [
  42. 'm_id' => $member->{'id'},
  43. 'tier_num' => $p_clan->{'tier_num'} + 1,
  44. 'one_m_id' => $p_clan->{'m_id'},
  45. 'two_m_id' => $p_clan->{'one_m_id'},
  46. 'three_m_id' => $p_clan->{'two_m_id'},
  47. 'four_m_id' => $p_clan->{'three_m_id'},
  48. 'five_m_id' => $p_clan->{'four_m_id'},
  49. 'm_ids' => 0,
  50. 'p_ids' => $p_clan->{'p_ids'} . ',' . $p_clan->{'m_id'},
  51. ];
  52. $clan_ret = MemberClan::create($member_clan);
  53. if (!empty($clan_ret)) {
  54. $p_clan->{'m_ids'} = $p_clan->{'m_ids'} . ',' . $member->{'id'};
  55. $clan_ret = MemberClan::where('id', $p_clan->{'id'})->update(['m_ids' => $p_clan->{'m_ids'}]);
  56. if (empty($clan_ret)) {
  57. DB::rollBack(); //回滚
  58. } else {
  59. //更新所有父级的信息
  60. if ($p_clan->{'p_ids'}) {
  61. $clan_list = MemberClan::whereIn('m_id', explode(',', $p_clan->{'p_ids'}))->select(['id', 'm_ids'])->get();
  62. foreach ($clan_list as $clan_info) {
  63. MemberClan::where('id', $clan_info->{'id'})->update(['m_ids' => $clan_info->{'m_ids'} . ',' . $member->{'id'}]);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * 更新会员关系
  71. * @param $m_member
  72. * @param $new_p_id
  73. * @return array|false
  74. * @throws \Exception
  75. */
  76. function updateRelevance(Member $m_member, $new_p_id)
  77. {
  78. $p_member = Member::where('id', $new_p_id)->select(['id', 'phone', 'recom_id'])->first();
  79. if (empty($p_member)) return false;
  80. DB::beginTransaction();
  81. //修改会员表的会员关系
  82. Member::where('id', $m_member->{'id'})->update(['recom_id' => $p_member->{'id'}]);
  83. //获取会员层级信息
  84. $m_clans = MemberClan::where('m_id', $m_member->{'id'})->select(['id', 'm_ids', 'p_ids'])->first();
  85. if (!empty($m_clans->{'m_ids'})) $move_ids = array_filter(explode(',', $m_clans->{'m_ids'}));
  86. $move_ids[] = $m_member->{'id'};
  87. //去处旧父级信息
  88. $old_p_ids_str = $m_clans->{'p_ids'};
  89. $old_p_ids = array_filter(explode(',', $m_clans->{'p_ids'}));
  90. foreach ($old_p_ids as $old_p_id) {
  91. $old_m_ids = MemberClan::where('m_id', $old_p_id)->value('m_ids');
  92. $old_m_ids = explode(',', $old_m_ids);
  93. $old_m_ids = array_diff($old_m_ids, $move_ids);
  94. MemberClan::where('m_id', $old_p_id)->update(['m_ids' => implode(',', $old_m_ids)]);
  95. }
  96. //更新新父级关系
  97. $p_clans = MemberClan::where('m_id', $p_member->{'id'})->select(['id', 'm_ids', 'p_ids'])->first();
  98. $new_p_ids_str = $p_clans->{'p_ids'} . ',' . $p_member->{'id'};
  99. $new_p_ids = array_filter(explode(',', $new_p_ids_str));
  100. foreach ($new_p_ids as $new_p_id) {
  101. $new_m_ids = MemberClan::where('m_id', $new_p_id)->value('m_ids');
  102. $new_m_ids = explode(',', $new_m_ids);
  103. $new_m_ids = array_unique(array_merge($new_m_ids, $move_ids));
  104. MemberClan::where('m_id', $new_p_id)->update(['m_ids' => implode(',', $new_m_ids)]);
  105. }
  106. //更新移动族谱的父级关系
  107. foreach ($move_ids as $move_id) {
  108. $move_info = MemberClan::where('m_id', $move_id)->first();
  109. $p_ids = str_replace($old_p_ids_str, $new_p_ids_str, $move_info->{'p_ids'});
  110. $update_move = ['p_ids' => $p_ids];
  111. $p_ids = explode(',', $p_ids);
  112. $p_ids = array_reverse($p_ids);
  113. $update_move['one_m_id'] = $p_ids[0];
  114. $update_move['two_m_id'] = empty($p_ids[1]) ? 0 : $p_ids[1];
  115. $update_move['three_m_id'] = empty($p_ids[2]) ? 0 : $p_ids[2];
  116. $update_move['four_m_id'] = empty($p_ids[3]) ? 0 : $p_ids[3];
  117. $update_move['five_m_id'] = empty($p_ids[4]) ? 0 : $p_ids[4];
  118. MemberClan::where('id', $move_info->{'id'})->update($update_move);
  119. }
  120. DB::commit();
  121. return true;
  122. }
  123. /**
  124. * 双线关系绑定
  125. * @param $mId
  126. * @param $pId
  127. * @return bool
  128. */
  129. function creatBoth($mId,$pId){
  130. $num=MemberBoth::where('m_id',$mId)->count();
  131. if($num>0){
  132. return ;
  133. }
  134. $bothData=[
  135. 'm_id'=>$mId,
  136. 'direct_id'=>$pId,
  137. 'left_id'=>0,
  138. 'right_id'=>0,
  139. ];
  140. $pBothData=MemberBoth::where('m_id',$pId)->first();
  141. if(empty($pBothData)){
  142. CommonServer::creatServer()->addErrorRecord('层级关系错误',$bothData);
  143. }else{
  144. if($pBothData->{'is_end'}!=0){
  145. //直推人已满就检查下面人员
  146. $childIds=array_filter(explode(',',$pBothData->{'child_ids'}));
  147. //重新获取
  148. $pBothData=MemberBoth::whereIn('m_id',$childIds)->where('is_end',0)->orderBy('tier_num' ,'asc')->orderBy('id','asc')->first();
  149. if(empty($pBothData)){
  150. CommonServer::creatServer()->addErrorRecord('子节点层级关系错误',$bothData);
  151. }
  152. }
  153. if($pBothData->{'left_id'}){
  154. $pBothData->{'right_id'}=$mId;
  155. }else{
  156. $pBothData->{'left_id'}=$mId;
  157. }
  158. $pBothData->{'child_ids'}.=(($pBothData->{'child_ids'}?',':'').$mId);
  159. $bothData['tier_num']=$pBothData->{'tier_num'}+1;
  160. $bothData['parent_id']=$pBothData->{'id'};
  161. $bothData['parent_ids']=$pBothData->{'parent_ids'}.','.$pBothData->{'id'};
  162. $bothData['child_ids']='';
  163. $bothData['is_end']='0';
  164. $bothData=MemberBoth::create($bothData);
  165. if($pBothData->{'left_id'} && $pBothData->{'right_id'}){
  166. $pBothData->{'is_end'}=1;
  167. }
  168. $pBothData->save();
  169. $parentIds=array_filter(explode(',',$bothData['parent_ids']));
  170. foreach ($parentIds as $parentId){
  171. if($parentId!=$pBothData->{'id'}){
  172. $childIds=MemberBoth::where('id',$parentId)->value('child_ids');
  173. $childIds=explode(',', $childIds);
  174. $childIds[]=$bothData->{'id'};
  175. MemberBoth::where('id',$parentId)->update(['child_ids'=>implode(',',$childIds)]);
  176. }
  177. }
  178. }
  179. return true;
  180. }
  181. }