UserService.php 20 KB

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