UserLevel.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\admin\model\user;
  3. use app\admin\model\system\SystemUserLevel;
  4. use crmeb\traits\ModelTrait;
  5. use crmeb\basic\BaseModel;
  6. /**
  7. * 用户管理 model
  8. * Class User
  9. * @package app\admin\model\user
  10. */
  11. class UserLevel extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'user_level';
  23. use ModelTrait;
  24. public static function setWhere($where, $alias = '', $userAlias = 'u.', $model = null)
  25. {
  26. $model = is_null($model) ? new self() : $model;
  27. if ($alias) {
  28. $model = $model->alias($alias);
  29. $alias .= '.';
  30. }
  31. if (isset($where['nickname']) && $where['nickname'] != '') $model = $model->where("{$userAlias}nickanme", $where['nickname']);
  32. if (isset($where['level_id']) && $where['level_id'] != '') $model = $model->where("{$alias}level_id", $where['level_id']);
  33. return $model->where("{$alias}status", 1)->where("{$alias}is_del", 0);
  34. }
  35. /*
  36. * 查询用户vip列表
  37. * @param array $where
  38. * */
  39. public static function getUserVipList($where)
  40. {
  41. $data = self::setWhere($where, 'a')->group('a.uid')->order('grade desc')
  42. ->field('a.*,u.nickname,u.avatar')
  43. ->join('user u', 'a.uid=u.uid')->page((int)$where['page'], (int)$where['limit'])->select();
  44. $data = count($data) ? $data->toArray() : [];
  45. foreach ($data as &$item) {
  46. $info = SystemUserLevel::where('id', $item['level_id'])->find();
  47. if ($info) {
  48. $item['name'] = $info['name'];
  49. $item['icon'] = $info['icon'];
  50. }
  51. $item['is_forever'] = $item['is_forever'] ? '永久会员' : '限时会员';
  52. $item['valid_time'] = $item['is_forever'] ? '永久' : date('Y-m-d H:i:s', $item['valid_time']);
  53. }
  54. $count = self::setWhere($where, 'a')->group('a.level_id')->order('grade desc')->join('user u', 'a.uid=u.uid')->count();
  55. return compact('data', 'count');
  56. }
  57. /*
  58. * 清除会员等级
  59. * @paran int $uid
  60. * @paran boolean
  61. * */
  62. public static function cleanUpLevel($uid)
  63. {
  64. self::rollbackTrans();
  65. $res = false !== self::where('uid', $uid)->update(['is_del' => 1]);
  66. if (UserTaskFinish::be(['uid' => $uid]))
  67. $res = $res && UserTaskFinish::where('uid', $uid)->delete();
  68. if ($res) {
  69. User::where('uid', $uid)->update(['clean_time' => time(), 'level' => 0]);
  70. self::commitTrans();
  71. return true;
  72. } else {
  73. self::rollbackTrans();
  74. return self::setErrorInfo('清除失败');
  75. }
  76. }
  77. }