Grade.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\common\model\user;
  13. use app\common\model\BaseModel;
  14. use app\common\library\helper;
  15. /**
  16. * 用户会员等级模型
  17. * Class Grade
  18. * @package app\common\model\user
  19. */
  20. class Grade extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'user_grade';
  24. // 定义主键
  25. protected $pk = 'grade_id';
  26. /**
  27. * 获取器:升级条件
  28. * @param $json
  29. * @return mixed
  30. */
  31. public function getUpgradeAttr($json)
  32. {
  33. return helper::jsonDecode($json);
  34. }
  35. /**
  36. * 获取器:等级权益
  37. * @param $json
  38. * @return mixed
  39. */
  40. public function getEquityAttr($json)
  41. {
  42. return helper::jsonDecode($json);
  43. }
  44. /**
  45. * 修改器:升级条件
  46. * @param $data
  47. * @return mixed
  48. */
  49. public function setUpgradeAttr($data)
  50. {
  51. // 这里需要转换下, 否则会出现浮点数精度丢失问题
  52. $data['expend_money'] = (string)$data['expend_money'];
  53. return helper::jsonEncode($data);
  54. }
  55. /**
  56. * 修改器:等级权益
  57. * @param $data
  58. * @return mixed
  59. */
  60. public function setEquityAttr($data)
  61. {
  62. // 这里需要转换下, 否则会出现浮点数精度丢失问题
  63. $data['discount'] = (string)$data['discount'];
  64. return helper::jsonEncode($data);
  65. }
  66. /**
  67. * 会员等级详情
  68. * @param int $gradId
  69. * @param array $with
  70. * @return array|null|static
  71. */
  72. public static function detail(int $gradId, array $with = [])
  73. {
  74. return static::get($gradId, $with);
  75. }
  76. /**
  77. * 验证等级权重是否存在
  78. * @param int $weight 验证的权重
  79. * @param int $gradeId 自身的等级ID
  80. * @return bool
  81. */
  82. public static function checkExistByWeight(int $weight, int $gradeId = 0): bool
  83. {
  84. $filter = [];
  85. $gradeId > 0 && $filter[] = ['grade_id', '<>', (int)$gradeId];
  86. return !!(new static)->where('weight', '=', (int)$weight)
  87. ->where($filter)
  88. ->where('is_delete', '=', 0)
  89. ->value('grade_id');
  90. }
  91. }