UserLevel.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 DbException
  64. */
  65. public static function setUserLevel($uid, $level_id, $mer_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. $data['mer_id'] = $mer_id;
  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);
  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 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 int $id 会员记录id
  138. * @param string $keyName 字段名
  139. * @return array|mixed|string|Model|null
  140. */
  141. public static function getUserLevelInfo($id, $keyName = '')
  142. {
  143. $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')
  144. ->join('system_user_level l', 'l.id=a.level_id')->find();
  145. if ($keyName) if (isset($vipinfo[$keyName])) return $vipinfo[$keyName]; else return '';
  146. return $vipinfo;
  147. }
  148. /**
  149. * 获取当前用户已成为的vip id
  150. * @param int $uid 用户id
  151. * @return array
  152. */
  153. public static function getUserLevelIds($uid)
  154. {
  155. return self::valiWhere()->group('level_id')->where('uid', $uid)->order('grade asc')->column('level_id', 'level_id');
  156. }
  157. /**
  158. * 检查是否能成为会员
  159. * @param int $uid 用户uid
  160. * @param bool $leveNowId
  161. * @return UserLevel|bool|Model
  162. * @throws DataNotFoundException
  163. * @throws ModelNotFoundException
  164. * @throws DbException
  165. */
  166. public static function setLevelComplete($uid, $leveNowId = false, $mer_id = false)
  167. {
  168. $user = User::merSet($mer_id)->where('uid', $uid)->find();
  169. if (!$user) return self::setErrorInfo('没有此用户,无法检测升级会员');
  170. $level = self::getUserLevel($uid);
  171. if ($level === false)
  172. $level_id = 0;
  173. else
  174. $level_id = self::getUserLevelInfo($level, 'level_id');
  175. $leveNowId = SystemUserLevel::getNextLevelId($level_id, $mer_id);
  176. if ($leveNowId === 0) return self::setErrorInfo('暂无可升会员');
  177. //查找当前需要升级的会员任务
  178. $taskAll = SystemUserTask::visibleWhere()->where('level_id', $leveNowId)->column('id', 'id');
  179. self::startTrans();
  180. $res2 = true;
  181. try {
  182. if ($level === false) {
  183. //没有成为会员的从用户添加的时间开始算起,如果被清理过会员从清理的时间开始算起
  184. $add_time = $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  185. } else {
  186. $add_time = self::getUserLevelInfo($level, 'add_time');
  187. }
  188. //查询并记录任务
  189. foreach ($taskAll as $id) {
  190. $res = SystemUserTask::setTaskFinish($id, $uid, $add_time);
  191. if (!$res) return self::setErrorInfo(SystemUserTask::getErrorInfo(), true);
  192. }
  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, $mer_id);
  200. }
  201. self::commitTrans();
  202. return $res2;
  203. } catch (\Exception $e) {
  204. self::rollbackTrans();
  205. return self::setErrorInfo($e->getMessage());
  206. }
  207. }
  208. /**
  209. * 清除会员等级
  210. * @paran int $uid
  211. * @paran boolean
  212. * */
  213. public static function cleanUpLevel($uid)
  214. {
  215. self::rollbackTrans();
  216. $res = false !== self::where('uid', $uid)->update(['is_del' => 1]);
  217. $res = $res && UserTaskFinish::where('uid', $uid)->delete();
  218. if ($res) {
  219. User::where('uid', $uid)->update(['level' => 0, 'clean_time' => time()]);
  220. self::commitTrans();
  221. return true;
  222. } else {
  223. self::rollbackTrans();
  224. return self::setErrorInfo('清除失败');
  225. }
  226. }
  227. }