SystemUserLevelServices.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user\level;
  13. use app\services\BaseServices;
  14. use app\dao\user\level\SystemUserLevelDao;
  15. use app\services\user\UserServices;
  16. /**
  17. * 系统设置用户等级
  18. * Class SystemUserLevelServices
  19. * @package app\services\user\level
  20. * @mixin SystemUserLevelDao
  21. */
  22. class SystemUserLevelServices extends BaseServices
  23. {
  24. /**
  25. * SystemUserLevelServices constructor.
  26. * @param SystemUserLevelDao $dao
  27. */
  28. public function __construct(SystemUserLevelDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 单个等级
  34. * @param int $id
  35. * @param string $field
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getLevel(int $id, string $field = '*')
  42. {
  43. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field);
  44. }
  45. /**
  46. * 获取会员等级信息缓存
  47. * @author 等风来
  48. * @email 136327134@qq.com
  49. * @date 2022/11/14
  50. * @param int $id
  51. * @return array|false|mixed|string|null
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getLevelCache(int $id)
  57. {
  58. $level = $this->dao->cacheRemember($id, function () use ($id) {
  59. $level = $this->getLevel($id);
  60. if ($level) {
  61. return $level->toArray();
  62. } else {
  63. return null;
  64. }
  65. });
  66. return $level;
  67. }
  68. /**
  69. * 获取某条件等级
  70. * @param array $where
  71. * @param string $field
  72. * @return array
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function getWhereLevel(array $where, string $field = '*')
  78. {
  79. return $this->dao->getOne($where, $field);
  80. }
  81. /**
  82. * 获取所有等级列表
  83. * @param string $field
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function getLevelList(array $where, string $field = '*')
  90. {
  91. $where_data = [];
  92. if (isset($where['is_show']) && $where['is_show'] !== '') $where_data[] = ['is_show', '=', $where['is_show']];
  93. if (isset($where['title']) && $where['title']) $where_data[] = ['name', 'LIKE', "%$where[title]%"];
  94. $where_data[] = ['is_del', '=', '0'];
  95. [$page, $limit] = $this->getPageValue();
  96. $list = $this->dao->getList($where_data, $field ?? '*', $page, $limit);
  97. foreach ($list as &$item) {
  98. $item['image'] = set_file_url($item['image']);
  99. $item['icon'] = set_file_url($item['icon']);
  100. }
  101. $count = $this->dao->getCount($where_data);
  102. return compact('list', 'count');
  103. }
  104. /**
  105. * 获取条件的会员等级列表
  106. * @param array $where
  107. * @param string $field
  108. */
  109. public function getWhereLevelList(array $where, string $field = '*')
  110. {
  111. if ($where) {
  112. $whereData = [['is_show', '=', 1], ['is_del', '=', 0], $where];
  113. } else {
  114. $whereData = [['is_show', '=', 1], ['is_del', '=', 0]];
  115. }
  116. return $this->dao->getList($whereData, $field ?? '*');
  117. }
  118. /**
  119. * 获取一些用户等级名称
  120. * @param $ids
  121. * @return array
  122. */
  123. public function getUsersLevel($ids)
  124. {
  125. return $this->dao->getColumn([['id', 'IN', $ids]], 'name', 'id');
  126. }
  127. /**
  128. * 获取会员等级列表
  129. * @param int $leval_id
  130. * @return array
  131. */
  132. public function getLevelListAndGrade(int $leval_id = 0, string $field = 'name,discount,image,icon,explain,id,grade,is_forever,valid_date,exp_num')
  133. {
  134. $list = $this->dao->getList(['is_del' => 0, 'is_show' => 1], $field);
  135. if ($list) {
  136. $listNew = array_combine(array_column($list, 'id'), $list);
  137. $grade = $listNew[$leval_id]['grade'] ?? 0;
  138. foreach ($list as &$item) {
  139. if ($grade < $item['grade'])
  140. $item['is_clear'] = true;
  141. else
  142. $item['is_clear'] = false;
  143. $item['task_list'] = [];
  144. }
  145. }
  146. return $list;
  147. }
  148. /**
  149. * 获取用户等级折扣
  150. * @param int $uid
  151. * @param int $id
  152. * @return null
  153. * @throws \think\db\exception\DataNotFoundException
  154. * @throws \think\db\exception\DbException
  155. * @throws \think\db\exception\ModelNotFoundException
  156. */
  157. public function getDiscount(int $uid, int $id)
  158. {
  159. $discount = null;
  160. /** @var UserServicesr $userServices */
  161. $userServices = app()->make(UserServices::class);
  162. $userInfo = $userServices->getOne(['uid' => $uid], 'uid,level,level_status');
  163. //需要会员是激活状态
  164. if ($id && $userInfo && $userInfo['level_status']) {
  165. $level = $this->dao->getOne(['id' => $id, 'is_del' => 0, 'is_show' => 1], 'id,discount');
  166. if (!$level) {//用户存在等级ID 但是被删除或者下架重新检测升级
  167. try {
  168. /** @var UserLevelServices $userLeveleServices */
  169. $userLeveleServices = app()->make(UserLevelServices::class);
  170. $userLeveleServices->detection($uid);
  171. } catch (\Throwable $e) {
  172. }
  173. $level = $this->dao->getOne(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'id,discount');
  174. }
  175. $discount = $level['discount'] ?? null;
  176. }
  177. return $discount;
  178. }
  179. }