UserService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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\User\Models\User;
  10. use App\Modules\User\Models\UserEnv;
  11. use DB;
  12. use App\Jobs\ActionTrigger;
  13. use App\Modules\OfficialAccount\Models\DistributionSelfDefineConfig;
  14. use App\Modules\Channel\Models\Channel;
  15. use App\Modules\Trade\Models\Order;
  16. use Redis;
  17. class UserService
  18. {
  19. /**
  20. * 创建用户
  21. * $params
  22. * 必穿参数 openid、unionid、distribution_channel_id
  23. * 可传 province、city、country、headimgurl、send_order_id、sex、register_ip
  24. */
  25. static function addUser($params)
  26. {
  27. return User::addUser($params);
  28. }
  29. static function getUserList($limit, $offset)
  30. {
  31. return User::getUserList($limit, $offset);
  32. }
  33. static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
  34. {
  35. return User::getUserByNickAndChannelId($distribution_channel_id, $nickname);
  36. }
  37. /**
  38. * 获取用户信息列表
  39. * @param $pageSize 每页显示的条数
  40. */
  41. static function getPaginationList($pageSize = 20)
  42. {
  43. return User::getPaginationList($pageSize);
  44. }
  45. /**
  46. * 获取用户信息列表
  47. * @param $pageSize 每页显示的条数
  48. */
  49. static function getUsers($pageSize = 20, $careStatus)
  50. {
  51. return User::getPaginationList($pageSize, $careStatus);
  52. }
  53. /**
  54. * 根据id获取用户信息
  55. * @return user object
  56. */
  57. static function getById($id)
  58. {
  59. return User::getById($id);
  60. }
  61. /**
  62. * 根据id获取用户信息
  63. * @return user object
  64. */
  65. static function getUserDataById($id)
  66. {
  67. return User::getUserDataById($id);
  68. }
  69. /**
  70. * 根据id获取用户信息
  71. * @return user object
  72. */
  73. static function getUserDataDetailById($id)
  74. {
  75. return User::getUserDataDetailById($id);
  76. }
  77. /**
  78. * 根据id获取用户名称
  79. * @return string [用户昵称]
  80. */
  81. static function getNicknameById($id)
  82. {
  83. return User::getById($id)->nickname;
  84. }
  85. /**
  86. * 更新用户信息
  87. * @param int $id 用户ID
  88. * @param array $params 可传 province、city、country、headimgurl、sex
  89. * @return boolen
  90. */
  91. static function updateInfo($id, $params)
  92. {
  93. \Log::info('~~~~~~~~~~~~~~~~~====update User====~~~~~~~~~~~~~~~~' . "\n");
  94. \Log::info($id);
  95. \Log::info($params);
  96. return User::updateInfo($id, $params);
  97. }
  98. /**
  99. * 根据union和分销id获取用户信息
  100. * @param $union
  101. * @param $channel_id
  102. * @return mixed
  103. */
  104. static function getUserByUnionAndChannelId($openid, $channel_id)
  105. {
  106. return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->select('id', 'openid', 'unionid')->first();
  107. }
  108. /**
  109. * 用户余额充值
  110. * @param $uid
  111. * @param $fee
  112. * @return mixed
  113. */
  114. static function addBalance($uid, $fee, $charge, $given)
  115. {
  116. return User::addBalance($uid, $fee, $charge, $given);
  117. }
  118. /**
  119. * 查询推广注册用户总数
  120. * @param $send_order_id
  121. * @return mixed
  122. */
  123. static function getPromotionTotal($send_order_id)
  124. {
  125. return User::getPromotionTotal($send_order_id);
  126. }
  127. /**
  128. * 查询渠道某天注册用户总数
  129. * @param $channel_id
  130. * @param $date
  131. * @return mixed
  132. */
  133. static function getChannelDayTotal($channel_id, $date)
  134. {
  135. return User::getChannelDayTotal($channel_id, $date);
  136. }
  137. /**
  138. * 查询渠道注册用户总数
  139. * @param $channel_id
  140. * @return mixed
  141. */
  142. static function getChannelTotal($channel_id)
  143. {
  144. return User::getChannelTotal($channel_id);
  145. }
  146. /**
  147. * 查询渠道某段时间注册用户总数
  148. * @param $channel_id
  149. * @param $startDate
  150. * @param $endDate
  151. * @return mixed
  152. */
  153. static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
  154. {
  155. return User::getChannelDayToDayTotal($channel_id, $startDate, $endDate);
  156. }
  157. /**
  158. * 查询渠道某段时间注册用户总数
  159. * @param array $channelIds
  160. * @param $startDate
  161. * @param $endDate
  162. * @return mixed
  163. */
  164. static function getChannelsDayToDayTotal($channelIds, $startDate = '', $endDate = '')
  165. {
  166. return User::getChannelsDayToDayTotal($channelIds, $startDate, $endDate);
  167. }
  168. /**
  169. * 查询注册用户总数
  170. * @param $params
  171. */
  172. static function getTotalCount($params)
  173. {
  174. return User::getTotalCount($params);
  175. }
  176. /**
  177. * 查询渠道是否有登录
  178. * @param $channelId
  179. * @param $start_time
  180. * @param $end_time
  181. * @return int
  182. */
  183. static function judgeUserYesterdayLogin($channelId, $start_time, $end_time)
  184. {
  185. $user_behavior = DB::table('channel_operate_record')
  186. ->where([
  187. ['distribution_channel_id', '=', $channelId],
  188. ['created_at', '>=', $start_time],
  189. ['created_at', '<=', $end_time]
  190. ])
  191. ->first();
  192. if ($user_behavior) return 1;
  193. return 0;
  194. }
  195. /**
  196. * @param $date
  197. * @param $company_id
  198. * @return mixed
  199. */
  200. static function getCompanyDayTotal($date, $company_id)
  201. {
  202. $end_date = date('Y-m-d', strtotime($date) + 86400);
  203. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  204. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  205. ->where([
  206. ['users.created_at', '>=', $date],
  207. ['users.created_at', '<', $end_date],
  208. ['channel_users.company_id', '=', $company_id]
  209. ])
  210. ->groupBy('channel_users.company_id')
  211. ->select(DB::raw("count(users.id) as register_sum"))
  212. ->first();
  213. if ($info) return $info->register_sum;
  214. //\Log::info('getCompanyDayTotal error:' . $company_id);
  215. return $info;
  216. }
  217. static function getCompanyTotal($company_id)
  218. {
  219. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  220. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  221. ->where('channel_users.company_id', $company_id)
  222. ->groupBy('channel_users.company_id')
  223. ->select(DB::raw("count(users.id) as register_sum"))
  224. ->first();
  225. if ($info) return $info->register_sum;
  226. //\Log::error('getCompanyTotal error:' . $company_id);
  227. return $info;
  228. }
  229. static function transfromBalanceByUser($from_uid, $to_uid)
  230. {
  231. $add_balance_sql = "update users,(select * from users where id =" . $from_uid . ") as u2
  232. set users.balance = users.balance + u2.balance,
  233. users.reward_balance = users.reward_balance +u2.reward_balance,
  234. users.charge_balance = users.charge_balance +u2.charge_balance where users.id =" . $to_uid;
  235. $res = DB::update($add_balance_sql);
  236. return User::updateInfo($from_uid, ['balance' => 0, 'reward_balance' => 0, 'charge_balance' => 0]);
  237. }
  238. // 扔用户一些动作到队列,异步处理一些额外逻辑
  239. static function PushUserActionToQueue($action_type,$distribution_channel_id,$param){
  240. try{
  241. // \Log::info('PushUserActionToQueue_param_start:'.$distribution_channel_id.' action_type:'.$action_type. ' param:'.json_encode($param));
  242. // 判断站点是否需要额外逻辑
  243. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id,'action_call_ycsd');
  244. if(empty($distribution_self_define_config)){
  245. // \Log::info('not_action_call_ycsd:'.$distribution_channel_id);
  246. return '';
  247. }else{
  248. \Log::info('PushUserActionToQueue_param:'.$distribution_channel_id.' action_type:'.$action_type. ' param:'.json_encode($param));
  249. // 如果支付通道已经切换回去,则不推送了
  250. // $pay_merchant_id = env('YCSD_CALL_PAY_MERCHANT_ID');
  251. // 当前默认
  252. $pay_merchant_id = Redis::get('YCSD_CALL_PAY_MERCHANT_ID');
  253. $channel = Channel::getById($distribution_channel_id);
  254. $current_pay_merchant_id = isset($channel->pay_merchant_id)?$channel->pay_merchant_id:'';
  255. \Log::info('action_call_ycsd:'.$distribution_channel_id.' $pay_merchant_id:'.$pay_merchant_id.' $current_pay_merchant_id:'.$current_pay_merchant_id);
  256. if($current_pay_merchant_id != $pay_merchant_id){
  257. // 判断订单是否是默认支付,放行,切换期间的bug
  258. if($action_type == 'CallBackOrder'){
  259. $order = Order::getByTradeNo($distribution_channel_id, $param['order_sn']);
  260. if(isset($order->pay_merchant_id) && $order->pay_merchant_id == $pay_merchant_id){
  261. \Log::info('action_call_ycsd_old_order_continue:'.$param['order_sn']);
  262. }else{
  263. \Log::info('action_call_ycsd_has_change_pay_return:'.$distribution_channel_id);
  264. return '';
  265. }
  266. }else{
  267. \Log::info('action_call_ycsd_has_change_pay_return:'.$distribution_channel_id);
  268. return '';
  269. }
  270. }
  271. \Log::info('action_call_ycsd:'.$distribution_channel_id);
  272. }
  273. $data = [];
  274. $data['send_time'] = date("Y-m-d H:i:s");
  275. $data['action_type'] = $action_type;
  276. if($action_type == 'Register'){
  277. $data['openid'] = $param['openid'];
  278. $data['uid'] = $param['uid'];
  279. }elseif($action_type == 'CreateOrder'){
  280. $data['orderSn'] = $param['order_sn'];
  281. $data['amount'] = $param['amount'];
  282. $data['openid'] = $param['openid'];
  283. $data['uid'] = $param['uid'];
  284. }elseif($action_type == 'CallBackOrder'){
  285. $data['orderSn'] = $param['order_sn'];
  286. $data['openid'] = $param['openid'];
  287. }
  288. $send_data=array(
  289. 'send_time'=>date("Y-m-d H:i:s"),
  290. 'data' => $data
  291. );
  292. \Log::info('PushUserActionToQueue_openid:'.$data['openid'].' action_type:'.$action_type);
  293. \Log::info('$send_data');\Log::info($send_data);
  294. $delay = 0;
  295. $job = (new ActionTrigger($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('action_trigger_list');
  296. dispatch($job);
  297. }catch(\Exception $e){
  298. \Log::info('ept:'.$e->getMessage());
  299. }
  300. }
  301. public static function isCpcUser($uid){
  302. //openid下所有强关超过三天的uid都无充值,对强关后的uid进行曝光
  303. //openid下有一个强关超过三天的uid,都无充值,对强关后的uid进行曝光
  304. $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
  305. JOIN force_subscribe_users f on u.id = f.uid and f.is_subscribed= 1
  306. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  307. $result = DB::select(sprintf($sql_format,$uid));
  308. if(!$result) return false;
  309. $is_has_force_subscribe_three_days =false;
  310. foreach ($result as $item){
  311. //有充值
  312. if($item->oid) return false;
  313. //强关没超过超过三天
  314. //if(time()-strtotime($item->subscribe_time) < 86400*3) return false;
  315. if(time()-strtotime($item->subscribe_time) > 86400*3) $is_has_force_subscribe_three_days = true;
  316. }
  317. return $is_has_force_subscribe_three_days;
  318. }
  319. public static function recordUA($ua,$uid)
  320. {
  321. return UserEnv::recordUA($ua,$uid);
  322. }
  323. }