MergeServer.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\Album;
  4. use App\Models\Comment;
  5. use App\Models\ImGroup;
  6. use App\Models\ImService;
  7. use App\Models\Member;
  8. use App\Models\MemberAddress;
  9. use App\Models\MemberClan;
  10. use App\Models\MemberLog;
  11. use App\Models\MemberOpenId;
  12. use App\Models\Order;
  13. use App\Models\OrderDelivery;
  14. use App\Models\OrderItem;
  15. use App\Models\RefundOrder;
  16. use App\Models\Shop;
  17. use App\Models\ShopBean;
  18. use App\Models\ShopFans;
  19. use App\Models\ShopOrder;
  20. use App\Models\UsedGoods;
  21. use App\Models\UsedOrder;
  22. use Illuminate\Support\Facades\DB;
  23. class MergeServer
  24. {
  25. static private $server = '';
  26. private function __construct()
  27. {
  28. }
  29. /**
  30. * 创建对象
  31. *
  32. */
  33. static function creatServer()
  34. {
  35. if (empty(self::$server)) {
  36. self::$server = new MergeServer();
  37. }
  38. return self::$server;
  39. }
  40. /**
  41. * 合并会员账户
  42. * @param $m_id
  43. * @param $old_id
  44. * @param $price
  45. * @return bool
  46. */
  47. function mergeAccount($m_id,$old_id,$info,$old_info,$member_info,$merge_source){
  48. DB::beginTransaction();
  49. try {
  50. //更新用户个人信息
  51. Member::where('id', $old_id)->update(['phone'=>$old_info['phone'],'status'=>0]);
  52. Member::where('id', $m_id)->update([
  53. 'nickname'=>$member_info['nickname'],
  54. 'head_img'=>$member_info['head_img'],
  55. 'sex'=>$member_info['sex'],
  56. 'describe'=>$member_info['describe'],
  57. 'phone'=>$old_info['phone'],
  58. 'pay_pass'=>$member_info['pay_pass'],
  59. 'share_img'=>'',
  60. 'code_img'=>'',
  61. 'merge_source'=>$merge_source
  62. ]);
  63. //将云相册旧id数据更新到新主体
  64. $albums = Album::where('m_id',$old_id)->where('is_del',0)->orderBy('id', 'asc')->pluck('id')->toArray();
  65. Album::where('m_id',$old_id)->where('is_del',0)->update(['m_id'=>$m_id]);
  66. //更新会员评论
  67. $comments = Comment::where('m_id',$old_id)->where('is_del',0)->orderBy('id', 'asc')->pluck('id')->toArray();
  68. Comment::where('m_id',$old_id)->where('is_del',0)->update(['m_id'=>$m_id]);
  69. //更新会员微信公众号openid
  70. $open_ids = MemberOpenId::where('m_id',$old_id)->pluck('id')->toArray();
  71. MemberOpenId::where('m_id',$old_id)->update(['m_id'=>$m_id]);
  72. //更新会员订单快递记录
  73. $order_delivery = OrderDelivery::where('m_id',$old_id)->pluck('id')->toArray();
  74. OrderDelivery::where('m_id',$old_id)->update(['m_id'=>$m_id]);
  75. //更新用户订单商品详情记录
  76. $order_items = OrderItem::where('m_id',$old_id)->pluck('id')->toArray();
  77. OrderItem::where('m_id',$old_id)->update(['m_id'=>$m_id]);
  78. //更新会员订单表
  79. $orders = Order::where('m_id',$old_id)->pluck('id')->toArray();
  80. Order::where('m_id',$old_id)->update(['m_id'=>$m_id]);
  81. //更新用户退款订单
  82. $refund_orders = RefundOrder::where('m_id',$old_id)->pluck('id')->toArray();
  83. RefundOrder::where('m_id',$old_id)->update(['m_id'=>$m_id]);
  84. //更新用户在店铺线下支付订单
  85. $shop_orders = ShopOrder::where('m_id',$old_id)->pluck('id')->toArray();
  86. ShopOrder::where('m_id',$old_id)->update(['m_id'=>$m_id]);
  87. //更新用户闲置商品(目前有问题,待定)
  88. $used_goods = UsedGoods::where('m_id',$old_id)->where('is_del',0)->pluck('id')->toArray();
  89. UsedGoods::where('m_id',$old_id)->where('is_del',0)->update(['m_id'=>$m_id]);
  90. //更新用户购买闲置商品订单
  91. $used_orders = UsedOrder::where('sell_id',$old_id)->where('is_del',0)->orWhere('buy_id',$old_id)->pluck('id')->toArray();
  92. UsedOrder::where('sell_id',$old_id)->where('is_del',0)->update(['sell_id'=>$m_id]);
  93. UsedOrder::where('buy_id',$old_id)->where('is_del',0)->update(['buy_id'=>$m_id]);
  94. //检查是否是客服
  95. $service = ImService::where('m_id',$old_id)->where('is_del',0)->limit(1)->pluck('id')->toArray();
  96. //更新消息组信息
  97. if($service){
  98. $group_ids = $this->updateImGroup(3,$old_id,$m_id);
  99. }else{
  100. $group_ids = $this->updateImGroup(1,$old_id,$m_id);
  101. }
  102. //更新会员地址
  103. $address = $this->updateAddress($m_id,$old_id);
  104. //更新会员资金流水表
  105. $this->updateFunds($m_id,$old_id,$info,$old_info);
  106. //迁移明细
  107. MoneyDetailServer::creatServer()->migrateLog($old_id,$m_id);
  108. //更新店铺粉丝记录
  109. $fans = $this->updateFans($m_id,$old_id);
  110. //合并下级
  111. $relevance = $this->mergeRelevance($m_id,$old_id);
  112. //记录日志
  113. $member = [
  114. 'm_id' => $m_id,
  115. 'old_id' => $old_id,
  116. ];
  117. $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');
  118. // var_dump(json_encode($data));exit();
  119. $ip=CommonServer::creatServer()->getClientIp();
  120. MemberLog::create(
  121. [
  122. 'm_id' => $m_id,
  123. 'url' => request()->url(),
  124. 'msg' => json_encode($data),
  125. 'ip' => $ip,
  126. 'data' => http_build_query(request()->all()),
  127. ]
  128. );
  129. DB::commit();
  130. return true;
  131. } catch (\Exception $e) {
  132. // var_dump($e->getMessage());
  133. DB::rollBack();
  134. return false;
  135. }
  136. }
  137. /**
  138. * 将旧账户地址同步到新主体账户
  139. * @param $m_id
  140. * @param $old_id
  141. * @return mixed
  142. */
  143. function updateAddress($m_id,$old_id){
  144. //获取旧账号的地址
  145. $address = MemberAddress::where('member_id',$old_id)->where('is_del',0)->select(['id','province','city','area','address'])->get();
  146. $ids = [];
  147. foreach ($address as $value){
  148. $find = MemberAddress::where('member_id',$m_id)
  149. ->where('is_del',0)
  150. ->where('province',$value['province'])
  151. ->where('city',$value['city'])
  152. ->where('area',$value['area'])
  153. ->where('address',$value['address'])
  154. ->select(['id'])
  155. ->first();
  156. if(!$find){
  157. $ids[] = $value['id'];
  158. }
  159. }
  160. MemberAddress::whereIn('id',$ids)->update(['member_id'=>$m_id]);
  161. return $ids;
  162. }
  163. /**
  164. * 将旧账户资金同步到新主体账户
  165. * @param $m_id
  166. * @param $old_id
  167. * @param $info
  168. * @param $old_info
  169. * @return bool
  170. */
  171. function updateFunds($m_id,$old_id,$info,$old_info){
  172. $price = $old_info['money'];//旧帐户余额
  173. $new_money = $info['consume_money'];//新主体账户消费金
  174. $old_money = $old_info['consume_money'];//旧帐户消费金
  175. if($price > 0){//有余额
  176. //更新余额-减少旧账户的
  177. MoneyDetailServer::creatServer()->write(1,19,$price,0,$old_id,'账户合并-余额转出');
  178. //更新余额-增加到主体账户
  179. MoneyDetailServer::creatServer()->write(1,19,$price,1,$m_id,'账户合并-余额转入');
  180. }
  181. //更新公益豆-查询旧账户有公益豆的店铺
  182. $bean = ShopBean::where('m_id',$old_id)->select(['shop_id','bean_money'])->get();
  183. foreach ($bean as $value){
  184. //更新公益豆-减少旧账户的
  185. MoneyDetailServer::creatServer()->write(3,19,$value['bean_money'],0,$old_id,'账户合并-公益豆转出',0,$value['shop_id']);
  186. //更新公益豆-增加到主体账户
  187. MoneyDetailServer::creatServer()->write(3,19,$value['bean_money'],1,$m_id,'账户合并-公益豆转入',0,$value['shop_id']);
  188. }
  189. //更新消费金-以消费金最低的为标准
  190. if($new_money <= $old_money){
  191. //更新消费金-减少旧账户的
  192. MoneyDetailServer::creatServer()->write(2,19,$old_money,0,$old_id,'账户合并-消费金变动');
  193. }elseif ($new_money > $old_money){
  194. //更新消费金-减少旧账户的
  195. MoneyDetailServer::creatServer()->write(2,19,$old_money,0,$old_id,'账户合并-消费金变动');
  196. $diff_money = bcsub($new_money,$old_money,4);
  197. //更新消费金-减少旧账户的
  198. MoneyDetailServer::creatServer()->write(2,19,$diff_money,0,$m_id,'账户合并-消费金变动');
  199. }
  200. return true;
  201. }
  202. /**
  203. * 将旧账户店铺粉丝同步到新主体账户
  204. * @param $m_id
  205. * @param $old_id
  206. * @return bool
  207. */
  208. function updateFans($m_id,$old_id){
  209. //检查新主体是否是实体店
  210. $shop_id = Shop::where([['m_id',$m_id]])->value('id');
  211. //获取新主体所属粉丝的店铺
  212. $fans_new = ShopFans::where('m_id','=',$m_id)->pluck('shop_id')->toArray();
  213. if($shop_id){
  214. $fans_new[] = $shop_id;
  215. }
  216. //获取不在新主体所属粉丝的店铺
  217. $fans_old = ShopFans::where('m_id','=',$old_id)->whereNotIn('shop_id',$fans_new)->pluck('id')->toArray();
  218. //存在新主体未所属粉丝的店铺
  219. if(!empty($fans_old)){
  220. ShopFans::whereIn('id',$fans_old)->update(['m_id'=>$m_id]);
  221. }
  222. //是否删除旧id的粉丝
  223. ShopFans::where('m_id',$old_id)->update(['status'=>0]);
  224. return $fans_old;
  225. }
  226. /**
  227. * 操作消息组信息
  228. * @param $type
  229. * @param $old_id
  230. * @param $m_id
  231. * @return array
  232. */
  233. function updateImGroup($type,$old_id,$m_id){
  234. if($type == 1){
  235. $ids = ImGroup::where('m_id',$old_id)->where('is_del',0)->orWhere('handle_id',$old_id)->pluck('id')->toArray();
  236. ImGroup::whereIn('id',$ids)->update(['is_del'=>1]);
  237. }elseif ($type == 3){//是客服
  238. //第一类型的数据
  239. $create_ids = ImGroup::where('m_id',$old_id)->where('is_del',0)->pluck('id')->toArray();
  240. $handle_ids = ImGroup::where('handle_id',$old_id)->where('is_del',0)->where('group_type',1)->pluck('id')->toArray();
  241. $one_ids = array_merge($create_ids,$handle_ids);
  242. ImGroup::whereIn('id',$one_ids)->update(['is_del'=>1]);
  243. //第二类数据
  244. $two_ids = ImGroup::where('handle_id',$old_id)->where('is_del',0)->where('group_type',3)->pluck('id')->toArray();
  245. $time = date('Y-m-d H:i:s',time()-3700);
  246. ImGroup::whereIn('id',$two_ids)->update(['last_time'=>$time]);
  247. //清除客服缓存
  248. RedisDataServer::creatServer()->delData('group_list_'.$type.'_' . $old_id);
  249. ImService::where('m_id',$old_id)->update(['m_id'=>$m_id,'status'=>0]);
  250. $ids = array_merge($one_ids,$two_ids);
  251. }
  252. return $ids;
  253. }
  254. /**
  255. * 将旧账户下级合并到新主体账户
  256. * @param $m_id
  257. * @param $old_id
  258. * @return bool
  259. */
  260. function mergeRelevance($m_id,$old_id){
  261. $p_member = Member::where('id', $m_id)->select(['id', 'phone', 'recom_id'])->first();
  262. if (empty($p_member)) return false;
  263. $check = $this->checkRelevance($m_id,$old_id);
  264. //获取需要合并的所有下级
  265. $m_clans = MemberClan::where('m_id', $old_id)->select(['id', 'one_m_id', 'm_ids', 'p_ids'])->first();
  266. $move = [];
  267. if($check == 2){
  268. $move = $this->moveRelevance($m_id,$m_clans['one_m_id']);
  269. if(!$move){
  270. return false;
  271. }
  272. //获取需要合并的所有下级
  273. $m_clans = MemberClan::where('m_id', $old_id)->select(['id', 'one_m_id', 'm_ids', 'p_ids'])->first();
  274. }
  275. $move_ids = array_filter(explode(',', $m_clans->{'m_ids'}));
  276. $old_ids[] = $old_id;
  277. //去处旧父级信息
  278. $old_p_ids_str = $m_clans->{'p_ids'} . ',' . $old_id;
  279. $old_p_ids = array_filter(explode(',', $m_clans->{'p_ids'}));
  280. foreach ($old_p_ids as $old_p_id) {
  281. //获取上级的下级
  282. $old_m_ids = MemberClan::where('m_id', $old_p_id)->value('m_ids');
  283. $old_m_ids = explode(',', $old_m_ids);
  284. //将下级里面包含的旧账户下级祛除更新
  285. $old_m_ids = array_diff($old_m_ids, $move_ids, $old_ids);
  286. MemberClan::where('m_id', $old_p_id)->update(['m_ids' => implode(',', $old_m_ids)]);
  287. }
  288. //更新新父级关系
  289. $p_clans = MemberClan::where('m_id', $m_id)->select(['id' ,'tier_num' ,'m_ids', 'p_ids'])->first();
  290. $new_p_ids_str = $p_clans->{'p_ids'} . ',' . $p_member->{'id'};
  291. $new_p_ids = array_filter(explode(',', $new_p_ids_str));
  292. foreach ($new_p_ids as $new_p_id) {
  293. //获取新上级的下级
  294. $new_m_ids = MemberClan::where('m_id', $new_p_id)->value('m_ids');
  295. $new_m_ids = explode(',', $new_m_ids);
  296. //将新上级的下级和旧账户的下级合并更新
  297. $new_m_ids = array_unique(array_merge($new_m_ids, $move_ids));
  298. MemberClan::where('m_id', $new_p_id)->update(['m_ids' => implode(',', $new_m_ids)]);
  299. }
  300. $num = 1;
  301. //更新移动旧账户下级的族谱父级关系
  302. foreach ($move_ids as $k=>$move_id) {
  303. if($num == 1){
  304. //修改会员表的会员关系
  305. Member::where('id', $move_id)->update(['recom_id' => $p_member->{'id'}]);
  306. }
  307. $tier_num = $num + $p_clans['tier_num'];
  308. $num += 1;
  309. $move_info = MemberClan::where('m_id', $move_id)->first();
  310. $p_ids = str_replace($old_p_ids_str, $new_p_ids_str, $move_info->{'p_ids'});
  311. $update_move = ['p_ids' => $p_ids];
  312. $p_ids = explode(',', $p_ids);
  313. $p_ids = array_reverse($p_ids);
  314. $update_move['tier_num'] = $tier_num;
  315. $update_move['one_m_id'] = $p_ids[0];
  316. $update_move['two_m_id'] = empty($p_ids[1]) ? 0 : $p_ids[1];
  317. $update_move['three_m_id'] = empty($p_ids[2]) ? 0 : $p_ids[2];
  318. $update_move['four_m_id'] = empty($p_ids[3]) ? 0 : $p_ids[3];
  319. $update_move['five_m_id'] = empty($p_ids[4]) ? 0 : $p_ids[4];
  320. MemberClan::where('id', $move_info->{'id'})->update($update_move);
  321. }
  322. //将旧账户的下级更新为空
  323. MemberClan::where('m_id', $old_id)->update(['m_ids' => 0]);
  324. $data['old_p_ids'] = $old_p_ids;
  325. $data['new_p_ids'] = $new_p_ids;
  326. $data['move_ids'] = $move_ids;
  327. $data['move'] = $move;
  328. return $data;
  329. }
  330. /**
  331. * 主体账户层级在旧账户下
  332. * @param $m_id
  333. * @param $new_p_id
  334. */
  335. function moveRelevance($m_id,$new_p_id){
  336. $p_member = Member::where('id', $new_p_id)->select(['id', 'phone', 'recom_id'])->first();
  337. if (empty($p_member)) return false;
  338. //修改会员表的会员关系
  339. Member::where('id', $m_id)->update(['recom_id' => $p_member->{'id'}]);
  340. //获取需要合并的所有下级
  341. $m_clans = MemberClan::where('m_id', $m_id)->select(['id', 'one_m_id', 'm_ids', 'p_ids'])->first();
  342. $move_ids = $m_id .','. $m_clans->{'m_ids'};
  343. $move_ids = array_filter(explode(',', $move_ids));
  344. //去处旧父级信息
  345. $old_p_ids_str = $m_clans->{'p_ids'};
  346. $old_p_ids = array_filter(explode(',', $m_clans->{'p_ids'}));
  347. foreach ($old_p_ids as $old_p_id) {
  348. //获取上级的下级
  349. $old_m_ids = MemberClan::where('m_id', $old_p_id)->value('m_ids');
  350. $old_m_ids = explode(',', $old_m_ids);
  351. //将下级里面包含的旧账户下级祛除更新
  352. $old_m_ids = array_diff($old_m_ids, $move_ids);
  353. MemberClan::where('m_id', $old_p_id)->update(['m_ids' => implode(',', $old_m_ids)]);
  354. }
  355. //更新新父级关系
  356. $p_clans = MemberClan::where('m_id', $new_p_id)->select(['id' ,'tier_num' ,'m_ids', 'p_ids'])->first();
  357. $new_p_ids_str = $p_clans->{'p_ids'} . ',' . $p_member->{'id'};
  358. $new_p_ids = array_filter(explode(',', $new_p_ids_str));
  359. foreach ($new_p_ids as $new_p_id) {
  360. //获取新上级的下级
  361. $new_m_ids = MemberClan::where('m_id', $new_p_id)->value('m_ids');
  362. $new_m_ids = explode(',', $new_m_ids);
  363. //将新上级的下级和旧账户的下级合并更新
  364. $new_m_ids = array_unique(array_merge($new_m_ids, $move_ids));
  365. MemberClan::where('m_id', $new_p_id)->update(['m_ids' => implode(',', $new_m_ids)]);
  366. }
  367. $num = 1;
  368. //更新移动旧账户下级的族谱父级关系
  369. foreach ($move_ids as $k=>$move_id) {
  370. $tier_num = $num + $p_clans['tier_num'];
  371. $num += 1;
  372. $move_info = MemberClan::where('m_id', $move_id)->first();
  373. $p_ids = str_replace($old_p_ids_str, $new_p_ids_str, $move_info->{'p_ids'});
  374. $update_move = ['p_ids' => $p_ids];
  375. $p_ids = explode(',', $p_ids);
  376. $p_ids = array_reverse($p_ids);
  377. $update_move['tier_num'] = $tier_num;
  378. $update_move['one_m_id'] = $p_ids[0];
  379. $update_move['two_m_id'] = empty($p_ids[1]) ? 0 : $p_ids[1];
  380. $update_move['three_m_id'] = empty($p_ids[2]) ? 0 : $p_ids[2];
  381. $update_move['four_m_id'] = empty($p_ids[3]) ? 0 : $p_ids[3];
  382. $update_move['five_m_id'] = empty($p_ids[4]) ? 0 : $p_ids[4];
  383. $move_info->update($update_move);
  384. }
  385. $data['old_p_ids'] = $old_p_ids;
  386. $data['new_p_ids'] = $new_p_ids;
  387. $data['move_ids'] = $move_ids;
  388. return $data;
  389. }
  390. /**
  391. * 检查各自会员下级中是否存在对方,即检查是否在分销一条线中
  392. * @param $m_id
  393. * @param $other_id
  394. * @return int
  395. */
  396. function checkRelevance($m_id,$other_id){
  397. //获取本次操作会员的下级
  398. $m_ids = MemberClan::where('m_id',$m_id)->value('m_ids');
  399. $m_ids = explode(',',$m_ids);
  400. if(in_array($other_id,$m_ids)){
  401. return 1;
  402. }
  403. //获取其他来源会员的下级
  404. $other_ids = MemberClan::where('m_id',$other_id)->value('m_ids');
  405. $other_ids = explode(',',$other_ids);
  406. if(in_array($m_id,$other_ids)){
  407. return 2;
  408. }
  409. return 0;
  410. }
  411. }