UserService.php 21 KB

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