UserLevel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\models\user;
  3. use app\models\system\SystemUserLevel;
  4. use app\models\system\SystemUserTask;
  5. use crmeb\basic\BaseModel;
  6. use crmeb\traits\ModelTrait;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Model;
  11. /**
  12. * TODO 会员等级Model
  13. * Class UserLevel
  14. * @package app\models\user
  15. */
  16. class UserLevel extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'user_level';
  28. use ModelTrait;
  29. /**
  30. * 获取用户等级人数
  31. * @param $uids
  32. * @return int
  33. */
  34. public static function setUserLevelCount($uids)
  35. {
  36. $model = new self();
  37. if (is_array($uids)) $model = $model->where('uid', 'in', $uids);
  38. else $model = $model->where('uid', $uids);
  39. return $model->count();
  40. }
  41. /**
  42. * 设置查询初始化条件
  43. * @param string $alias 表别名
  44. * @param null $model 模型实例化对象
  45. * @return UserLevel
  46. */
  47. public static function valiWhere($alias = '', $model = null)
  48. {
  49. $model = is_null($model) ? new self() : $model;
  50. if ($alias) {
  51. $model = $model->alias($alias);
  52. $alias .= '.';
  53. }
  54. return $model->where("{$alias}status", 1)->where("{$alias}is_del", 0);
  55. }
  56. /**
  57. * 设置会员等级
  58. * @param int $uid 用户uid
  59. * @param int $level_id 等级id
  60. * @return UserLevel|bool|Model
  61. * @throws DataNotFoundException
  62. * @throws ModelNotFoundException
  63. * @throws \think\exception\DbException
  64. */
  65. public static function setUserLevel($uid, $level_id)
  66. {
  67. $vipinfo = SystemUserLevel::get($level_id);
  68. if (!$vipinfo) return false;
  69. $userinfo = User::find($uid);
  70. if (!$userinfo) return false;
  71. $add_valid_time = (int)$vipinfo->valid_date * 86400;
  72. $uservipinfo = self::valiWhere()->where('uid', $uid)->where('level_id', $level_id)->find();
  73. //检查是否购买过
  74. if ($uservipinfo) {
  75. $stay = 0;
  76. //剩余时间
  77. if (time() < $uservipinfo->valid_time) $stay = $uservipinfo->valid_time - time();
  78. //如果购买过当前等级的会员过期了.从当前时间开始计算
  79. //过期时效: 剩余时间+当前会员等级时间+当前time
  80. $add_valid_time = $stay + $add_valid_time + time();
  81. $data['is_forever'] = $vipinfo->is_forever;
  82. $data['valid_time'] = $add_valid_time;
  83. User::where('uid', $uid)->update(['level' => $level_id]);
  84. return self::where('uid', $uid)->where('level_id', $level_id)->update($data);
  85. } else {
  86. $data = [
  87. 'is_forever' => $vipinfo->is_forever,
  88. 'status' => 1,
  89. 'is_del' => 0,
  90. 'grade' => $vipinfo->grade,
  91. 'uid' => $uid,
  92. 'add_time' => time(),
  93. 'level_id' => $level_id,
  94. 'discount' => $vipinfo->discount,
  95. ];
  96. if ($data['is_forever'])
  97. $data['valid_time'] = 0;
  98. else
  99. $data['valid_time'] = $add_valid_time + time();
  100. $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  101. $res = self::create($data);
  102. if (!$res) return false;
  103. User::where('uid', $uid)->update(['level' => $level_id]);
  104. return $res;
  105. }
  106. }
  107. /**
  108. * 获取当前用户会员等级返回当前用户等级id
  109. * @param int $uid 用户uid
  110. * @param int $grade 会员id
  111. * @return bool|mixed
  112. * @throws DataNotFoundException
  113. * @throws ModelNotFoundException
  114. * @throws \think\exception\DbException
  115. */
  116. public static function getUserLevel($uid, $grade = 0)
  117. {
  118. $model = self::valiWhere();
  119. if ($grade) $model = $model->where('grade', '<', $grade);
  120. $level = $model->where('uid', $uid)->order('grade desc')->field('level_id,is_forever,valid_time,id,status,grade')->find();
  121. if (!$level) return false;
  122. if ($level->is_forever) return $level->id;
  123. //会员已经过期
  124. if (time() > $level->valid_time) {
  125. if ($level->status == 1) {
  126. $level->status = 0;
  127. $level->save();
  128. }
  129. return self::getUserLevel($uid, $level->grade);
  130. } else
  131. //会员没有过期
  132. return $level->id;
  133. }
  134. /**
  135. * 获取会员详细信息
  136. * @param $id 会员记录id
  137. * @param string $keyName 字段名
  138. * @return array|mixed|string|Model|null
  139. * @throws DataNotFoundException
  140. * @throws ModelNotFoundException
  141. * @throws \think\exception\DbException
  142. */
  143. public static function getUserLevelInfo($id, $keyName = '')
  144. {
  145. $vipinfo = self::valiWhere('a')->where('a.id', $id)->field('l.id,a.add_time,l.discount,a.level_id,l.name,l.money,l.icon,l.is_pay,l.grade,l.group_point_award,l.group_game_award')
  146. ->join('system_user_level l', 'l.id=a.level_id')->find();
  147. if ($keyName) if (isset($vipinfo[$keyName])) return $vipinfo[$keyName]; else return '';
  148. return $vipinfo;
  149. }
  150. /**
  151. * 获取当前用户已成为的vip id
  152. * @param $uid 用户id
  153. * @return array
  154. */
  155. public static function getUserLevelIds($uid)
  156. {
  157. return self::valiWhere()->group('level_id')->where('uid', $uid)->order('grade asc')->column('level_id', 'level_id');
  158. }
  159. /**
  160. * 检查是否能成为会员
  161. * @param int $uid 用户uid
  162. * @param bool $leveNowId
  163. * @return UserLevel|bool|Model
  164. * @throws DataNotFoundException
  165. * @throws ModelNotFoundException
  166. * @throws DbException
  167. */
  168. public static function setLevelComplete($uid, $leveNowId = false)
  169. {
  170. $user = User::where('uid', $uid)->find();
  171. if (!$user) return self::setErrorInfo('没有此用户,无法检测升级会员');
  172. $level = self::getUserLevel($uid);
  173. if ($level === false)
  174. $level_id = 0;
  175. else
  176. $level_id = self::getUserLevelInfo($level, 'level_id');
  177. if (sys_config('level_type', 0) == 0) {
  178. $leveNowId = SystemUserLevel::getNextLevelId($level_id, SystemUserLevel::setWhere()->where('grade', '>', $level ? self::getUserLevelInfo($level, 'grade') : 0)->min('grade'));
  179. if ($leveNowId === 0) return self::setErrorInfo('暂无可升会员');
  180. //查找当前需要升级的会员任务
  181. $taskAll = SystemUserTask::visibleWhere()->where('level_id', $leveNowId)->column('id', 'id');
  182. if (!$taskAll) return true;
  183. // self::startTrans();
  184. $res2 = true;
  185. try {
  186. if ($level === false) {
  187. //没有成为会员的从用户添加的时间开始算起,如果被清理过会员从清理的时间开始算起
  188. $add_time = $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  189. } else {
  190. $add_time = self::getUserLevelInfo($level, 'add_time');
  191. }
  192. //查询并记录任务
  193. foreach ($taskAll as $id) {
  194. $res = SystemUserTask::setTaskFinish($id, $uid, $add_time);
  195. if (!$res) return self::setErrorInfo(SystemUserTask::getErrorInfo());
  196. }
  197. //获取需要成为会员的任务完成度
  198. if (SystemUserTask::getTaskComplete($leveNowId, $uid)) {
  199. //设置任务已使用
  200. $res = SystemUserTask::setTarkStatus($leveNowId, $uid);
  201. if (!$res) return self::setErrorInfo('设置任务状态失败');
  202. //记录会员
  203. $res2 = self::setUserLevel($uid, $leveNowId);
  204. }
  205. // self::commitTrans();
  206. return $res2;
  207. } catch (\Exception $e) {
  208. // self::rollbackTrans();
  209. return self::setErrorInfo($e->getMessage());
  210. }
  211. } else {
  212. $leveNowId = SystemUserLevel::getNextLevelId($level_id, SystemUserLevel::setWhere()->max('grade'));
  213. // self::startTrans();
  214. try {
  215. while (1) {
  216. if ($leveNowId === 0) return self::setErrorInfo('暂无可升会员');
  217. //查找当前需要升级的会员任务
  218. $taskAll = SystemUserTask::visibleWhere()->where('level_id', $leveNowId)->column('id', 'id');
  219. if (!$taskAll) {
  220. $leveNowGrade = SystemUserLevel::where('id', $leveNowId)->value('grade');
  221. $leveNowId = SystemUserLevel::getNextLevelId($level_id, $leveNowGrade - 1);
  222. if ($leveNowId) {
  223. continue;
  224. } else {
  225. return self::setErrorInfo('暂无可升会员');
  226. }
  227. }
  228. $res2 = true;
  229. if ($level === false) {
  230. //没有成为会员的从用户添加的时间开始算起,如果被清理过会员从清理的时间开始算起
  231. $add_time = $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  232. } else {
  233. $add_time = self::getUserLevelInfo($level, 'add_time');
  234. }
  235. //查询并记录任务
  236. foreach ($taskAll as $id) {
  237. $res = SystemUserTask::setTaskFinish($id, $uid, $add_time);
  238. if (!$res) return self::setErrorInfo(SystemUserTask::getErrorInfo());
  239. }
  240. //获取需要成为会员的任务完成度
  241. if (SystemUserTask::getTaskComplete($leveNowId, $uid)) {
  242. //设置任务已使用
  243. $res = SystemUserTask::setTarkStatus($leveNowId, $uid);
  244. if (!$res) return self::setErrorInfo('设置任务状态失败');
  245. //记录会员
  246. $res2 = self::setUserLevel($uid, $leveNowId);
  247. } else {
  248. $leveNowGrade = SystemUserLevel::where('id', $leveNowId)->value('grade');
  249. $leveNowId = SystemUserLevel::getNextLevelId($level_id, $leveNowGrade - 1);
  250. if ($leveNowId) {
  251. continue;
  252. }
  253. }
  254. if ($res2) {
  255. // self::commitTrans();
  256. return $res2;
  257. } else {
  258. // self::rollbackTrans();
  259. return $res2;
  260. }
  261. }
  262. } catch (\Exception $e) {
  263. // self::rollbackTrans();
  264. return self::setErrorInfo($e->getMessage());
  265. }
  266. }
  267. }
  268. }