UserService.php 21 KB

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