Grade.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\model\user;
  3. use think\Hook;
  4. use app\common\model\BaseModel;
  5. /**
  6. * 用户会员等级模型
  7. * Class Grade
  8. * @package app\common\model\user
  9. */
  10. class Grade extends BaseModel
  11. {
  12. protected $name = 'user_grade';
  13. /**
  14. * 订单模型初始化
  15. */
  16. public static function init()
  17. {
  18. parent::init();
  19. // 监听订单处理事件
  20. $static = new static;
  21. Hook::listen('user_grade', $static);
  22. }
  23. /**
  24. * 获取器:升级条件
  25. * @param $json
  26. * @return mixed
  27. */
  28. public function getUpgradeAttr($json)
  29. {
  30. return json_decode($json, true);
  31. }
  32. /**
  33. * 获取器:等级权益
  34. * @param $json
  35. * @return mixed
  36. */
  37. public function getEquityAttr($json)
  38. {
  39. return json_decode($json, true);
  40. }
  41. /**
  42. * 修改器:升级条件
  43. * @param $data
  44. * @return mixed
  45. */
  46. public function setUpgradeAttr($data)
  47. {
  48. return json_encode($data);
  49. }
  50. /**
  51. * 修改器:等级权益
  52. * @param $data
  53. * @return mixed
  54. */
  55. public function setEquityAttr($data)
  56. {
  57. return json_encode($data);
  58. }
  59. /**
  60. * 会员等级详情
  61. * @param $grade_id
  62. * @param array $with
  63. * @return static|null
  64. * @throws \think\exception\DbException
  65. */
  66. public static function detail($grade_id, $with = [])
  67. {
  68. return static::get($grade_id, $with);
  69. }
  70. /**
  71. * 获取可用的会员等级列表
  72. * @param null $wxappId
  73. * @param array $order 排序规则
  74. * @return false|\PDOStatement|string|\think\Collection
  75. */
  76. public static function getUsableList($wxappId = null, $order = ['weight' => 'asc'])
  77. {
  78. $model = new static;
  79. $wxappId = $wxappId ? $wxappId : $model::$wxapp_id;
  80. return $model->where('status', '=', '1')
  81. ->where('is_delete', '=', '0')
  82. ->where('wxapp_id', '=', $wxappId)
  83. ->order($order)
  84. ->select();
  85. }
  86. /**
  87. * 验证等级权重是否存在
  88. * @param int $weight 验证的权重
  89. * @param int $gradeId 自身的等级ID
  90. * @return bool
  91. */
  92. public static function checkExistByWeight($weight, $gradeId = 0)
  93. {
  94. $model = new static;
  95. $gradeId > 0 && $model->where('grade_id', '<>', (int)$gradeId);
  96. return $model->where('weight', '=', (int)$weight)
  97. ->where('is_delete', '=', 0)
  98. ->value('grade_id');
  99. }
  100. }