123
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.ruoyi.cai.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ruoyi.cai.domain.Adolescent;
|
||||
import com.ruoyi.cai.dto.app.query.user.AdolescentSetting;
|
||||
import com.ruoyi.cai.dto.app.query.user.AdolescentSwitch;
|
||||
|
||||
/**
|
||||
* 青少年模式Service接口
|
||||
*
|
||||
* @author 77
|
||||
* @date 2024-01-23
|
||||
*/
|
||||
public interface AdolescentService extends IService<Adolescent> {
|
||||
void setAdolescent(AdolescentSetting setting);
|
||||
|
||||
Adolescent getByUserId(Long userId);
|
||||
|
||||
void switchAdolescent(AdolescentSwitch adolescentSwitch);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.ruoyi.cai.service.impl;
|
||||
|
||||
import cn.dev33.satoken.secure.BCrypt;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.cai.domain.Adolescent;
|
||||
import com.ruoyi.cai.dto.app.query.user.AdolescentSetting;
|
||||
import com.ruoyi.cai.dto.app.query.user.AdolescentSwitch;
|
||||
import com.ruoyi.cai.mapper.AdolescentMapper;
|
||||
import com.ruoyi.cai.service.AdolescentService;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.helper.LoginHelper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 青少年模式Service业务层处理
|
||||
*
|
||||
* @author 77
|
||||
* @date 2024-01-23
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class AdolescentServiceImpl extends ServiceImpl<AdolescentMapper,Adolescent> implements AdolescentService {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void setAdolescent(AdolescentSetting setting) {
|
||||
if(StringUtils.isEmpty(setting.getPassword())){
|
||||
throw new ServiceException("密码不能为空");
|
||||
}
|
||||
Long userId = LoginHelper.getUserId();
|
||||
ReentrantLock lock = new ReentrantLock();
|
||||
try {
|
||||
lock.lock();
|
||||
Adolescent adolescent = this.getByUserId(userId);
|
||||
if(adolescent == null){
|
||||
adolescent = new Adolescent();
|
||||
}
|
||||
adolescent.setOpen(1);
|
||||
adolescent.setPassword(BCrypt.hashpw(setting.getPassword()));
|
||||
this.saveOrUpdate(adolescent);
|
||||
}finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Adolescent getByUserId(Long userId){
|
||||
return this.getOne(Wrappers.lambdaQuery(Adolescent.class).eq(Adolescent::getUserId,userId).last("limit 1"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void switchAdolescent(AdolescentSwitch adolescentSwitch) {
|
||||
if(adolescentSwitch.getIsOpen() == null) {
|
||||
throw new ServiceException("参数不正确");
|
||||
}else if(adolescentSwitch.getIsOpen() != 0 && adolescentSwitch.getIsOpen() != 1){
|
||||
throw new ServiceException("参数不正确");
|
||||
}
|
||||
Adolescent adolescent = this.getByUserId(LoginHelper.getUserId());
|
||||
String hashpw = BCrypt.hashpw(adolescentSwitch.getPassword());
|
||||
if(adolescent == null || !adolescent.getPassword().equals(hashpw)){
|
||||
throw new ServiceException("密码错误");
|
||||
}
|
||||
if(adolescentSwitch.getIsOpen() == 0){
|
||||
this.removeById(adolescent);
|
||||
}else {
|
||||
adolescent.setOpen(1);
|
||||
this.updateById(adolescent);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user