Grade.php 2.3 KB

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