UserService.php 20 KB

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