UserLevel.php 11 KB

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