123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- namespace App\Servers;
- use App\Models\Album;
- use App\Models\Comment;
- use App\Models\ImGroup;
- use App\Models\ImService;
- use App\Models\Member;
- use App\Models\MemberAddress;
- use App\Models\MemberClan;
- use App\Models\MemberLog;
- use App\Models\MemberOpenId;
- use App\Models\Order;
- use App\Models\OrderDelivery;
- use App\Models\OrderItem;
- use App\Models\RefundOrder;
- use App\Models\Shop;
- use App\Models\ShopBean;
- use App\Models\ShopFans;
- use App\Models\ShopOrder;
- use App\Models\UsedGoods;
- use App\Models\UsedOrder;
- use Illuminate\Support\Facades\DB;
- class MergeServer
- {
- static private $server = '';
- private function __construct()
- {
- }
- /**
- * 创建对象
- *
- */
- static function creatServer()
- {
- if (empty(self::$server)) {
- self::$server = new MergeServer();
- }
- return self::$server;
- }
- /**
- * 合并会员账户
- * @param $m_id
- * @param $old_id
- * @param $price
- * @return bool
- */
- function mergeAccount($m_id,$old_id,$info,$old_info,$member_info,$merge_source){
- DB::beginTransaction();
- try {
- //更新用户个人信息
- Member::where('id', $old_id)->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;
- }
- }
|