UserService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/21
  6. * Time: 10:42
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\Book\Models\BookGiftsSend;
  10. use App\Modules\Book\Models\UserShelfBooks;
  11. use App\Modules\Book\Services\UserShelfBooksService;
  12. use App\Modules\Subscribe\Models\BookOrder;
  13. use App\Modules\Subscribe\Models\ChapterOrder;
  14. use App\Modules\Subscribe\Services\BookOrderService;
  15. use App\Modules\Subscribe\Services\ChapterOrderService;
  16. use App\Modules\User\Models\User;
  17. use App\Modules\User\Models\UserEnv;
  18. use App\Modules\User\Models\UserSign;
  19. use DB;
  20. use App\Jobs\ActionTrigger;
  21. use App\Modules\OfficialAccount\Models\DistributionSelfDefineConfig;
  22. use App\Modules\Channel\Models\Channel;
  23. use App\Modules\Trade\Models\Order;
  24. use Redis;
  25. class UserService
  26. {
  27. /**
  28. * 创建用户
  29. * $params
  30. * 必穿参数 openid、unionid、distribution_channel_id
  31. * 可传 province、city、country、headimgurl、send_order_id、sex、register_ip
  32. */
  33. static function addUser($params)
  34. {
  35. return User::addUser($params);
  36. }
  37. static function getUserList($limit, $offset)
  38. {
  39. return User::getUserList($limit, $offset);
  40. }
  41. static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
  42. {
  43. return User::getUserByNickAndChannelId($distribution_channel_id, $nickname);
  44. }
  45. /**
  46. * 获取用户信息列表
  47. * @param $pageSize 每页显示的条数
  48. */
  49. static function getPaginationList($pageSize = 20)
  50. {
  51. return User::getPaginationList($pageSize);
  52. }
  53. /**
  54. * 获取用户信息列表
  55. * @param $pageSize 每页显示的条数
  56. */
  57. static function getUsers($pageSize = 20, $careStatus)
  58. {
  59. return User::getPaginationList($pageSize, $careStatus);
  60. }
  61. /**
  62. * 根据id获取用户信息
  63. * @return user object
  64. */
  65. static function getById($id)
  66. {
  67. return User::getById($id);
  68. }
  69. /**
  70. * 根据id获取用户信息
  71. * @return user object
  72. */
  73. static function getUserDataById($id)
  74. {
  75. return User::getUserDataById($id);
  76. }
  77. /**
  78. * 根据id获取用户信息
  79. * @return user object
  80. */
  81. static function getUserDataDetailById($id)
  82. {
  83. return User::getUserDataDetailById($id);
  84. }
  85. /**
  86. * 根据id获取用户名称
  87. * @return string [用户昵称]
  88. */
  89. static function getNicknameById($id)
  90. {
  91. return User::getById($id)->nickname;
  92. }
  93. /**
  94. * 更新用户信息
  95. * @param int $id 用户ID
  96. * @param array $params 可传 province、city、country、headimgurl、sex
  97. * @return boolen
  98. */
  99. static function updateInfo($id, $params)
  100. {
  101. \Log::info('~~~~~~~~~~~~~~~~~====update User====~~~~~~~~~~~~~~~~' . "\n");
  102. \Log::info($id);
  103. \Log::info($params);
  104. return User::updateInfo($id, $params);
  105. }
  106. /**
  107. * 根据union和分销id获取用户信息
  108. * @param $union
  109. * @param $channel_id
  110. * @return mixed
  111. */
  112. static function getUserByUnionAndChannelId($openid, $channel_id)
  113. {
  114. return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->select('id', 'openid', 'unionid','balance','charge_balance','reward_balance')->first();
  115. }
  116. static function getUserByOpenidAndChannelId($openid, $channel_id)
  117. {
  118. return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->first();
  119. }
  120. /**
  121. * 用户余额充值
  122. * @param $uid
  123. * @param $fee
  124. * @return mixed
  125. */
  126. static function addBalance($uid, $fee, $charge, $given)
  127. {
  128. return User::addBalance($uid, $fee, $charge, $given);
  129. }
  130. /**
  131. * 查询推广注册用户总数
  132. * @param $send_order_id
  133. * @return mixed
  134. */
  135. static function getPromotionTotal($send_order_id)
  136. {
  137. return User::getPromotionTotal($send_order_id);
  138. }
  139. /**
  140. * 查询渠道某天注册用户总数
  141. * @param $channel_id
  142. * @param $date
  143. * @return mixed
  144. */
  145. static function getChannelDayTotal($channel_id, $date)
  146. {
  147. return User::getChannelDayTotal($channel_id, $date);
  148. }
  149. /**
  150. * 查询渠道注册用户总数
  151. * @param $channel_id
  152. * @return mixed
  153. */
  154. static function getChannelTotal($channel_id)
  155. {
  156. return User::getChannelTotal($channel_id);
  157. }
  158. /**
  159. * 查询渠道某段时间注册用户总数
  160. * @param $channel_id
  161. * @param $startDate
  162. * @param $endDate
  163. * @return mixed
  164. */
  165. static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
  166. {
  167. return User::getChannelDayToDayTotal($channel_id, $startDate, $endDate);
  168. }
  169. /**
  170. * 查询渠道某段时间注册用户总数
  171. * @param array $channelIds
  172. * @param $startDate
  173. * @param $endDate
  174. * @return mixed
  175. */
  176. static function getChannelsDayToDayTotal($channelIds, $startDate = '', $endDate = '')
  177. {
  178. return User::getChannelsDayToDayTotal($channelIds, $startDate, $endDate);
  179. }
  180. /**
  181. * 查询注册用户总数
  182. * @param $params
  183. */
  184. static function getTotalCount($params)
  185. {
  186. return User::getTotalCount($params);
  187. }
  188. /**
  189. * 查询渠道是否有登录
  190. * @param $channelId
  191. * @param $start_time
  192. * @param $end_time
  193. * @return int
  194. */
  195. static function judgeUserYesterdayLogin($channelId, $start_time, $end_time)
  196. {
  197. $user_behavior = DB::table('channel_operate_record')
  198. ->where([
  199. ['distribution_channel_id', '=', $channelId],
  200. ['created_at', '>=', $start_time],
  201. ['created_at', '<=', $end_time]
  202. ])
  203. ->first();
  204. if ($user_behavior) return 1;
  205. return 0;
  206. }
  207. /**
  208. * @param $date
  209. * @param $company_id
  210. * @return mixed
  211. */
  212. static function getCompanyDayTotal($date, $company_id)
  213. {
  214. $end_date = date('Y-m-d', strtotime($date) + 86400);
  215. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  216. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  217. ->where([
  218. ['users.created_at', '>=', $date],
  219. ['users.created_at', '<', $end_date],
  220. ['channel_users.company_id', '=', $company_id]
  221. ])
  222. ->groupBy('channel_users.company_id')
  223. ->select(DB::raw("count(users.id) as register_sum"))
  224. ->first();
  225. if ($info) return $info->register_sum;
  226. //\Log::info('getCompanyDayTotal error:' . $company_id);
  227. return $info;
  228. }
  229. static function getCompanyTotal($company_id)
  230. {
  231. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  232. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  233. ->where('channel_users.company_id', $company_id)
  234. ->groupBy('channel_users.company_id')
  235. ->select(DB::raw("count(users.id) as register_sum"))
  236. ->first();
  237. if ($info) return $info->register_sum;
  238. //\Log::error('getCompanyTotal error:' . $company_id);
  239. return $info;
  240. }
  241. static function transfromBalanceByUser($from_uid, $to_uid)
  242. {
  243. $add_balance_sql = "update users,(select * from users where id =" . $from_uid . ") as u2
  244. set users.balance = users.balance + u2.balance,
  245. users.reward_balance = users.reward_balance +u2.reward_balance,
  246. users.charge_balance = users.charge_balance +u2.charge_balance where users.id =" . $to_uid;
  247. $res = DB::update($add_balance_sql);
  248. return User::updateInfo($from_uid, ['balance' => 0, 'reward_balance' => 0, 'charge_balance' => 0]);
  249. }
  250. // 扔用户一些动作到队列,异步处理一些额外逻辑
  251. static function PushUserActionToQueue($action_type,$distribution_channel_id,$param){
  252. try{
  253. // \Log::info('PushUserActionToQueue_param_start:'.$distribution_channel_id.' action_type:'.$action_type. ' param:'.json_encode($param));
  254. // 判断站点是否需要额外逻辑
  255. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id,'action_call_ycsd');
  256. if(empty($distribution_self_define_config)){
  257. // \Log::info('not_action_call_ycsd:'.$distribution_channel_id);
  258. return '';
  259. }else{
  260. \Log::info('PushUserActionToQueue_param:'.$distribution_channel_id.' action_type:'.$action_type. ' param:'.json_encode($param));
  261. // 如果支付通道已经切换回去,则不推送了
  262. // $pay_merchant_id = env('YCSD_CALL_PAY_MERCHANT_ID');
  263. // 当前默认
  264. $pay_merchant_id = Redis::get('YCSD_CALL_PAY_MERCHANT_ID');
  265. $channel = Channel::getById($distribution_channel_id);
  266. $current_pay_merchant_id = isset($channel->pay_merchant_id)?$channel->pay_merchant_id:'';
  267. \Log::info('action_call_ycsd:'.$distribution_channel_id.' $pay_merchant_id:'.$pay_merchant_id.' $current_pay_merchant_id:'.$current_pay_merchant_id);
  268. if($current_pay_merchant_id != $pay_merchant_id){
  269. // 判断订单是否是默认支付,放行,切换期间的bug
  270. if($action_type == 'CallBackOrder'){
  271. $order = Order::getByTradeNo($distribution_channel_id, $param['order_sn']);
  272. if(isset($order->pay_merchant_id) && $order->pay_merchant_id == $pay_merchant_id){
  273. \Log::info('action_call_ycsd_old_order_continue:'.$param['order_sn']);
  274. }else{
  275. \Log::info('action_call_ycsd_has_change_pay_return:'.$distribution_channel_id);
  276. return '';
  277. }
  278. }else{
  279. \Log::info('action_call_ycsd_has_change_pay_return:'.$distribution_channel_id);
  280. return '';
  281. }
  282. }
  283. \Log::info('action_call_ycsd:'.$distribution_channel_id);
  284. }
  285. $data = [];
  286. $data['send_time'] = date("Y-m-d H:i:s");
  287. $data['action_type'] = $action_type;
  288. if($action_type == 'Register'){
  289. $data['openid'] = $param['openid'];
  290. $data['uid'] = $param['uid'];
  291. }elseif($action_type == 'CreateOrder'){
  292. $data['orderSn'] = $param['order_sn'];
  293. $data['amount'] = $param['amount'];
  294. $data['openid'] = $param['openid'];
  295. $data['uid'] = $param['uid'];
  296. }elseif($action_type == 'CallBackOrder'){
  297. $data['orderSn'] = $param['order_sn'];
  298. $data['openid'] = $param['openid'];
  299. }
  300. $send_data=array(
  301. 'send_time'=>date("Y-m-d H:i:s"),
  302. 'data' => $data
  303. );
  304. \Log::info('PushUserActionToQueue_openid:'.$data['openid'].' action_type:'.$action_type);
  305. \Log::info('$send_data');\Log::info($send_data);
  306. $delay = 0;
  307. $job = (new ActionTrigger($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('action_trigger_list');
  308. dispatch($job);
  309. }catch(\Exception $e){
  310. \Log::info('ept:'.$e->getMessage());
  311. }
  312. }
  313. public static function isCpcUser($uid){
  314. //openid下所有强关超过三天的uid都无充值,对强关后的uid进行曝光
  315. //openid下有一个强关超过三天的uid,都无充值,对强关后的uid进行曝光
  316. $sql_format = "SELECT u.id,f.subscribe_time,(SELECT id from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as oid FROM users u
  317. JOIN force_subscribe_users f on u.id = f.uid and f.is_subscribed= 1
  318. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  319. $result = DB::select(sprintf($sql_format,$uid));
  320. if(!$result) return false;
  321. $is_has_force_subscribe_three_days =false;
  322. foreach ($result as $item){
  323. //有充值
  324. if($item->oid) return false;
  325. //强关没超过超过三天
  326. //if(time()-strtotime($item->subscribe_time) < 86400*3) return false;
  327. if(time()-strtotime($item->subscribe_time) > 86400*3) $is_has_force_subscribe_three_days = true;
  328. }
  329. return $is_has_force_subscribe_three_days;
  330. }
  331. public static function recordUA($ua,$uid)
  332. {
  333. return UserEnv::recordUA($ua,$uid);
  334. }
  335. public static function transfer(int $from,int $to,int $distribution_channel_id){
  336. $transfer_record = DB::table('crm_transfer')->where('from_uid',$from)->first();
  337. if($transfer_record){
  338. return ;
  339. }
  340. $from_user = self::getById($from);
  341. $to_user = self::getById($to);
  342. if(!$from_user) return ;
  343. if(!$to_user) return ;
  344. //阅读记录迁移***************************************
  345. $record = Redis::hgetall('book_read:'.$from);
  346. $not_uid_key = ['last_read','sign_counts','sign_info','sign_day'];
  347. $data = [];
  348. foreach ($record as $k=>$item){
  349. if(is_numeric($k) || in_array($k,$not_uid_key)){
  350. $data[$k] = $item;
  351. }
  352. }
  353. //\Log::info('read record is');
  354. //\Log::info($data);
  355. if($data) Redis::hmset('book_read:'.$to,$data);
  356. //签到记录
  357. /*$user_sign_model = new UserSign();
  358. $user_sign_model->setCurrentTable(date('Ym'));
  359. $sign_record = $user_sign_model->where('uid',$from)->select('price','sign_time','day','created_at')->orderBy('sign_time','desc')->get();
  360. $temp = [];
  361. if($sign_record){
  362. foreach ($sign_record as $item){
  363. $temp[] = [
  364. 'uid'=>$to,
  365. 'price'=>$item->price,
  366. 'day'=>$item->day,
  367. 'sign_time'=>$item->sign_time,
  368. 'created_at'=>$item->created_at,
  369. 'updated_at'=>date('Y-m-d H:i:s'),
  370. ];
  371. }
  372. //\Log::info('user_sign is');
  373. //\Log::info($temp);
  374. DB::table('user_sign'.date('Ym'))->insert($temp);
  375. }*/
  376. //订阅记录(按本)
  377. $book_order = BookOrder::where('uid',$from)->where('bid','>',0)->get();
  378. if($book_order){
  379. $temp = [];
  380. foreach ($book_order as $book){
  381. $temp[] = [
  382. 'distribution_channel_id'=>$distribution_channel_id,
  383. 'bid'=>$book->bid,
  384. 'book_name'=>$book->book_name,
  385. 'uid'=>$to,
  386. 'u'=>$book->u,
  387. 'fee'=>0,
  388. 'created_at'=>$book->created_at,
  389. 'updated_at'=>date('Y-m-d H:i:s'),
  390. ];
  391. }
  392. //\Log::info('book_order is');
  393. //\Log::info($temp);
  394. DB::table('book_orders')->insert($temp);
  395. }
  396. //订阅记录(按章)
  397. $chapter_order_record = ChapterOrderService::getRecordByUid($from,'','',true);
  398. if($chapter_order_record){
  399. $temp = [];
  400. $i = 1;
  401. $chapter_model = new ChapterOrder();
  402. $chapter_model->setCurrentTable($to);
  403. foreach ($chapter_order_record as $chapter_order){
  404. $temp[] = [
  405. 'distribution_channel_id'=>$distribution_channel_id,
  406. 'bid'=>$chapter_order->bid,
  407. 'cid'=>$chapter_order->cid,
  408. 'chapter_name'=>$chapter_order->chapter_name,
  409. 'book_name'=>$chapter_order->book_name,
  410. 'uid'=>$to,
  411. 'send_order_id'=>0,
  412. 'fee'=>0,
  413. 'created_at'=>$chapter_order->created_at,
  414. 'updated_at'=>date('Y-m-d H:i:s')
  415. ];
  416. if($i % 100 == 0){
  417. $chapter_model->insert($temp);
  418. $temp = [];
  419. }
  420. $i ++;
  421. }
  422. //\Log::info('chapter order is');
  423. //\Log::info($temp);
  424. $chapter_model->insert($temp);
  425. }
  426. //打赏记录
  427. $gift_result = BookGiftsSend::where('uid',$from)->get();
  428. if($gift_result){
  429. $tmp = [];
  430. foreach ($gift_result as $g){
  431. $tmp[] = [
  432. 'uid'=>$to,
  433. 'gift_id'=>$g->gift_id,
  434. 'bid'=>$g->bid,
  435. 'icon'=>$g->icon,
  436. 'name_desc'=>$g->name_desc,
  437. 'cost'=>0,
  438. 'cost_reward'=>$g->cost_reward,
  439. 'cost_recharge'=>$g->cost_recharge,
  440. 'created_at'=>$g->created_at,
  441. 'updated_at'=>date('Y-m-d H:i:s')
  442. ];
  443. }
  444. //\Log::info('book gift is');
  445. //\Log::info($temp);
  446. DB::table('book_gifts_send')->insert($tmp);
  447. }
  448. //书架
  449. $result = UserShelfBooks::where('uid',$from)->get();
  450. if($result){
  451. $tmp = [];
  452. foreach ($result as $s){
  453. $tmp[] = [
  454. 'uid'=>$to,
  455. 'distribution_channel_id'=>$distribution_channel_id,
  456. 'bid'=>$s->bid,
  457. 'created_at'=>$s->created_at,
  458. 'updated_at'=>date('Y-m-d H:i:s')
  459. ];
  460. }
  461. //\Log::info('user book shelf is');
  462. //\Log::info($temp);
  463. DB::table('user_shelf_books')->insert($tmp);
  464. }
  465. //包年
  466. $result = DB::table('year_orders')->where('uid',$from)->where('end_time','>=',date('Y-m-d H:i:s'))->first();
  467. if($result){
  468. DB::table('year_orders')->insert([
  469. 'uid'=>$to,
  470. 'begin_time'=>$result->begin_time,
  471. 'end_time'=>$result->end_time,
  472. 'distribution_channel_id'=>$distribution_channel_id,
  473. 'send_order_id'=>$result->send_order_id,
  474. 'fee'=>$result->fee,
  475. 'created_at'=>date('Y-m-d H:i:s'),
  476. 'updated_at'=>$result->updated_at,
  477. ]);
  478. //\Log::info('year_orders is');
  479. //print_r($result);
  480. }
  481. //书币
  482. $to_user->balance += $from_user->balance;
  483. $to_user->reward_balance += $from_user->balance;
  484. $to_user->nickname = $from_user->nickname;
  485. $to_user->sex = $from_user->sex;
  486. $to_user->head_img = $from_user->head_img;
  487. $to_user->country = $from_user->country;
  488. $to_user->city = $from_user->city;
  489. $to_user->province = $from_user->province;
  490. $to_user->save();
  491. DB::table('crm_transfer')->insert([
  492. 'from_uid'=>$from,
  493. 'to_uid'=>$to,
  494. 'is_enable'=>1,
  495. 'created_at'=>date('Y-m-d H:i:s'),
  496. 'updated_at'=>date('Y-m-d H:i:s')
  497. ]);
  498. }
  499. }