| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace app\extra\service\user;
- use app\extra\basic\Service;
- use app\extra\dao\user\UserLevelDao;
- use app\extra\service\cutter\CutterOrderService;
- use support\exception\BusinessException;
- use think\exception\ValidateException;
- class UserLevelService extends Service
- {
- /**
- * 设置模型
- * @return string
- */
- public function __construct()
- {
- $this->dao = new UserLevelDao();
- }
- /**
- * 列表
- * @param array $param
- */
- public function getList(array $param = [],string $field = '*', int $page = 0,int $limit = 0,string $order = 'id desc')
- {
- $list = $this->dao->getList($this->searchFilter($param),$field,$page,$limit,$order);
- $count = $this->dao->count($this->searchFilter($param));
- return compact('list','count');
- }
- protected function searchFilter(array $param = []): array
- {
- $filter = [];
- !empty($param['is_show']) && $filter[] = ["is_show", '=', $param['is_show']];
- !empty($param['is_del']) && $filter[] = ["is_del", '=', $param['is_del']];
- return $filter;
- }
- /**
- * 设置用户等级
- * @param $uid 用户uid
- * @param $level_id 等级id
- * @return UserLevel|bool|\think\Model
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function setUserLevel(int $uid, int $level_id, $vipinfo = [])
- {
- /** @var SystemUserLevelServices $systemLevelServices */
- $systemLevelServices = app()->make(SystemUserLevelServices::class);
- if (!$vipinfo) {
- $vipinfo = $systemLevelServices->getLevel($level_id);
- if (!$vipinfo) {
- throw new AdminException('用户等级不存在');
- }
- }
- /** @var $user */
- $user = app()->make(UserServices::class);
- $userinfo = $user->getUserInfo($uid);
- //把之前等级作废
- $this->dao->update(['uid' => $uid], ['status' => 0, 'is_del' => 1]);
- //检查是否购买过
- $uservipinfo = $this->getWhereLevel(['uid' => $uid, 'level_id' => $level_id, 'is_del' => 0]);
- $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
- $data['add_time'] = time();
- if ($uservipinfo) {
- $data['status'] = 1;
- $data['is_del'] = 0;
- if (!$this->dao->update(['id' => $uservipinfo['id']], $data))
- throw new AdminException('修改用户等级信息失败');
- } else {
- $data = array_merge($data, [
- 'is_forever' => $vipinfo->is_forever,
- 'status' => 1,
- 'is_del' => 0,
- 'grade' => $vipinfo->grade,
- 'uid' => $uid,
- 'level_id' => $level_id,
- 'discount' => $vipinfo->discount,
- ]);
- $data['valid_time'] = 0;
- if (!$this->dao->save($data)) throw new AdminException('写入用户等级信息失败');
- }
- if ($level_id > $userinfo['level']) {
- $change_exp = $vipinfo['exp_num'] - $userinfo['exp'];
- $change_exp = $change_exp<0?0:$change_exp;
- $pm = 1;
- $type = 'system_exp_add';
- $title = '系统增加经验';
- $mark = '系统增加' . $change_exp . '经验';
- } else {
- $change_exp = $userinfo['exp'] - $vipinfo['exp_num'];
- $change_exp = $change_exp<0?0:$change_exp;
- $pm = 0;
- $type = 'system_exp_sub';
- $title = '系统减少经验';
- $mark = '系统减少' . $change_exp . '经验';
- }
- $bill_data['uid'] = $uid;
- $bill_data['pm'] = $pm;
- $bill_data['title'] = $title;
- $bill_data['category'] = 'exp';
- $bill_data['type'] = $type;
- $bill_data['number'] = $change_exp;
- $bill_data['balance'] = $userinfo['exp'];
- $bill_data['mark'] = $mark;
- $bill_data['status'] = 1;
- $bill_data['add_time'] = time();
- /** @var UserBillServices $userBillService */
- $userBillService = app()->make(UserBillServices::class);
- if (!$userBillService->save($bill_data)) throw new AdminException('增加记录失败');
- if (!$user->update(['uid' => $uid], ['level' => $level_id, 'exp' => $vipinfo['exp_num']])) throw new AdminException('修改用户用户等级失败');
- return true;
- }
- /**
- * 检测用户会员升级
- * @param $uid
- * @return bool
- */
- public function detection(int $uid)
- {
- $userService = new UserService();
- $userLevelLogService = new UserLevelLogService();
- $user = $userService->getOne(['uid'=>$uid]);
- if (!$user) {
- throw new ValidateException('没有此用户,无法检测升级用户等级');
- }
- $userAllLevel = $this->selectWhere([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']]]);
- if ($userAllLevel->isEmpty()) {
- $userLevelLogService->update([['uid'
- ,'=',$uid],['grade','>',0]], ['status' =>'0','is_del'=>1]);
- return true;
- }
- $userAllLevel = $userAllLevel->toArray();
- $data = [];
- $userLevel = $userLevelLogService->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
- foreach ($userAllLevel as $vipinfo) {
- if (in_array($vipinfo['id'], $userLevel)) {
- continue;
- }
- $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
- $uservip = $userLevelLogService->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
- if (!$uservip->isEmpty()) {
- //降级在升级情况
- $data['status'] = 1;
- $data['is_del'] = 0;
- if (!$userLevelLogService->update($uservip['id'], $data, 'id')) {
- throw new ValidateException('检测升级失败2');
- }
- } else {
- $data = array_merge($data, [
- 'status' => 1,
- 'is_del' => 0,
- 'grade' => $vipinfo['grade'],
- 'uid' => $uid,
- 'level_id' => $vipinfo['id'],
- ]);
- if (!$userLevelLogService->save($data)) {
- throw new ValidateException('检测升级失败3');
- }
- }
- }
- $userLevelLogService->update([['uid','=',$uid],['grade','<>',end($userAllLevel)['id']??0]], ['status' =>'0']);
- $userLevelLogService->update([['uid','=',$uid],['grade','>',end($userAllLevel)['id']??0]], ['status' =>'0','is_del' => 1]);
- if (!$userService->update($uid, ['level' => end($userAllLevel)['id']], 'uid')) {
- throw new ValidateException('检测升级失败4');
- }
- return true;
- }
- }
|