Grade.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\model\user;
  10. use app\common\model\user\Grade as GradeModel;
  11. use app\store\model\User as UserModel;
  12. /**
  13. * 用户会员等级模型
  14. * Class Grade
  15. * @package app\store\model\user
  16. */
  17. class Grade extends GradeModel
  18. {
  19. /**
  20. * 获取列表记录
  21. * @return \think\Paginator
  22. * @throws \think\exception\DbException
  23. */
  24. public function getList()
  25. {
  26. return $this->where('is_delete', '=', 0)
  27. ->order(['weight' => 'asc', 'create_time' => 'desc'])
  28. ->paginate(15, false, [
  29. 'query' => request()->request()
  30. ]);
  31. }
  32. /**
  33. * 新增记录
  34. * @param $data
  35. * @return bool
  36. * @throws \Exception
  37. */
  38. public function add($data)
  39. {
  40. if (!$this->validateForm($data)) {
  41. return false;
  42. }
  43. $data['wxapp_id'] = self::$wxapp_id;
  44. return $this->allowField(true)->save($data);
  45. }
  46. /**
  47. * 编辑记录
  48. * @param $data
  49. * @return false|int
  50. */
  51. public function edit($data)
  52. {
  53. if (!$this->validateForm($data, 'edit')) {
  54. return false;
  55. }
  56. return $this->allowField(true)->save($data) !== false;
  57. }
  58. /**
  59. * 软删除
  60. * @return false|int
  61. */
  62. public function setDelete()
  63. {
  64. // 判断该等级下是否存在会员
  65. if (UserModel::checkExistByGradeId($this['grade_id'])) {
  66. $this->error = '该会员等级下存在用户,不允许删除';
  67. return false;
  68. }
  69. return $this->save(['is_delete' => 1]);
  70. }
  71. /**
  72. * 表单验证
  73. * @param $data
  74. * @param string $scene
  75. * @return bool
  76. */
  77. private function validateForm($data, $scene = 'add')
  78. {
  79. if ($scene === 'add') {
  80. // 需要判断等级权重是否已存在
  81. if (self::checkExistByWeight($data['weight'])) {
  82. $this->error = '等级权重已存在';
  83. return false;
  84. }
  85. } elseif ($scene === 'edit') {
  86. // 需要判断等级权重是否已存在
  87. if (self::checkExistByWeight($data['weight'], $this['grade_id'])) {
  88. $this->error = '等级权重已存在';
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. }