// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\GoodsCategoryModel; use App\Models\MemberLevelModel; use App\Services\BaseService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; /** * 用户等级管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class MemberLevelService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new MemberLevelModel(); } /** * 静态入口 */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 获取数据列表 * @param array $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { // 分页查询 $list = $this->model->orderBy('id', 'asc') ->where('mark', 1) ->where(function ($query) use ($params) { // 分类名称 if (isset($params['name']) && $params['name']) { $query->where('name', 'like', "%{$params['name']}%"); } // 状态筛选 if (isset($params['status']) && $params['status'] !== null && $params['status'] !== '') { $query->where('status', intval($params['status'])); } }) ->orderBy('id', 'asc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list->toArray(); return [ 'msg' => '操作成功', 'code' => 0, 'data' => $list['data'], 'count' => $list['total'] ]; } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 参数 $param = request()->all(); // 验证分类名称 if (empty($param['name'])) { $this->error = '等级名称不能为空'; return false; } if (empty($param['upper_count'])) { $this->error = '升级所需直推人数不为空'; return false; } if (empty($param['bonus'])) { $this->error = '等级分红奖励金额不为空'; return false; } // 设置默认值 if (!isset($param['status'])) { $param['status'] = 1; // 默认有效 } if (!isset($param['remark'])) { $param['remark'] = ''; // 默认备注为空 } // 保存数据 $result = $this->model->edit($param); if (!$result) { $this->error = '操作失败'; return false; } return true; } /** * 设置状态 * @param array $params * @return bool */ public function setStatus($params) { $id = isset($params['id']) ? intval($params['id']) : 0; $status = isset($params['status']) ? intval($params['status']) : 1; if (!$id) { $this->error = '等级ID不能为空'; return false; } $info = $this->model->where(['id' => $id, 'mark' => 1])->first(); if (!$info) { $this->error = '等级信息不存在'; return false; } // 更新状态 $this->model->where('id', $id)->update([ 'status' => $status, 'update_time' => time() ]); $this->error = $status == 1 ? '启用成功' : '禁用成功'; return true; } /** * 删除(重写父类方法) * @return array */ public function delete() { // 参数 $param = request()->all(); // 记录ID $id = getter($param, "id"); if (empty($id)) { return message("ID不能为空", false); } $info = $this->model->where(['id' => $id, 'mark' => 1])->first(); if (!$info) { return message("信息不存在", false); } // 删除(软删除) $this->model->where('id', $id)->update([ 'mark' => 0, 'update_time' => time() ]); return message("删除成功", true); } /** * 获取选项(用于下拉选择) * @return array */ public function options() { $map = [['mark', '=', 1], ['status', '=', 1]]; $list = $this->model->where($map) ->orderBy('id', 'asc') ->select(['id', 'name', 'upper_count','bonus']) ->get() ->toArray(); return message(MESSAGE_OK, true, $list); } /* * 各等级身份人数 * @return array|mixed */ public function getListByLevel() { $cacheKey = "caches:member:level_list"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model->where(['mark'=>1]) ->select(['id','name','upper_count','bonus','status']) ->orderBy('id') ->get() ->keyBy('id'); $data = $data?$data->toArray() : []; if($data){ RedisService::set($cacheKey, $data, rand(5,10)); } return $data; } }