MemberLevelService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\GoodsCategoryModel;
  13. use App\Models\MemberLevelModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 用户等级管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Common
  22. */
  23. class MemberLevelService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MemberLevelModel();
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 获取数据列表
  48. * @param array $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. // 分页查询
  55. $list = $this->model->orderBy('id', 'asc')
  56. ->where('mark', 1)
  57. ->where(function ($query) use ($params) {
  58. // 分类名称
  59. if (isset($params['name']) && $params['name']) {
  60. $query->where('name', 'like', "%{$params['name']}%");
  61. }
  62. // 状态筛选
  63. if (isset($params['status']) && $params['status'] !== null && $params['status'] !== '') {
  64. $query->where('status', intval($params['status']));
  65. }
  66. })
  67. ->orderBy('id', 'asc')
  68. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  69. $list = $list->toArray();
  70. return [
  71. 'msg' => '操作成功',
  72. 'code' => 0,
  73. 'data' => $list['data'],
  74. 'count' => $list['total']
  75. ];
  76. }
  77. /**
  78. * 添加或编辑
  79. * @return array
  80. * @since 2020/11/11
  81. * @author laravel开发员
  82. */
  83. public function edit()
  84. {
  85. // 参数
  86. $param = request()->all();
  87. // 验证分类名称
  88. if (empty($param['name'])) {
  89. $this->error = '等级名称不能为空';
  90. return false;
  91. }
  92. if (empty($param['upper_count'])) {
  93. $this->error = '升级所需直推人数不为空';
  94. return false;
  95. }
  96. if (empty($param['bonus'])) {
  97. $this->error = '等级分红奖励金额不为空';
  98. return false;
  99. }
  100. // 设置默认值
  101. if (!isset($param['status'])) {
  102. $param['status'] = 1; // 默认有效
  103. }
  104. if (!isset($param['remark'])) {
  105. $param['remark'] = ''; // 默认备注为空
  106. }
  107. // 保存数据
  108. $result = $this->model->edit($param);
  109. if (!$result) {
  110. $this->error = '操作失败';
  111. return false;
  112. }
  113. return true;
  114. }
  115. /**
  116. * 设置状态
  117. * @param array $params
  118. * @return bool
  119. */
  120. public function setStatus($params)
  121. {
  122. $id = isset($params['id']) ? intval($params['id']) : 0;
  123. $status = isset($params['status']) ? intval($params['status']) : 1;
  124. if (!$id) {
  125. $this->error = '等级ID不能为空';
  126. return false;
  127. }
  128. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  129. if (!$info) {
  130. $this->error = '等级信息不存在';
  131. return false;
  132. }
  133. // 更新状态
  134. $this->model->where('id', $id)->update([
  135. 'status' => $status,
  136. 'update_time' => time()
  137. ]);
  138. $this->error = $status == 1 ? '启用成功' : '禁用成功';
  139. return true;
  140. }
  141. /**
  142. * 删除(重写父类方法)
  143. * @return array
  144. */
  145. public function delete()
  146. {
  147. // 参数
  148. $param = request()->all();
  149. // 记录ID
  150. $id = getter($param, "id");
  151. if (empty($id)) {
  152. return message("ID不能为空", false);
  153. }
  154. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  155. if (!$info) {
  156. return message("信息不存在", false);
  157. }
  158. // 删除(软删除)
  159. $this->model->where('id', $id)->update([
  160. 'mark' => 0,
  161. 'update_time' => time()
  162. ]);
  163. return message("删除成功", true);
  164. }
  165. /**
  166. * 获取选项(用于下拉选择)
  167. * @return array
  168. */
  169. public function options()
  170. {
  171. $map = [['mark', '=', 1], ['status', '=', 1]];
  172. $list = $this->model->where($map)
  173. ->orderBy('id', 'asc')
  174. ->select(['id', 'name', 'upper_count','bonus', 'icon'])
  175. ->get()
  176. ->toArray();
  177. return message(MESSAGE_OK, true, $list);
  178. }
  179. /*
  180. * 各等级身份人数
  181. * @return array|mixed
  182. */
  183. public function getListByLevel()
  184. {
  185. $cacheKey = "caches:member:level_list";
  186. $data = RedisService::get($cacheKey);
  187. if($data){
  188. return $data;
  189. }
  190. $data = $this->model->where(['mark'=>1])
  191. ->select(['id','name','upper_count','bonus','status'])
  192. ->orderBy('id')
  193. ->get()
  194. ->keyBy('id');
  195. $data = $data?$data->toArray() : [];
  196. if($data){
  197. RedisService::set($cacheKey, $data, rand(5,10));
  198. }
  199. return $data;
  200. }
  201. }