SystemUserLevelServices.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\system;
  13. use app\services\BaseServices;
  14. use app\dao\system\SystemUserLevelDao;
  15. /**
  16. *
  17. * Class SystemUserLevelServices
  18. * @package app\services\system
  19. */
  20. class SystemUserLevelServices extends BaseServices
  21. {
  22. /**
  23. * SystemUserLevelServices constructor.
  24. * @param SystemUserLevelDao $dao
  25. */
  26. public function __construct(SystemUserLevelDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 单个等级
  32. * @param int $id
  33. * @param string $field
  34. * @return array
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function getLevel(int $id, string $field = '*')
  40. {
  41. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field);
  42. }
  43. /**
  44. * 获取某条件等级
  45. * @param array $where
  46. * @param string $field
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getWhereLevel(array $where, string $field = '*')
  53. {
  54. return $this->dao->getOne($where, $field);
  55. }
  56. /**
  57. * 获取所有等级列表
  58. * @param string $field
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function getLevelList(array $where, string $field = '*')
  65. {
  66. $where_data = [];
  67. if (isset($where['is_show']) && $where['is_show'] !== '') $where_data[] = ['is_show', '=', $where['is_show']];
  68. if (isset($where['title']) && $where['title']) $where_data[] = ['name', 'LIKE', "%$where[title]%"];
  69. $where_data[] = ['is_del', '=', '0'];
  70. [$page, $limit] = $this->getPageValue();
  71. $list = $this->dao->getList($where_data, $field ?? '*', $page, $limit);
  72. foreach ($list as &$item){
  73. $item['image'] = set_file_url($item['image']);
  74. $item['icon'] = set_file_url($item['icon']);
  75. }
  76. $count = $this->dao->getCount($where_data);
  77. return compact('list', 'count');
  78. }
  79. /**
  80. * 获取条件的会员等级列表
  81. * @param array $where
  82. * @param string $field
  83. */
  84. public function getWhereLevelList(array $where, string $field = '*')
  85. {
  86. if ($where) {
  87. $whereData = [['is_show', '=', 1], ['is_del', '=', 0], $where];
  88. } else {
  89. $whereData = [['is_show', '=', 1], ['is_del', '=', 0]];
  90. }
  91. return $this->dao->getList($whereData, $field ?? '*');
  92. }
  93. /**
  94. * 获取一些用户等级名称
  95. * @param $ids
  96. * @return array
  97. */
  98. public function getUsersLevel($ids)
  99. {
  100. return $this->dao->getColumn([['id', 'IN', $ids]], 'name', 'id');
  101. }
  102. /**
  103. * 获取会员等级列表
  104. * @param int $leval_id
  105. * @return array
  106. */
  107. public function getLevelListAndGrade(int $leval_id = 0, string $field = 'name,discount,image,icon,explain,id,grade,is_forever,valid_date,exp_num')
  108. {
  109. $list = $this->dao->getList(['is_del' => 0, 'is_show' => 1], $field);
  110. if ($list) {
  111. $listNew = array_combine(array_column($list, 'id'), $list);
  112. $grade = $listNew[$leval_id]['grade'] ?? 0;
  113. foreach ($list as &$item) {
  114. if ($grade < $item['grade'])
  115. $item['is_clear'] = true;
  116. else
  117. $item['is_clear'] = false;
  118. $item['task_list'] = [];
  119. }
  120. }
  121. return $list;
  122. }
  123. }