MemberService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. $adminUid = isset($params['admin_uid'])? intval($params['admin_uid']) : 0;
  72. if($adminUid){
  73. $query->where('a.market_uid','=', $adminUid);
  74. }
  75. $status = isset($params['status'])? intval($params['status']) : 0;
  76. if($status){
  77. $query->where('a.status','=',$status);
  78. }
  79. $source = isset($params['source'])? intval($params['source']) : 0;
  80. if($source){
  81. $query->where('a.source','=',$source);
  82. }
  83. })
  84. ->select(['a.*','b.realname as market_name','b.nickname as market_nickname'])
  85. ->orderBy('a.create_time','desc')
  86. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  87. $list = $list? $list->toArray() :[];
  88. if($list){
  89. foreach($list['data'] as &$item){
  90. $item['create_time_text'] = isset($item['create_time']) && $item['create_time']? datetime($item['create_time'],'Y.m.d H:i') : '';
  91. $item['age'] = isset($item['birthday']) && $item['birthday']? max(0,date('Y') - date('Y', $item['birthday'])) : '';
  92. $item['birthday'] = isset($item['birthday']) && $item['birthday']? date('Y-m-d', $item['birthday']) : '';
  93. }
  94. }
  95. return [
  96. 'pageSize'=> $pageSize,
  97. 'total'=>isset($list['total'])? $list['total'] : 0,
  98. 'list'=> isset($list['data'])? $list['data'] : []
  99. ];
  100. }
  101. /**
  102. * 统计推广员录入会员数量
  103. * @param int $marketUid 推广员ID:0-全部
  104. * @return array|int[]|mixed
  105. */
  106. public function getCountsByMarket($marketUid=0)
  107. {
  108. $cacheKey = "caches:counts:member:{$marketUid}";
  109. $counts = RedisService::get($cacheKey);
  110. if($counts){
  111. return $counts;
  112. }
  113. $counts = [
  114. 'total'=> 0,
  115. 'month'=> 0,
  116. 'last_month'=> 0,
  117. 'last_day'=> 0,
  118. 'day'=> 0,
  119. ];
  120. $where = ['mark'=>1, 'status'=> 1];
  121. if($marketUid){
  122. $where['market_uid'] = $marketUid;
  123. }
  124. // 总数
  125. $counts['total'] = $this->model->where($where)->count('id');
  126. // 上个月
  127. $counts['last_month'] = $this->model->where($where)
  128. ->where('create_time','>=', strtotime('-1 month'))
  129. ->where('create_time','<', strtotime(date('Y-m-01')))
  130. ->count('id');
  131. // 本月
  132. $counts['month'] = $this->model->where($where)
  133. ->where('create_time','>=', strtotime(date('Y-m-01')))
  134. ->count('id');
  135. // 昨日
  136. $counts['last_day'] = $this->model->where($where)
  137. ->where('create_time','>=', strtotime('-1 day'))
  138. ->where('create_time','<', strtotime(date('Y-m-d')))
  139. ->count('id');
  140. // 今日
  141. $counts['day'] = $this->model->where($where)
  142. ->where('create_time','>=', strtotime(date('Y-m-01')))
  143. ->count('id');
  144. RedisService::set($cacheKey, $counts, rand(3, 5));
  145. return $counts;
  146. }
  147. /**
  148. * 添加会编辑会员
  149. * @return array
  150. * @since 2020/11/11
  151. * @author laravel开发员
  152. */
  153. public function edit($marketUid=0)
  154. {
  155. // 请求参数
  156. $data = request()->all();
  157. // 头像处理
  158. if(isset($data['avatar'])){
  159. $avatar = trim($data['avatar']);
  160. if (strpos($avatar, "temp")) {
  161. $data['avatar'] = save_image($avatar, 'member');
  162. } else {
  163. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  164. }
  165. }
  166. // 出生日期
  167. if (isset($data['birthday'])) {
  168. $data['birthday'] = strtotime($data['birthday'].' 00:00:00');
  169. }
  170. // 城市处理
  171. $city = isset($data['city']) ? $data['city'] : [];
  172. if (!empty($data['city'])) {
  173. // 省份
  174. $data['province_id'] = $city[0];
  175. // 城市
  176. $data['city_id'] = $city[1];
  177. // 县区
  178. $data['district_id'] = $city[2];
  179. }
  180. unset($data['city']);
  181. $data['market_uid'] = $marketUid;
  182. return parent::edit($data); // TODO: Change the autogenerated stub
  183. }
  184. }