update(['phone'=>$old_info['phone'],'status'=>0]); Member::where('id', $m_id)->update([ 'nickname'=>$member_info['nickname'], 'head_img'=>$member_info['head_img'], 'sex'=>$member_info['sex'], 'describe'=>$member_info['describe'], 'phone'=>$old_info['phone'], 'pay_pass'=>$member_info['pay_pass'], 'share_img'=>'', 'code_img'=>'', 'merge_source'=>$merge_source ]); //将云相册旧id数据更新到新主体 $albums = Album::where('m_id',$old_id)->where('is_del',0)->orderBy('id', 'asc')->pluck('id')->toArray(); Album::where('m_id',$old_id)->where('is_del',0)->update(['m_id'=>$m_id]); //更新会员评论 $comments = Comment::where('m_id',$old_id)->where('is_del',0)->orderBy('id', 'asc')->pluck('id')->toArray(); Comment::where('m_id',$old_id)->where('is_del',0)->update(['m_id'=>$m_id]); //更新会员微信公众号openid $open_ids = MemberOpenId::where('m_id',$old_id)->pluck('id')->toArray(); MemberOpenId::where('m_id',$old_id)->update(['m_id'=>$m_id]); //更新会员订单快递记录 $order_delivery = OrderDelivery::where('m_id',$old_id)->pluck('id')->toArray(); OrderDelivery::where('m_id',$old_id)->update(['m_id'=>$m_id]); //更新用户订单商品详情记录 $order_items = OrderItem::where('m_id',$old_id)->pluck('id')->toArray(); OrderItem::where('m_id',$old_id)->update(['m_id'=>$m_id]); //更新会员订单表 $orders = Order::where('m_id',$old_id)->pluck('id')->toArray(); Order::where('m_id',$old_id)->update(['m_id'=>$m_id]); //更新用户退款订单 $refund_orders = RefundOrder::where('m_id',$old_id)->pluck('id')->toArray(); RefundOrder::where('m_id',$old_id)->update(['m_id'=>$m_id]); //更新用户在店铺线下支付订单 $shop_orders = ShopOrder::where('m_id',$old_id)->pluck('id')->toArray(); ShopOrder::where('m_id',$old_id)->update(['m_id'=>$m_id]); //更新用户闲置商品(目前有问题,待定) $used_goods = UsedGoods::where('m_id',$old_id)->where('is_del',0)->pluck('id')->toArray(); UsedGoods::where('m_id',$old_id)->where('is_del',0)->update(['m_id'=>$m_id]); //更新用户购买闲置商品订单 $used_orders = UsedOrder::where('sell_id',$old_id)->where('is_del',0)->orWhere('buy_id',$old_id)->pluck('id')->toArray(); UsedOrder::where('sell_id',$old_id)->where('is_del',0)->update(['sell_id'=>$m_id]); UsedOrder::where('buy_id',$old_id)->where('is_del',0)->update(['buy_id'=>$m_id]); //检查是否是客服 $service = ImService::where('m_id',$old_id)->where('is_del',0)->limit(1)->pluck('id')->toArray(); //更新消息组信息 if($service){ $group_ids = $this->updateImGroup(3,$old_id,$m_id); }else{ $group_ids = $this->updateImGroup(1,$old_id,$m_id); } //更新会员地址 $address = $this->updateAddress($m_id,$old_id); //更新会员资金流水表 $this->updateFunds($m_id,$old_id,$info,$old_info); //迁移明细 MoneyDetailServer::creatServer()->migrateLog($old_id,$m_id); //更新店铺粉丝记录 $fans = $this->updateFans($m_id,$old_id); //合并下级 $relevance = $this->mergeRelevance($m_id,$old_id); //记录日志 $member = [ 'm_id' => $m_id, 'old_id' => $old_id, ]; $data = compact('member','albums','comments','open_ids','order_delivery','order_items','orders','refund_orders','shop_orders','used_goods','used_orders','address','fans','relevance','service','group_ids'); // var_dump(json_encode($data));exit(); $ip=CommonServer::creatServer()->getClientIp(); MemberLog::create( [ 'm_id' => $m_id, 'url' => request()->url(), 'msg' => json_encode($data), 'ip' => $ip, 'data' => http_build_query(request()->all()), ] ); DB::commit(); return true; } catch (\Exception $e) { // var_dump($e->getMessage()); DB::rollBack(); return false; } } /** * 将旧账户地址同步到新主体账户 * @param $m_id * @param $old_id * @return mixed */ function updateAddress($m_id,$old_id){ //获取旧账号的地址 $address = MemberAddress::where('member_id',$old_id)->where('is_del',0)->select(['id','province','city','area','address'])->get(); $ids = []; foreach ($address as $value){ $find = MemberAddress::where('member_id',$m_id) ->where('is_del',0) ->where('province',$value['province']) ->where('city',$value['city']) ->where('area',$value['area']) ->where('address',$value['address']) ->select(['id']) ->first(); if(!$find){ $ids[] = $value['id']; } } MemberAddress::whereIn('id',$ids)->update(['member_id'=>$m_id]); return $ids; } /** * 将旧账户资金同步到新主体账户 * @param $m_id * @param $old_id * @param $info * @param $old_info * @return bool */ function updateFunds($m_id,$old_id,$info,$old_info){ $price = $old_info['money'];//旧帐户余额 $new_money = $info['consume_money'];//新主体账户消费金 $old_money = $old_info['consume_money'];//旧帐户消费金 if($price > 0){//有余额 //更新余额-减少旧账户的 MoneyDetailServer::creatServer()->write(1,19,$price,0,$old_id,'账户合并-余额转出'); //更新余额-增加到主体账户 MoneyDetailServer::creatServer()->write(1,19,$price,1,$m_id,'账户合并-余额转入'); } //更新公益豆-查询旧账户有公益豆的店铺 $bean = ShopBean::where('m_id',$old_id)->select(['shop_id','bean_money'])->get(); foreach ($bean as $value){ //更新公益豆-减少旧账户的 MoneyDetailServer::creatServer()->write(3,19,$value['bean_money'],0,$old_id,'账户合并-公益豆转出',0,$value['shop_id']); //更新公益豆-增加到主体账户 MoneyDetailServer::creatServer()->write(3,19,$value['bean_money'],1,$m_id,'账户合并-公益豆转入',0,$value['shop_id']); } //更新消费金-以消费金最低的为标准 if($new_money <= $old_money){ //更新消费金-减少旧账户的 MoneyDetailServer::creatServer()->write(2,19,$old_money,0,$old_id,'账户合并-消费金变动'); }elseif ($new_money > $old_money){ //更新消费金-减少旧账户的 MoneyDetailServer::creatServer()->write(2,19,$old_money,0,$old_id,'账户合并-消费金变动'); $diff_money = bcsub($new_money,$old_money,4); //更新消费金-减少旧账户的 MoneyDetailServer::creatServer()->write(2,19,$diff_money,0,$m_id,'账户合并-消费金变动'); } return true; } /** * 将旧账户店铺粉丝同步到新主体账户 * @param $m_id * @param $old_id * @return bool */ function updateFans($m_id,$old_id){ //检查新主体是否是实体店 $shop_id = Shop::where([['m_id',$m_id]])->value('id'); //获取新主体所属粉丝的店铺 $fans_new = ShopFans::where('m_id','=',$m_id)->pluck('shop_id')->toArray(); if($shop_id){ $fans_new[] = $shop_id; } //获取不在新主体所属粉丝的店铺 $fans_old = ShopFans::where('m_id','=',$old_id)->whereNotIn('shop_id',$fans_new)->pluck('id')->toArray(); //存在新主体未所属粉丝的店铺 if(!empty($fans_old)){ ShopFans::whereIn('id',$fans_old)->update(['m_id'=>$m_id]); } //是否删除旧id的粉丝 ShopFans::where('m_id',$old_id)->update(['status'=>0]); return $fans_old; } /** * 操作消息组信息 * @param $type * @param $old_id * @param $m_id * @return array */ function updateImGroup($type,$old_id,$m_id){ if($type == 1){ $ids = ImGroup::where('m_id',$old_id)->where('is_del',0)->orWhere('handle_id',$old_id)->pluck('id')->toArray(); ImGroup::whereIn('id',$ids)->update(['is_del'=>1]); }elseif ($type == 3){//是客服 //第一类型的数据 $create_ids = ImGroup::where('m_id',$old_id)->where('is_del',0)->pluck('id')->toArray(); $handle_ids = ImGroup::where('handle_id',$old_id)->where('is_del',0)->where('group_type',1)->pluck('id')->toArray(); $one_ids = array_merge($create_ids,$handle_ids); ImGroup::whereIn('id',$one_ids)->update(['is_del'=>1]); //第二类数据 $two_ids = ImGroup::where('handle_id',$old_id)->where('is_del',0)->where('group_type',3)->pluck('id')->toArray(); $time = date('Y-m-d H:i:s',time()-3700); ImGroup::whereIn('id',$two_ids)->update(['last_time'=>$time]); //清除客服缓存 RedisDataServer::creatServer()->delData('group_list_'.$type.'_' . $old_id); ImService::where('m_id',$old_id)->update(['m_id'=>$m_id,'status'=>0]); $ids = array_merge($one_ids,$two_ids); } return $ids; } /** * 将旧账户下级合并到新主体账户 * @param $m_id * @param $old_id * @return bool */ function mergeRelevance($m_id,$old_id){ $p_member = Member::where('id', $m_id)->select(['id', 'phone', 'recom_id'])->first(); if (empty($p_member)) return false; $check = $this->checkRelevance($m_id,$old_id); //获取需要合并的所有下级 $m_clans = MemberClan::where('m_id', $old_id)->select(['id', 'one_m_id', 'm_ids', 'p_ids'])->first(); $move = []; if($check == 2){ $move = $this->moveRelevance($m_id,$m_clans['one_m_id']); if(!$move){ return false; } //获取需要合并的所有下级 $m_clans = MemberClan::where('m_id', $old_id)->select(['id', 'one_m_id', 'm_ids', 'p_ids'])->first(); } $move_ids = array_filter(explode(',', $m_clans->{'m_ids'})); $old_ids[] = $old_id; //去处旧父级信息 $old_p_ids_str = $m_clans->{'p_ids'} . ',' . $old_id; $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, $old_ids); MemberClan::where('m_id', $old_p_id)->update(['m_ids' => implode(',', $old_m_ids)]); } //更新新父级关系 $p_clans = MemberClan::where('m_id', $m_id)->select(['id' ,'tier_num' ,'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)]); } $num = 1; //更新移动旧账户下级的族谱父级关系 foreach ($move_ids as $k=>$move_id) { if($num == 1){ //修改会员表的会员关系 Member::where('id', $move_id)->update(['recom_id' => $p_member->{'id'}]); } $tier_num = $num + $p_clans['tier_num']; $num += 1; $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['tier_num'] = $tier_num; $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); } //将旧账户的下级更新为空 MemberClan::where('m_id', $old_id)->update(['m_ids' => 0]); $data['old_p_ids'] = $old_p_ids; $data['new_p_ids'] = $new_p_ids; $data['move_ids'] = $move_ids; $data['move'] = $move; return $data; } /** * 主体账户层级在旧账户下 * @param $m_id * @param $new_p_id */ function moveRelevance($m_id,$new_p_id){ $p_member = Member::where('id', $new_p_id)->select(['id', 'phone', 'recom_id'])->first(); if (empty($p_member)) return false; //修改会员表的会员关系 Member::where('id', $m_id)->update(['recom_id' => $p_member->{'id'}]); //获取需要合并的所有下级 $m_clans = MemberClan::where('m_id', $m_id)->select(['id', 'one_m_id', 'm_ids', 'p_ids'])->first(); $move_ids = $m_id .','. $m_clans->{'m_ids'}; $move_ids = array_filter(explode(',', $move_ids)); //去处旧父级信息 $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', $new_p_id)->select(['id' ,'tier_num' ,'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)]); } $num = 1; //更新移动旧账户下级的族谱父级关系 foreach ($move_ids as $k=>$move_id) { $tier_num = $num + $p_clans['tier_num']; $num += 1; $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['tier_num'] = $tier_num; $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]; $move_info->update($update_move); } $data['old_p_ids'] = $old_p_ids; $data['new_p_ids'] = $new_p_ids; $data['move_ids'] = $move_ids; return $data; } /** * 检查各自会员下级中是否存在对方,即检查是否在分销一条线中 * @param $m_id * @param $other_id * @return int */ function checkRelevance($m_id,$other_id){ //获取本次操作会员的下级 $m_ids = MemberClan::where('m_id',$m_id)->value('m_ids'); $m_ids = explode(',',$m_ids); if(in_array($other_id,$m_ids)){ return 1; } //获取其他来源会员的下级 $other_ids = MemberClan::where('m_id',$other_id)->value('m_ids'); $other_ids = explode(',',$other_ids); if(in_array($m_id,$other_ids)){ return 2; } return 0; } }