User.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. use DB;
  11. class User extends Model
  12. {
  13. protected $table = 'users';
  14. protected $fillable =
  15. ['id', 'openid', 'unionid', 'distribution_channel_id', 'head_img',
  16. 'register_ip', 'send_order_id', 'balance', 'sex', 'country', 'city',
  17. 'province', 'nickname', 'charge_balance', 'reward_balance', 'is_new', 'nickname'
  18. ];
  19. /**
  20. * 获取用户信息列表
  21. * @param $pageSize 每页显示的条数
  22. */
  23. static function getUserList($limit = 10000, $offset = 0)
  24. {
  25. return self::orderBy('id', 'asc')->limit($limit)->offset($offset)->get();
  26. }
  27. /**
  28. * 获取用户信息列表
  29. * @param $pageSize 每页显示的条数
  30. */
  31. static function getPaginationList($pageSize = 20, $careStatus = 0)
  32. {
  33. if (0 == $careStatus) {
  34. return self::orderBy('id', 'desc')->paginate($pageSize);
  35. } elseif (1 == $careStatus) {
  36. return self::where('head_img', '!=', '')->orderBy('id', 'desc')->paginate($pageSize);
  37. } elseif (2 == $careStatus) {
  38. return self::where('head_img', '=', '')->orderBy('id', 'desc')->paginate($pageSize);
  39. }
  40. }
  41. /**
  42. * 根据id获取用户信息
  43. */
  44. static function getById($id)
  45. {
  46. return self::find($id);
  47. }
  48. /**
  49. * 根据id获取用户信息
  50. */
  51. static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
  52. {
  53. return self::where(['distribution_channel_id' => $distribution_channel_id, 'nickname' => $nickname])->first();
  54. }
  55. /**
  56. * 根据id获取用户信息
  57. */
  58. static function getUserDataById($id)
  59. {
  60. return self::where('id', $id)->get();
  61. }
  62. /**
  63. * 创建用户
  64. */
  65. static function addUser($params)
  66. {
  67. $openid = isset($params['openid']) ? $params['openid'] : '';
  68. $unionid = isset($params['unionid']) ? $params['unionid'] : '';
  69. $distribution_channel_id = isset($params['distribution_channel_id']) ? $params['distribution_channel_id'] : '';
  70. if ($openid && $unionid && $distribution_channel_id) {
  71. $province = isset($params['province']) ? $params['province'] : '';
  72. $city = isset($params['city']) ? $params['city'] : '';
  73. $country = isset($params['country']) ? $params['country'] : '';
  74. $head_img = isset($params['headimgurl']) ? $params['headimgurl'] : '';
  75. $send_order_id = isset($params['send_order_id']) ? $params['send_order_id'] : 0;
  76. $sex = isset($params['sex']) ? $params['sex'] : 0;
  77. $register_ip = isset($params['register_ip']) ? $params['register_ip'] : '';
  78. $is_new = isset($params['is_new']) ? $params['is_new'] : 0;
  79. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  80. $base_data = compact('unionid', 'distribution_channel_id');
  81. $extra_data = compact('openid', 'province', 'city', 'country', 'head_img', 'send_order_id', 'sex', 'register_ip', 'is_new', 'nickname');
  82. return self::firstOrCreate($base_data, $extra_data);
  83. }
  84. }
  85. /**
  86. * 更新用户
  87. */
  88. static function updateInfo($id, $params = [])
  89. {
  90. return self::where('id', $id)->update($params);
  91. }
  92. static function getUser($uid)
  93. {
  94. return self::where('id', $uid)->first();
  95. }
  96. /**
  97. * 用户余额添加
  98. * @param $uid
  99. * @param $fee
  100. * @return mixed
  101. */
  102. static function addBalance($uid, $fee, $charge, $given)
  103. {
  104. $res1 = User::where('id', $uid)->increment('balance', $fee);
  105. $res2 = User::where('id', $uid)->increment('charge_balance', $charge);
  106. if ($given) User::where('id', $uid)->increment('reward_balance', $given);
  107. return $res1 && $res2;
  108. }
  109. //获取推广用户总数
  110. static function getPromotionTotal($send_order_id)
  111. {
  112. return self::where('send_order_id', $send_order_id)->count();
  113. }
  114. /**
  115. * 查询渠道某天注册用户总数
  116. * @param $channel_id
  117. * @param $date
  118. * @return mixed
  119. */
  120. static function getChannelDayTotal($channel_id, $date)
  121. {
  122. $end_date = date('Y-m-d', strtotime($date) + 86400);
  123. return self::where('distribution_channel_id', $channel_id)->where('created_at', '>=', $date)->where('created_at', '<', $end_date)->count();
  124. }
  125. /**
  126. * 查询渠道注册用户总数
  127. * @param $channel_id
  128. * @return mixed
  129. */
  130. static function getChannelTotal($channel_id)
  131. {
  132. return self::where('distribution_channel_id', $channel_id)->count();
  133. }
  134. /**
  135. * 查询渠道某段时间注册用户总数
  136. * @param $channel_id
  137. * @param $startDate
  138. * @param $endDate
  139. * @return mixed
  140. */
  141. static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
  142. {
  143. $searchObj = self::whereIn('distribution_channel_id', $channel_id);
  144. if ($startDate) {
  145. $searchObj->where('created_at', '>=', $startDate);
  146. }
  147. if ($endDate) {
  148. $searchObj->where('created_at', '<', $endDate);
  149. }
  150. return $searchObj->count();
  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. $searchObj = DB::table(DB::raw('users use index(distribution_channel_created)'))->whereIn('distribution_channel_id', $channelIds);
  162. //$searchObj = self::whereIn('distribution_channel_id', $channelIds);
  163. if ($startDate) {
  164. $searchObj->where('created_at', '>=', $startDate);
  165. }
  166. if ($endDate) {
  167. $searchObj->where('created_at', '<', $endDate);
  168. }
  169. //\Log::info($searchObj->toSql());
  170. return $searchObj->count();
  171. }
  172. /**
  173. * 查询注册用户总数
  174. * @param $params
  175. */
  176. static function getTotalCount($params)
  177. {
  178. $searchObj = self::orderBy('id', 'desc');
  179. if (isset($params['begin_time'])) $searchObj->where('created_at', '>=', $params['begin_time']);
  180. if (isset($params['end_time'])) $searchObj->where('created_at', '<=', $params['end_time']);
  181. return $searchObj->count();
  182. }
  183. static function search($params)
  184. {
  185. $searchObj = self::orderBy('id', 'desc');
  186. if (isset($params['begin_time'])) $searchObj->where('created_at', '>=', $params['begin_time']);
  187. if (isset($params['end_time'])) $searchObj->where('created_at', '<=', $params['end_time']);
  188. return $searchObj;
  189. }
  190. static function getIdIndex($params)
  191. {
  192. $searchObj = self::orderBy('id', 'desc');
  193. if (isset($params['begin_time'])) $searchObj->where('created_at', '>=', $params['begin_time']);
  194. if (isset($params['end_time'])) $searchObj->where('created_at', '<=', $params['end_time']);
  195. $searchObj->select(
  196. DB::raw('max(id) max_id'),
  197. DB::raw('min(id) min_id')
  198. );
  199. return $searchObj->first();
  200. }
  201. static function getUserDataDetailById($id)
  202. {
  203. return self::where('users.id', $id)
  204. ->select(DB::raw("users.*,year_orders.begin_time,year_orders.end_time"))
  205. ->leftjoin('year_orders', 'year_orders.uid', '=', 'users.id')
  206. ->get();
  207. }
  208. }