UserService.php 7.0 KB

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