UserService.php 22 KB

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