UserService.php 22 KB

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