📄 userserviceimpl.java
字号:
/*
* Created on 2007-1-14
* Last modified on 2007-9-20
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.user;
import java.util.List;
import org.apache.log4j.Logger;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.model.BasicInfoModel;
import com.yeqiangwei.club.service.model.GroupModel;
import com.yeqiangwei.club.service.model.ManageLogModel;
import com.yeqiangwei.club.service.model.UserModel;
import com.yeqiangwei.club.service.security.GroupService;
import com.yeqiangwei.club.service.util.BasicInfoService;
import com.yeqiangwei.club.service.util.CounterService;
import com.yeqiangwei.club.service.util.CountersService;
import com.yeqiangwei.club.cache.Cache;
import com.yeqiangwei.club.cache.CacheFactory;
import com.yeqiangwei.club.cache.CacheKeys;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.UserDAO;
import com.yeqiangwei.club.dao.model.Counter;
import com.yeqiangwei.club.dao.model.Counters;
import com.yeqiangwei.club.dao.model.Register;
import com.yeqiangwei.club.dao.model.User;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.exception.DAOException;
import com.yeqiangwei.club.param.CounterParameter;
import com.yeqiangwei.club.param.UserParameter;
import com.yeqiangwei.club.util.BeanLocator;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.AProfileForm;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.TypeChange;
public class UserServiceImpl implements UserService{
private static final Logger logger = Logger.getLogger(UserServiceImpl.class.getName());
private UserModel userModel;
private Cache userCache = CacheFactory.creator(CacheRegion.USER);
public static void main(String args[]){
com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
UserServiceImpl u = new UserServiceImpl();
User user = u.getUserDAO().findById(1585);
System.out.println(user.getUserName());
user = u.getUserDAO().findById(1585);
System.out.println(user.getUserName());
}
public int updateIsIndexedByUserId(int userId, boolean isIndexed){
if(userId>0){
return this.getUserDAO().updateIsIndexedByUserId(userId,isIndexed);
}else{
return 0;
}
}
public UserModel update(AProfileForm form) throws ClubException {
UserModel user = this.findById(form.getUserId());
if(Validator.isEmpty(user)){
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}
BasicInfoModel basicInfo = this.getBasicInfoService().findOnly();
if(!user.getPassword().equals(form.getPassword())){
String password = StringHelper.encodeString(basicInfo.getPasswordEncrypt(),form.getPassword());
form.setPassword(password);
}
BeanUtils.copyProperties(user,form);
user.setIsIndexed(false);
this.update(user,false);
return user;
}
public void createOrUpdate(UserModel model) throws ClubException {
if(model.getUserId()<=0){
this.create(model);
}else{
this.update(model);
}
}
public void create(UserModel model) throws ClubException {
//统计相关开始
int ymd = TypeChange.stringToInt(FormatDateTime.formatDateTime("yyyyMMdd"));
CountersService countersService = ServiceWrapper.<CountersService>getSingletonInstance(ServiceLocator.COUNTERS);
CounterParameter param = new CounterParameter();
param.setYmd(ymd);
Counters counters = countersService.findOnlyByParameter(param);
if(Validator.isEmpty(counters)){
counters = new Counters();
}
counters.setYmd(ymd);
CounterService counterService = ServiceWrapper.<CounterService>getSingletonInstance(ServiceLocator.COUNTER);
Counter counter = counterService.findOnly();
if(Validator.isEmpty(counter)){
counter = new Counter();
}
if(model.getSex()==1){
counter.setBoys(counter.getBoys()+1);
counters.setBoys(counters.getBoys()+1);
}else{
counter.setGirls(counter.getGirls()+1);
counters.setGirls(counters.getGirls()+1);
}
//统计相关结束
//用户初始化参数开始
GroupModel group = this.getGroupService().findByRegisterDefault();
if(!Validator.isEmpty(group)){//初始化用户等级结束
model.setGroupId(group.getGroupId());
}else{
//this.setMessage(MessageUtils.getMessage("error_reg_group"));
throw new ClubException(MessageUtils.getMessage("error_reg_group")); //无法分配默认用户组
}
BasicInfoModel basicInfo = this.getBasicInfoService().findOnly();
model.setFileUploadMaxSize(basicInfo.getFileUploadUserSize()); //默认空间
model.setFileUploadUseSize(0);
if(basicInfo.getFileUploadIsOpen()){
model.setFileUploadIsOpen((byte)1);
}else{
model.setFileUploadIsOpen((byte)0);
}
model.setNoAllowed(basicInfo.getUserAllowedType());
model.setPassword(StringHelper.encodeString(basicInfo.getPasswordEncrypt()
,model.getPassword()));
if(basicInfo.getUserAllowedType()==2){
RegisterService registerService = ServiceWrapper.<RegisterService>getSingletonInstance(ServiceLocator.REGISTER);
Register register = registerService.findByKeyAndNumber(model.getEmailAddress(), model.getRegisterNumber());
if(Validator.isEmpty(register)){
//this.setMessage(MessageUtils.getMessage("error_reg_number"));
throw new ClubException(MessageUtils.getMessage("error_reg_number"));
}else if(register.getUsed()){
//this.setMessage(MessageUtils.getMessage("error_reg_number"));
throw new ClubException(MessageUtils.getMessage("error_reg_number"));
}else{
model.setNoAllowed((byte)0);
register.setUsed(true);
registerService.update(register);
}
}
//初始化用户等级
this.ruleUtils(model,0); //按积分制度更新参数
//用户初始化参数结束
counterService.createOrUpdate(counter);
countersService.createOrUpdate(counters);
User item = new User();
BeanUtils.copyProperties(item,model); //BO->PO
try {
this.getUserDAO().create(item);
} catch (DAOException e) {
logger.error(e.toString());
throw new ClubException(MessageUtils.getMessage("error_reg_system"));
}
BeanUtils.copyProperties(model,item); //PO->BO
userCache.put(CacheKeys.getKey(model),model);
//this.setMessage(MessageUtils.getMessage("reg",model));
}
private UserModel filter(UserModel model, boolean passwordEncrypt) throws ClubException{
if(Validator.isEmpty(model)||model.getUserId()==0){
throw new ClubException(MessageUtils.getMessage("error_update_noid"));
}
if(passwordEncrypt){
model.setPassword(StringHelper.encodeString(
this.getBasicInfoService().findOnly().getPasswordEncrypt()
,model.getPassword()));
}
return model;
}
public void update(UserModel model, boolean passwordEncrypt) throws ClubException{
this.filter(model,passwordEncrypt);
User item = this.getUserDAO().findById(model.getUserId());
if(Validator.isEmpty(item)){
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}else{
BeanUtils.copyProperties(item,model); //BO->PO
try {
this.getUserDAO().update(item);
} catch (DAOException e) {
logger.error(e.toString());
throw new ClubException(MessageUtils.getMessage("error_reg_system"));
}
}
BeanUtils.copyProperties(model,item); //PO->BO
userCache.put(CacheKeys.getKey(model),model);
}
public void update(UserModel model) throws ClubException {
this.update(model,false);
}
public int delete(UserModel model) throws ClubException {
CounterService counterService = ServiceWrapper.<CounterService>getSingletonInstance(ServiceLocator.COUNTER);
Counter counter = counterService.findOnly();
int c = 0;
if(!Validator.isEmpty(model)){
if(model.getUserId()==0){
model = this.findByUserName(model.getUserName());
}
if(model!=null){
User item = new User();
BeanUtils.copyProperties(item,model);//BO->PO
try {
c = this.getUserDAO().delete(item);
} catch (DAOException e) {
logger.error(e.toString());
throw new ClubException(MessageUtils.getMessage("error_reg_system"));
}
}
if(!Validator.isEmpty(counter)){
if(model.getSex()==1){
counter.setBoys(counter.getBoys()-1);
}
else if(model.getSex()==2){
counter.setGirls(counter.getGirls()-1);
}
counterService.update(counter);
}
}else{
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}
userCache.remove(CacheKeys.getKey(model));
/*
if(c>0){
userCache.remove(CacheKeys.getKey(model));
this.setMessage(MessageUtils.getMessage("success"));
}else{
this.setMessage(MessageUtils.getMessage("error"));
}
*/
return c;
}
public int delete(String[] ids) throws ClubException {
int success = 0;
int error = 0;
int c = 0;
if(ids!=null&&ids.length>0){
for(int i=0; i<ids.length; i++){
int id = TypeChange.stringToInt(ids[i]);
UserModel model = new UserModel();
model.setUserId(id);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -