UserService.php 20 KB

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