| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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;
- }
- }
|