| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 会员管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class MemberService
- * @package App\Services\Common
- */
- class MemberService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * MemberService constructor.
- */
- public function __construct()
- {
- $this->model = new MemberModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1];
- $list = $this->model->from('member as a')
- ->leftJoin('user as b', 'b.id', '=', 'a.market_uid')
- ->where($where)
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('a.realname','like',"%{$keyword}%");
- }
- $mobile = isset($params['mobile'])? $params['mobile'] : '';
- if($mobile){
- $query->where('a.mobile','like',"%{$mobile}%");
- }
- $marketName = isset($params['market_name'])? $params['market_name'] : '';
- if($marketName){
- $query->where('b.realname','like',"%{$marketName}%");
- }
- $adminUid = isset($params['admin_uid'])? intval($params['admin_uid']) : 0;
- if($adminUid){
- $query->where('a.market_uid','=', $adminUid);
- }
- $status = isset($params['status'])? intval($params['status']) : 0;
- if($status){
- $query->where('a.status','=',$status);
- }
- $source = isset($params['source'])? intval($params['source']) : 0;
- if($source){
- $query->where('a.source','=',$source);
- }
- })
- ->select(['a.*','b.realname as market_name','b.nickname as market_nickname'])
- ->orderBy('a.create_time','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time_text'] = isset($item['create_time']) && $item['create_time']? datetime($item['create_time'],'Y.m.d H:i') : '';
- $item['age'] = isset($item['birthday']) && $item['birthday']? max(0,date('Y') - date('Y', $item['birthday'])) : '';
- $item['birthday'] = isset($item['birthday']) && $item['birthday']? date('Y-m-d', $item['birthday']) : '';
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 统计推广员录入会员数量
- * @param int $marketUid 推广员ID:0-全部
- * @return array|int[]|mixed
- */
- public function getCountsByMarket($marketUid=0)
- {
- $cacheKey = "caches:counts:member:{$marketUid}";
- $counts = RedisService::get($cacheKey);
- if($counts){
- return $counts;
- }
- $counts = [
- 'total'=> 0,
- 'month'=> 0,
- 'last_month'=> 0,
- 'last_day'=> 0,
- 'day'=> 0,
- ];
- $where = ['mark'=>1, 'status'=> 1];
- if($marketUid){
- $where['market_uid'] = $marketUid;
- }
- // 总数
- $counts['total'] = $this->model->where($where)->count('id');
- // 上个月
- $counts['last_month'] = $this->model->where($where)
- ->where('create_time','>=', strtotime('-1 month'))
- ->where('create_time','<', strtotime(date('Y-m-01')))
- ->count('id');
- // 本月
- $counts['month'] = $this->model->where($where)
- ->where('create_time','>=', strtotime(date('Y-m-01')))
- ->count('id');
- // 昨日
- $counts['last_day'] = $this->model->where($where)
- ->where('create_time','>=', strtotime('-1 day'))
- ->where('create_time','<', strtotime(date('Y-m-d')))
- ->count('id');
- // 今日
- $counts['day'] = $this->model->where($where)
- ->where('create_time','>=', strtotime(date('Y-m-01')))
- ->count('id');
- RedisService::set($cacheKey, $counts, rand(3, 5));
- return $counts;
- }
- /**
- * 添加会编辑会员
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit($marketUid=0)
- {
- // 请求参数
- $data = request()->all();
- // 头像处理
- if(isset($data['avatar'])){
- $avatar = trim($data['avatar']);
- if (strpos($avatar, "temp")) {
- $data['avatar'] = save_image($avatar, 'member');
- } else {
- $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
- }
- }
- // 出生日期
- if (isset($data['birthday'])) {
- $data['birthday'] = strtotime($data['birthday'].' 00:00:00');
- }
- // 城市处理
- $city = isset($data['city']) ? $data['city'] : [];
- if (!empty($data['city'])) {
- // 省份
- $data['province_id'] = $city[0];
- // 城市
- $data['city_id'] = $city[1];
- // 县区
- $data['district_id'] = $city[2];
- }
- unset($data['city']);
- $data['market_uid'] = $marketUid;
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- }
|