UserService.php 21 KB

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