UserService.php 16 KB

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