UserService.php 20 KB

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