UserLevel.php 7.9 KB

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