UserService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. class UserService
  13. {
  14. /**
  15. * 创建用户
  16. * $params
  17. * 必穿参数 openid、unionid、distribution_channel_id
  18. * 可传 province、city、country、headimgurl、send_order_id、sex、register_ip
  19. */
  20. static function addUser($params)
  21. {
  22. return User::addUser($params);
  23. }
  24. static function getUserList($limit, $offset)
  25. {
  26. return User::getUserList($limit, $offset);
  27. }
  28. static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
  29. {
  30. return User::getUserByNickAndChannelId($distribution_channel_id, $nickname);
  31. }
  32. /**
  33. * 获取用户信息列表
  34. * @param $pageSize 每页显示的条数
  35. */
  36. static function getPaginationList($pageSize = 20)
  37. {
  38. return User::getPaginationList($pageSize);
  39. }
  40. /**
  41. * 获取用户信息列表
  42. * @param $pageSize 每页显示的条数
  43. */
  44. static function getUsers($pageSize = 20, $careStatus)
  45. {
  46. return User::getPaginationList($pageSize, $careStatus);
  47. }
  48. /**
  49. * 根据id获取用户信息
  50. * @return user object
  51. */
  52. static function getById($id)
  53. {
  54. return User::getById($id);
  55. }
  56. /**
  57. * 根据id获取用户信息
  58. * @return user object
  59. */
  60. static function getUserDataById($id)
  61. {
  62. return User::getUserDataById($id);
  63. }
  64. /**
  65. * 根据id获取用户信息
  66. * @return user object
  67. */
  68. static function getUserDataDetailById($id)
  69. {
  70. return User::getUserDataDetailById($id);
  71. }
  72. /**
  73. * 根据id获取用户名称
  74. * @return string [用户昵称]
  75. */
  76. static function getNicknameById($id)
  77. {
  78. return User::getById($id)->nickname;
  79. }
  80. /**
  81. * 更新用户信息
  82. * @param int $id 用户ID
  83. * @param array $params 可传 province、city、country、headimgurl、sex
  84. * @return boolen
  85. */
  86. static function updateInfo($id, $params)
  87. {
  88. \Log::info('~~~~~~~~~~~~~~~~~====update User====~~~~~~~~~~~~~~~~' . "\n");
  89. \Log::info($id);
  90. \Log::info($params);
  91. return User::updateInfo($id, $params);
  92. }
  93. /**
  94. * 根据union和分销id获取用户信息
  95. * @param $union
  96. * @param $channel_id
  97. * @return mixed
  98. */
  99. static function getUserByUnionAndChannelId($openid, $channel_id)
  100. {
  101. return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->select('id', 'openid', 'unionid')->first();
  102. }
  103. /**
  104. * 用户余额充值
  105. * @param $uid
  106. * @param $fee
  107. * @return mixed
  108. */
  109. static function addBalance($uid, $fee, $charge, $given)
  110. {
  111. return User::addBalance($uid, $fee, $charge, $given);
  112. }
  113. /**
  114. * 查询推广注册用户总数
  115. * @param $send_order_id
  116. * @return mixed
  117. */
  118. static function getPromotionTotal($send_order_id)
  119. {
  120. return User::getPromotionTotal($send_order_id);
  121. }
  122. /**
  123. * 查询渠道某天注册用户总数
  124. * @param $channel_id
  125. * @param $date
  126. * @return mixed
  127. */
  128. static function getChannelDayTotal($channel_id, $date)
  129. {
  130. return User::getChannelDayTotal($channel_id, $date);
  131. }
  132. /**
  133. * 查询渠道注册用户总数
  134. * @param $channel_id
  135. * @return mixed
  136. */
  137. static function getChannelTotal($channel_id)
  138. {
  139. return User::getChannelTotal($channel_id);
  140. }
  141. /**
  142. * 查询渠道某段时间注册用户总数
  143. * @param $channel_id
  144. * @param $startDate
  145. * @param $endDate
  146. * @return mixed
  147. */
  148. static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
  149. {
  150. return User::getChannelDayToDayTotal($channel_id, $startDate, $endDate);
  151. }
  152. /**
  153. * 查询渠道某段时间注册用户总数
  154. * @param array $channelIds
  155. * @param $startDate
  156. * @param $endDate
  157. * @return mixed
  158. */
  159. static function getChannelsDayToDayTotal($channelIds, $startDate = '', $endDate = '')
  160. {
  161. return User::getChannelsDayToDayTotal($channelIds, $startDate, $endDate);
  162. }
  163. /**
  164. * 查询注册用户总数
  165. * @param $params
  166. */
  167. static function getTotalCount($params)
  168. {
  169. return User::getTotalCount($params);
  170. }
  171. /**
  172. * 查询渠道是否有登录
  173. * @param $channelId
  174. * @param $start_time
  175. * @param $end_time
  176. * @return int
  177. */
  178. static function judgeUserYesterdayLogin($channelId, $start_time, $end_time)
  179. {
  180. $user_behavior = DB::table('channel_operate_record')
  181. ->where([
  182. ['distribution_channel_id', '=', $channelId],
  183. ['created_at', '>=', $start_time],
  184. ['created_at', '<=', $end_time]
  185. ])
  186. ->first();
  187. if ($user_behavior) return 1;
  188. return 0;
  189. }
  190. /**
  191. * @param $date
  192. * @param $company_id
  193. * @return mixed
  194. */
  195. static function getCompanyDayTotal($date, $company_id)
  196. {
  197. $end_date = date('Y-m-d', strtotime($date) + 86400);
  198. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  199. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  200. ->where([
  201. ['users.created_at', '>=', $date],
  202. ['users.created_at', '<', $end_date],
  203. ['channel_users.company_id', '=', $company_id]
  204. ])
  205. ->groupBy('channel_users.company_id')
  206. ->select(DB::raw("count(users.id) as register_sum"))
  207. ->first();
  208. if ($info) return $info->register_sum;
  209. //\Log::info('getCompanyDayTotal error:' . $company_id);
  210. return $info;
  211. }
  212. static function getCompanyTotal($company_id)
  213. {
  214. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  215. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  216. ->where('channel_users.company_id', $company_id)
  217. ->groupBy('channel_users.company_id')
  218. ->select(DB::raw("count(users.id) as register_sum"))
  219. ->first();
  220. if ($info) return $info->register_sum;
  221. //\Log::error('getCompanyTotal error:' . $company_id);
  222. return $info;
  223. }
  224. static function transfromBalanceByUser($from_uid, $to_uid)
  225. {
  226. $add_balance_sql = "update users,(select * from users where id =" . $from_uid . ") as u2
  227. set users.balance = users.balance + u2.balance,
  228. users.reward_balance = users.reward_balance +u2.reward_balance,
  229. users.charge_balance = users.charge_balance +u2.charge_balance where users.id =" . $to_uid;
  230. $res = DB::update($add_balance_sql);
  231. return User::updateInfo($from_uid, ['balance' => 0, 'reward_balance' => 0, 'charge_balance' => 0]);
  232. }
  233. // 扔用户一些动作到队列,异步处理一些额外逻辑
  234. static function PushUserActionToQueue($action_type,$distribution_channel_id,$param){
  235. try{
  236. // 判断站点是否需要额外逻辑
  237. $data = [];
  238. $data['send_time'] = date("Y-m-d H:i:s");
  239. $data['action_type'] = $action_type;
  240. if($action_type == 'Register'){
  241. $data['openid'] = $param['openid'];
  242. $data['uid'] = $param['uid'];
  243. }elseif($action_type == 'CreateOrder'){
  244. $data['orderSn'] = $param['order_sn'];
  245. $data['amount'] = $param['amount'];
  246. $data['openid'] = $param['openid'];
  247. $data['uid'] = $param['uid'];
  248. }elseif($action_type == 'CallBackOrder'){
  249. $data['orderSn'] = $param['order_sn'];
  250. $data['openid'] = $param['openid'];
  251. }
  252. $send_data=array(
  253. 'send_time'=>date("Y-m-d H:i:s"),
  254. 'data' => $data
  255. );
  256. \Log::info('PushUserActionToQueue_openid:'.$data['openid'].' action_type:'.$action_type);
  257. \Log::info('$send_data');\Log::info($send_data);
  258. $delay = 0;
  259. $job = (new ActionTrigger($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('action_trigger_list');
  260. dispatch($job);
  261. }catch(\Exception $e){
  262. \Log::info('ept:'.$e->getMessage());
  263. }
  264. }
  265. }