Grade.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model\user;
  13. use app\common\model\user\Grade as GradeModel;
  14. use app\store\model\User as UserModel;
  15. /**
  16. * 用户会员等级模型
  17. * Class Grade
  18. * @package app\store\model\user
  19. */
  20. class Grade extends GradeModel
  21. {
  22. // 表单验证场景: 新增
  23. const FORM_SCENE_ADD = 'add';
  24. // 表单验证场景: 编辑
  25. const FORM_SCENE_EDIT = 'edit';
  26. /**
  27. * 获取全部记录
  28. * @param array $param
  29. * @return \think\Collection
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function getAll(array $param)
  35. {
  36. return $this->where($this->getFilter($param))
  37. ->where('is_delete', '=', 0)
  38. ->order(['weight', $this->getPk()])
  39. ->select();
  40. }
  41. /**
  42. * 获取列表记录
  43. * @param array $param
  44. * @return \think\Paginator
  45. * @throws \think\db\exception\DbException
  46. */
  47. public function getList(array $param)
  48. {
  49. return $this->where($this->getFilter($param))
  50. ->where('is_delete', '=', 0)
  51. ->order(['weight', $this->getPk()])
  52. ->paginate();
  53. }
  54. /**
  55. * 获取查询条件
  56. * @param array $param
  57. * @return array
  58. */
  59. private function getFilter(array $param)
  60. {
  61. // 默认查询条件
  62. $params = $this->setQueryDefaultValue($param, [
  63. 'status' => -1 // 状态(1启用 0禁用 -1全部)
  64. ]);
  65. // 检索查询条件
  66. $filter = $params['status'] > -1 ? ['status' => (int)$params['status']] : [];
  67. return $filter;
  68. }
  69. /**
  70. * 新增记录
  71. * @param $data
  72. * @return bool
  73. * @throws \Exception
  74. */
  75. public function add(array $data)
  76. {
  77. if (!$this->validateForm($data, self::FORM_SCENE_ADD)) {
  78. return false;
  79. }
  80. $data['store_id'] = self::$storeId;
  81. return $this->save($data);
  82. }
  83. /**
  84. * 编辑记录
  85. * @param $data
  86. * @return false|int
  87. */
  88. public function edit(array $data)
  89. {
  90. if (!$this->validateForm($data, self::FORM_SCENE_EDIT)) {
  91. return false;
  92. }
  93. return $this->save($data) !== false;
  94. }
  95. /**
  96. * 软删除
  97. * @return false|int
  98. */
  99. public function setDelete()
  100. {
  101. // 判断该等级下是否存在会员
  102. if (UserModel::checkExistByGradeId((int)$this['grade_id'])) {
  103. $this->error = '该会员等级下存在用户,不允许删除';
  104. return false;
  105. }
  106. return $this->save(['is_delete' => 1]);
  107. }
  108. /**
  109. * 表单验证
  110. * @param $data
  111. * @param string $scene
  112. * @return bool
  113. */
  114. private function validateForm(array $data, string $scene = self::FORM_SCENE_ADD)
  115. {
  116. if ($scene === self::FORM_SCENE_ADD) {
  117. // 需要判断等级权重是否已存在
  118. if (self::checkExistByWeight($data['weight'])) {
  119. $this->error = '等级权重已存在';
  120. return false;
  121. }
  122. } elseif ($scene === self::FORM_SCENE_EDIT) {
  123. // 需要判断等级权重是否已存在
  124. if (self::checkExistByWeight($data['weight'], $this['grade_id'])) {
  125. $this->error = '等级权重已存在';
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. }