MemberRightServices.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. namespace app\services\user\member;
  12. use app\dao\user\member\MemberRightDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * Class MemberRightServices
  17. * @package app\services\user\member
  18. * @mixin MemberRightDao
  19. */
  20. class MemberRightServices extends BaseServices
  21. {
  22. /**
  23. * MemberRightServices constructor.
  24. * @param MemberRightDao $memberRightDao
  25. */
  26. public function __construct(MemberRightDao $memberRightDao)
  27. {
  28. $this->dao = $memberRightDao;
  29. }
  30. /**
  31. * @param array $where
  32. * @return array
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function getSearchList(array $where = [])
  38. {
  39. [$page, $limit] = $this->getPageValue();
  40. $list = $this->dao->getSearchList($where, $page, $limit);
  41. foreach ($list as &$item) {
  42. $item['image'] = set_file_url($item['image']);
  43. }
  44. $count = $this->dao->count($where);
  45. return compact('list', 'count');
  46. }
  47. /**
  48. * 编辑保存
  49. * @param int $id
  50. * @param array $data
  51. */
  52. public function save(int $id, array $data)
  53. {
  54. if (!$data['right_type']) throw new AdminException("会员权益类型缺失");
  55. if (!$id) throw new AdminException("id参数缺失");
  56. if (!$data['title'] || !$data['show_title']) throw new AdminException("请设置权益名称");
  57. if (!$data['image']) throw new AdminException("请上传会员权益图标");
  58. switch ($data['right_type']) {
  59. case "integral":
  60. if (!$data['number']) throw new AdminException("请设置返还积分倍数");
  61. if ($data['number'] < 0) throw new AdminException("返还积分倍数不能为负数");
  62. $save['number'] = abs($data['number']);
  63. break;
  64. case "express" :
  65. if (!$data['number']) throw new AdminException("请设置运费折扣");
  66. if ($data['number'] < 0) throw new AdminException("运费折扣不能为负数");
  67. $save['number'] = abs($data['number']);
  68. break;
  69. case "sign" :
  70. if (!$data['number']) throw new AdminException("请设置签到积分倍数");
  71. if ($data['number'] < 0) throw new AdminException("签到积分倍数不能为负数");
  72. $save['number'] = abs($data['number']);
  73. break;
  74. case "offline" :
  75. if (!$data['number']) throw new AdminException("请设置线下付款折扣");
  76. if ($data['number'] < 0) throw new AdminException("线下付款不能为负数");
  77. $save['number'] = abs($data['number']);
  78. }
  79. $save['show_title'] = $data['show_title'];
  80. $save['image'] = $data['image'];
  81. $save['status'] = $data['status'];
  82. $save['sort'] = $data['sort'];
  83. $this->dao->update($id, $data);
  84. $right = $this->dao->get($id);
  85. if ($right) {
  86. $this->dao->cacheUpdate($right->toArray());
  87. }
  88. return true;
  89. }
  90. /**
  91. * 获取单条信息
  92. * @param array $where
  93. * @param string $field
  94. * @return array|false|\think\Model|null
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function getOne(array $where, string $field = '*')
  100. {
  101. if (!$where) return false;
  102. return $this->dao->getOne($where, $field);
  103. }
  104. /**
  105. * 查看某权益是否开启
  106. * @param $rightType
  107. * @return bool
  108. */
  109. public function getMemberRightStatus($rightType)
  110. {
  111. if (!$rightType) return false;
  112. $status = $this->dao->value(['right_type' => $rightType], 'status');
  113. if ($status) return true;
  114. return false;
  115. }
  116. /**
  117. * 在缓存中查找权益等级
  118. * @param string $rightType
  119. * @return false|mixed
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. * @author 等风来
  124. * @email 136327134@qq.com
  125. * @date 2022/11/10
  126. */
  127. public function getMemberRightCache(string $rightType)
  128. {
  129. $list = $this->dao->cacheList();
  130. if ($list === null || (is_array($list) && !$list)) {
  131. $list = $this->dao->getSearchList(['status' => 1]);
  132. $this->dao->cacheCreate($list);
  133. }
  134. foreach ($list as $item) {
  135. if ($rightType == $item['right_type']) {
  136. return $item;
  137. }
  138. }
  139. return false;
  140. }
  141. }