UserLevelService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\extra\service\user;
  3. use app\extra\basic\Service;
  4. use app\extra\dao\user\UserLevelDao;
  5. use app\extra\service\cutter\CutterOrderService;
  6. use support\exception\BusinessException;
  7. use think\exception\ValidateException;
  8. class UserLevelService extends Service
  9. {
  10. /**
  11. * 设置模型
  12. * @return string
  13. */
  14. public function __construct()
  15. {
  16. $this->dao = new UserLevelDao();
  17. }
  18. /**
  19. * 列表
  20. * @param array $param
  21. */
  22. public function getList(array $param = [],string $field = '*', int $page = 0,int $limit = 0,string $order = 'id desc')
  23. {
  24. $list = $this->dao->getList($this->searchFilter($param),$field,$page,$limit,$order);
  25. $count = $this->dao->count($this->searchFilter($param));
  26. return compact('list','count');
  27. }
  28. protected function searchFilter(array $param = []): array
  29. {
  30. $filter = [];
  31. !empty($param['is_show']) && $filter[] = ["is_show", '=', $param['is_show']];
  32. !empty($param['is_del']) && $filter[] = ["is_del", '=', $param['is_del']];
  33. return $filter;
  34. }
  35. /**
  36. * 设置用户等级
  37. * @param $uid 用户uid
  38. * @param $level_id 等级id
  39. * @return UserLevel|bool|\think\Model
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @throws \think\exception\DbException
  43. */
  44. public function setUserLevel(int $uid, int $level_id, $vipinfo = [])
  45. {
  46. /** @var SystemUserLevelServices $systemLevelServices */
  47. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  48. if (!$vipinfo) {
  49. $vipinfo = $systemLevelServices->getLevel($level_id);
  50. if (!$vipinfo) {
  51. throw new AdminException('用户等级不存在');
  52. }
  53. }
  54. /** @var $user */
  55. $user = app()->make(UserServices::class);
  56. $userinfo = $user->getUserInfo($uid);
  57. //把之前等级作废
  58. $this->dao->update(['uid' => $uid], ['status' => 0, 'is_del' => 1]);
  59. //检查是否购买过
  60. $uservipinfo = $this->getWhereLevel(['uid' => $uid, 'level_id' => $level_id, 'is_del' => 0]);
  61. $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  62. $data['add_time'] = time();
  63. if ($uservipinfo) {
  64. $data['status'] = 1;
  65. $data['is_del'] = 0;
  66. if (!$this->dao->update(['id' => $uservipinfo['id']], $data))
  67. throw new AdminException('修改用户等级信息失败');
  68. } else {
  69. $data = array_merge($data, [
  70. 'is_forever' => $vipinfo->is_forever,
  71. 'status' => 1,
  72. 'is_del' => 0,
  73. 'grade' => $vipinfo->grade,
  74. 'uid' => $uid,
  75. 'level_id' => $level_id,
  76. 'discount' => $vipinfo->discount,
  77. ]);
  78. $data['valid_time'] = 0;
  79. if (!$this->dao->save($data)) throw new AdminException('写入用户等级信息失败');
  80. }
  81. if ($level_id > $userinfo['level']) {
  82. $change_exp = $vipinfo['exp_num'] - $userinfo['exp'];
  83. $change_exp = $change_exp<0?0:$change_exp;
  84. $pm = 1;
  85. $type = 'system_exp_add';
  86. $title = '系统增加经验';
  87. $mark = '系统增加' . $change_exp . '经验';
  88. } else {
  89. $change_exp = $userinfo['exp'] - $vipinfo['exp_num'];
  90. $change_exp = $change_exp<0?0:$change_exp;
  91. $pm = 0;
  92. $type = 'system_exp_sub';
  93. $title = '系统减少经验';
  94. $mark = '系统减少' . $change_exp . '经验';
  95. }
  96. $bill_data['uid'] = $uid;
  97. $bill_data['pm'] = $pm;
  98. $bill_data['title'] = $title;
  99. $bill_data['category'] = 'exp';
  100. $bill_data['type'] = $type;
  101. $bill_data['number'] = $change_exp;
  102. $bill_data['balance'] = $userinfo['exp'];
  103. $bill_data['mark'] = $mark;
  104. $bill_data['status'] = 1;
  105. $bill_data['add_time'] = time();
  106. /** @var UserBillServices $userBillService */
  107. $userBillService = app()->make(UserBillServices::class);
  108. if (!$userBillService->save($bill_data)) throw new AdminException('增加记录失败');
  109. if (!$user->update(['uid' => $uid], ['level' => $level_id, 'exp' => $vipinfo['exp_num']])) throw new AdminException('修改用户用户等级失败');
  110. return true;
  111. }
  112. /**
  113. * 检测用户会员升级
  114. * @param $uid
  115. * @return bool
  116. */
  117. public function detection(int $uid)
  118. {
  119. $userService = new UserService();
  120. $userLevelLogService = new UserLevelLogService();
  121. $user = $userService->getOne(['uid'=>$uid]);
  122. if (!$user) {
  123. throw new ValidateException('没有此用户,无法检测升级用户等级');
  124. }
  125. $userAllLevel = $this->selectWhere([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']]]);
  126. if ($userAllLevel->isEmpty()) {
  127. $userLevelLogService->update([['uid'
  128. ,'=',$uid],['grade','>',0]], ['status' =>'0','is_del'=>1]);
  129. return true;
  130. }
  131. $userAllLevel = $userAllLevel->toArray();
  132. $data = [];
  133. $userLevel = $userLevelLogService->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
  134. foreach ($userAllLevel as $vipinfo) {
  135. if (in_array($vipinfo['id'], $userLevel)) {
  136. continue;
  137. }
  138. $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  139. $uservip = $userLevelLogService->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
  140. if (!$uservip->isEmpty()) {
  141. //降级在升级情况
  142. $data['status'] = 1;
  143. $data['is_del'] = 0;
  144. if (!$userLevelLogService->update($uservip['id'], $data, 'id')) {
  145. throw new ValidateException('检测升级失败2');
  146. }
  147. } else {
  148. $data = array_merge($data, [
  149. 'status' => 1,
  150. 'is_del' => 0,
  151. 'grade' => $vipinfo['grade'],
  152. 'uid' => $uid,
  153. 'level_id' => $vipinfo['id'],
  154. ]);
  155. if (!$userLevelLogService->save($data)) {
  156. throw new ValidateException('检测升级失败3');
  157. }
  158. }
  159. }
  160. $userLevelLogService->update([['uid','=',$uid],['grade','<>',end($userAllLevel)['id']??0]], ['status' =>'0']);
  161. $userLevelLogService->update([['uid','=',$uid],['grade','>',end($userAllLevel)['id']??0]], ['status' =>'0','is_del' => 1]);
  162. if (!$userService->update($uid, ['level' => end($userAllLevel)['id']], 'uid')) {
  163. throw new ValidateException('检测升级失败4');
  164. }
  165. return true;
  166. }
  167. }