UserLevel.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $res = $res && UserTaskFinish::where('uid', $uid)->delete();
  67. if ($res) {
  68. User::where('uid', $uid)->update(['clean_time' => time()]);
  69. self::commitTrans();
  70. return true;
  71. } else {
  72. self::rollbackTrans();
  73. return self::setErrorInfo('清除失败');
  74. }
  75. }
  76. }