MemberService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\MemberModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 会员管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class MemberService
  20. * @package App\Services\Common
  21. */
  22. class MemberService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * MemberService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MemberModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1];
  55. $list = $this->model->from('member as a')
  56. ->leftJoin('user as b', 'b.id', '=', 'a.market_uid')
  57. ->where($where)
  58. ->where(function ($query) use($params){
  59. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  60. if($keyword){
  61. $query->where('a.realname','like',"%{$keyword}%");
  62. }
  63. $mobile = isset($params['mobile'])? $params['mobile'] : '';
  64. if($mobile){
  65. $query->where('a.mobile','like',"%{$mobile}%");
  66. }
  67. $marketName = isset($params['market_name'])? $params['market_name'] : '';
  68. if($marketName){
  69. $query->where('b.realname','like',"%{$marketName}%");
  70. }
  71. $status = isset($params['status'])? intval($params['status']) : 0;
  72. if($status){
  73. $query->where('a.status','=',$status);
  74. }
  75. $source = isset($params['source'])? intval($params['source']) : 0;
  76. if($source){
  77. $query->where('a.source','=',$source);
  78. }
  79. })
  80. ->select(['a.*','b.realname as market_name','b.nickname as market_nickname'])
  81. ->orderBy('a.create_time','desc')
  82. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  83. $list = $list? $list->toArray() :[];
  84. if($list){
  85. foreach($list['data'] as &$item){
  86. $item['create_time_text'] = isset($item['create_time']) && $item['create_time']? datetime($item['create_time'],'Y.m.d H:i') : '';
  87. $item['age'] = isset($item['birthday']) && $item['birthday']? max(0,date('Y') - date('Y', $item['birthday'])) : '';
  88. $item['birthday'] = isset($item['birthday']) && $item['birthday']? date('Y-m-d', $item['birthday']) : '';
  89. }
  90. }
  91. return [
  92. 'pageSize'=> $pageSize,
  93. 'total'=>isset($list['total'])? $list['total'] : 0,
  94. 'list'=> isset($list['data'])? $list['data'] : []
  95. ];
  96. }
  97. /**
  98. * 统计推广员录入会员数量
  99. * @param int $marketUid 推广员ID:0-全部
  100. * @return array|int[]|mixed
  101. */
  102. public function getCountsByMarket($marketUid=0)
  103. {
  104. $cacheKey = "caches:counts:member:{$marketUid}";
  105. $counts = RedisService::get($cacheKey);
  106. if($counts){
  107. return $counts;
  108. }
  109. $counts = [
  110. 'total'=> 0,
  111. 'month'=> 0,
  112. 'last_month'=> 0,
  113. 'last_day'=> 0,
  114. 'day'=> 0,
  115. ];
  116. $where = ['mark'=>1, 'status'=> 1];
  117. if($marketUid){
  118. $where['market_uid'] = $marketUid;
  119. }
  120. // 总数
  121. $counts['total'] = $this->model->where($where)->count('id');
  122. // 上个月
  123. $counts['last_month'] = $this->model->where($where)
  124. ->where('create_time','>=', strtotime('-1 month'))
  125. ->where('create_time','<', strtotime(date('Y-m-01')))
  126. ->count('id');
  127. // 本月
  128. $counts['month'] = $this->model->where($where)
  129. ->where('create_time','>=', strtotime(date('Y-m-01')))
  130. ->count('id');
  131. // 昨日
  132. $counts['last_day'] = $this->model->where($where)
  133. ->where('create_time','>=', strtotime('-1 day'))
  134. ->where('create_time','<', strtotime(date('Y-m-d')))
  135. ->count('id');
  136. // 今日
  137. $counts['day'] = $this->model->where($where)
  138. ->where('create_time','>=', strtotime(date('Y-m-01')))
  139. ->count('id');
  140. RedisService::set($cacheKey, $counts, rand(3, 5));
  141. return $counts;
  142. }
  143. /**
  144. * 添加会编辑会员
  145. * @return array
  146. * @since 2020/11/11
  147. * @author laravel开发员
  148. */
  149. public function edit($marketUid=0)
  150. {
  151. // 请求参数
  152. $data = request()->all();
  153. // 头像处理
  154. if(isset($data['avatar'])){
  155. $avatar = trim($data['avatar']);
  156. if (strpos($avatar, "temp")) {
  157. $data['avatar'] = save_image($avatar, 'member');
  158. } else {
  159. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  160. }
  161. }
  162. // 出生日期
  163. if (isset($data['birthday'])) {
  164. $data['birthday'] = strtotime($data['birthday'].' 00:00:00');
  165. }
  166. // 城市处理
  167. $city = isset($data['city']) ? $data['city'] : [];
  168. if (!empty($data['city'])) {
  169. // 省份
  170. $data['province_id'] = $city[0];
  171. // 城市
  172. $data['city_id'] = $city[1];
  173. // 县区
  174. $data['district_id'] = $city[2];
  175. }
  176. unset($data['city']);
  177. $data['market_uid'] = $marketUid;
  178. return parent::edit($data); // TODO: Change the autogenerated stub
  179. }
  180. }