Grade.php 2.6 KB

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