123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace App\Servers;
- use App\Jobs\MemberShareJob;
- use App\Models\Member;
- use App\Models\MemberBoth;
- use App\Models\MemberClan;
- use Illuminate\Support\Facades\DB;
- /**
- * 会员
- */
- class MemberClanServer
- {
- /**
- * 错误信息
- * @var string
- */
- private $errorMsg = '';
- static private $server = '';
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return MemberClanServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) {
- self::$server = new MemberClanServer();
- }
- return self::$server;
- }
- /**
- * 创建会员关系
- * @param Member $member
- * @throws \Exception
- */
- function creatRelevance(Member $member)
- {
- $p_clan = MemberClan::where('m_id', $member->{'recom_id'})->first();
- $member_clan = [
- 'm_id' => $member->{'id'},
- 'tier_num' => $p_clan->{'tier_num'} + 1,
- 'one_m_id' => $p_clan->{'m_id'},
- 'two_m_id' => $p_clan->{'one_m_id'},
- 'three_m_id' => $p_clan->{'two_m_id'},
- 'four_m_id' => $p_clan->{'three_m_id'},
- 'five_m_id' => $p_clan->{'four_m_id'},
- 'm_ids' => 0,
- 'p_ids' => $p_clan->{'p_ids'} . ',' . $p_clan->{'m_id'},
- ];
- $clan_ret = MemberClan::create($member_clan);
- if (!empty($clan_ret)) {
- $p_clan->{'m_ids'} = $p_clan->{'m_ids'} . ',' . $member->{'id'};
- $clan_ret = MemberClan::where('id', $p_clan->{'id'})->update(['m_ids' => $p_clan->{'m_ids'}]);
- if (empty($clan_ret)) {
- DB::rollBack(); //回滚
- } else {
- //更新所有父级的信息
- if ($p_clan->{'p_ids'}) {
- $clan_list = MemberClan::whereIn('m_id', explode(',', $p_clan->{'p_ids'}))->select(['id', 'm_ids'])->get();
- foreach ($clan_list as $clan_info) {
- MemberClan::where('id', $clan_info->{'id'})->update(['m_ids' => $clan_info->{'m_ids'} . ',' . $member->{'id'}]);
- }
- }
- }
- }
- }
- /**
- * 更新会员关系
- * @param $m_member
- * @param $new_p_id
- * @return array|false
- * @throws \Exception
- */
- function updateRelevance(Member $m_member, $new_p_id)
- {
- $p_member = Member::where('id', $new_p_id)->select(['id', 'phone', 'recom_id'])->first();
- if (empty($p_member)) return false;
- DB::beginTransaction();
- //修改会员表的会员关系
- Member::where('id', $m_member->{'id'})->update(['recom_id' => $p_member->{'id'}]);
- //获取会员层级信息
- $m_clans = MemberClan::where('m_id', $m_member->{'id'})->select(['id', 'm_ids', 'p_ids'])->first();
- if (!empty($m_clans->{'m_ids'})) $move_ids = array_filter(explode(',', $m_clans->{'m_ids'}));
- $move_ids[] = $m_member->{'id'};
- //去处旧父级信息
- $old_p_ids_str = $m_clans->{'p_ids'};
- $old_p_ids = array_filter(explode(',', $m_clans->{'p_ids'}));
- foreach ($old_p_ids as $old_p_id) {
- $old_m_ids = MemberClan::where('m_id', $old_p_id)->value('m_ids');
- $old_m_ids = explode(',', $old_m_ids);
- $old_m_ids = array_diff($old_m_ids, $move_ids);
- MemberClan::where('m_id', $old_p_id)->update(['m_ids' => implode(',', $old_m_ids)]);
- }
- //更新新父级关系
- $p_clans = MemberClan::where('m_id', $p_member->{'id'})->select(['id', 'm_ids', 'p_ids'])->first();
- $new_p_ids_str = $p_clans->{'p_ids'} . ',' . $p_member->{'id'};
- $new_p_ids = array_filter(explode(',', $new_p_ids_str));
- foreach ($new_p_ids as $new_p_id) {
- $new_m_ids = MemberClan::where('m_id', $new_p_id)->value('m_ids');
- $new_m_ids = explode(',', $new_m_ids);
- $new_m_ids = array_unique(array_merge($new_m_ids, $move_ids));
- MemberClan::where('m_id', $new_p_id)->update(['m_ids' => implode(',', $new_m_ids)]);
- }
- //更新移动族谱的父级关系
- foreach ($move_ids as $move_id) {
- $move_info = MemberClan::where('m_id', $move_id)->first();
- $p_ids = str_replace($old_p_ids_str, $new_p_ids_str, $move_info->{'p_ids'});
- $update_move = ['p_ids' => $p_ids];
- $p_ids = explode(',', $p_ids);
- $p_ids = array_reverse($p_ids);
- $update_move['one_m_id'] = $p_ids[0];
- $update_move['two_m_id'] = empty($p_ids[1]) ? 0 : $p_ids[1];
- $update_move['three_m_id'] = empty($p_ids[2]) ? 0 : $p_ids[2];
- $update_move['four_m_id'] = empty($p_ids[3]) ? 0 : $p_ids[3];
- $update_move['five_m_id'] = empty($p_ids[4]) ? 0 : $p_ids[4];
- MemberClan::where('id', $move_info->{'id'})->update($update_move);
- }
- DB::commit();
- return true;
- }
- /**
- * 双线关系绑定
- * @param $mId
- * @param $pId
- * @return bool
- */
- function creatBoth($mId,$pId){
- $num=MemberBoth::where('m_id',$mId)->count();
- if($num>0){
- return ;
- }
- $bothData=[
- 'm_id'=>$mId,
- 'direct_id'=>$pId,
- 'left_id'=>0,
- 'right_id'=>0,
- ];
- $pBothData=MemberBoth::where('m_id',$pId)->first();
- if(empty($pBothData)){
- CommonServer::creatServer()->addErrorRecord('层级关系错误',$bothData);
- }else{
- if($pBothData->{'is_end'}!=0){
- //直推人已满就检查下面人员
- $childIds=array_filter(explode(',',$pBothData->{'child_ids'}));
- //重新获取
- $pBothData=MemberBoth::whereIn('m_id',$childIds)->where('is_end',0)->orderBy('tier_num' ,'asc')->orderBy('id','asc')->first();
- if(empty($pBothData)){
- CommonServer::creatServer()->addErrorRecord('子节点层级关系错误',$bothData);
- }
- }
- if($pBothData->{'left_id'}){
- $pBothData->{'right_id'}=$mId;
- }else{
- $pBothData->{'left_id'}=$mId;
- }
- $pBothData->{'child_ids'}.=(($pBothData->{'child_ids'}?',':'').$mId);
- $bothData['tier_num']=$pBothData->{'tier_num'}+1;
- $bothData['parent_id']=$pBothData->{'id'};
- $bothData['parent_ids']=$pBothData->{'parent_ids'}.','.$pBothData->{'id'};
- $bothData['child_ids']='';
- $bothData['is_end']='0';
- $bothData=MemberBoth::create($bothData);
- if($pBothData->{'left_id'} && $pBothData->{'right_id'}){
- $pBothData->{'is_end'}=1;
- }
- $pBothData->save();
- $parentIds=array_filter(explode(',',$bothData['parent_ids']));
- foreach ($parentIds as $parentId){
- if($parentId!=$pBothData->{'id'}){
- $childIds=MemberBoth::where('id',$parentId)->value('child_ids');
- $childIds=explode(',', $childIds);
- $childIds[]=$bothData->{'id'};
- MemberBoth::where('id',$parentId)->update(['child_ids'=>implode(',',$childIds)]);
- }
- }
- }
- return true;
- }
- }
|