UserService.php 20 KB

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