UserService.php 11 KB

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