com.quhaodian.user.data.service.impl.UserInfoServiceImpl Maven / Gradle / Ivy
package com.quhaodian.user.data.service.impl;
import com.quhaodian.data.core.Finder;
import com.quhaodian.data.core.Updater;
import com.quhaodian.data.page.Filter;
import com.quhaodian.data.page.Order;
import com.quhaodian.data.page.Page;
import com.quhaodian.data.page.Pageable;
import com.quhaodian.data.utils.FilterUtils;
import com.quhaodian.user.data.dao.UserInfoDao;
import com.quhaodian.user.data.dao.UserLabelDao;
import com.quhaodian.user.data.entity.UserInfo;
import com.quhaodian.user.data.entity.UserRole;
import com.quhaodian.user.data.service.UserInfoService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by imake on 2017年07月20日16:35:49.
*/
@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
private UserInfoDao dao;
private UserLabelDao labelDao;
@Override
@Transactional(readOnly = true)
public UserInfo findById(Long id) {
return dao.findById(id);
}
@Override
@Transactional
public UserInfo save(UserInfo bean) {
dao.save(bean);
return bean;
}
@Override
public UserInfo addLabel(Long user, String... lables) {
UserInfo result = dao.findById(user);
for (String lable : lables) {
if (lable != null) {
result.getLabels().add(labelDao.label(lable.trim()));
}
}
return result;
}
@Override
@Transactional
public UserInfo update(UserInfo bean) {
Updater updater = new Updater(bean);
return dao.updateByUpdater(updater);
}
@Override
@Transactional
public UserInfo deleteById(Long id) {
UserInfo bean = dao.findById(id);
dao.deleteById(id);
return bean;
}
@Override
@Transactional
public UserInfo[] deleteByIds(Long[] ids) {
UserInfo[] beans = new UserInfo[ids.length];
for (int i = 0, len = ids.length; i < len; i++) {
beans[i] = deleteById(ids[i]);
}
return beans;
}
@Autowired
public void setDao(UserInfoDao dao) {
this.dao = dao;
}
@Override
public Page page(Pageable pageable) {
return dao.page(pageable);
}
@Override
public Page page(Pageable pageable, Object search) {
List filters = FilterUtils.getFilters(search);
if (filters != null) {
pageable.getFilters().addAll(filters);
}
return dao.page(pageable);
}
@Override
public List list(int first, Integer size, List filters, List orders) {
return dao.list(first, size, filters, orders);
}
@Override
public UserInfo findByUserName(String username) {
UserInfo result = null;
Finder finder = Finder.create();
finder.append("from UserInfo u where u.username ='" + username + "'");
List us = dao.find(finder);
if (us != null && us.size() > 0) {
result = us.get(0);
}
return result;
}
@Override
public Collection extends String> findAuthorities(Long id) {
List authorities = new ArrayList();
UserInfo admin = dao.findById(id);
if (admin != null) {
for (UserRole role : admin.getRoles()) {
authorities.addAll(role.getAuthorities());
}
}
return authorities;
}
@Override
public void updateUserLogin(UserInfo user) {
user = dao.findById(user.getId());
user.setLastDate(new Date());
Integer times = user.getLoginSize();
if (times == null) {
times = 0;
}
times++;
user.setLoginSize(times);
}
@Override
public UserInfo addRole(Long id, Long roleid) {
UserInfo entity = dao.findById(id);
if (entity != null) {
UserRole bean = new UserRole();
bean.setId(roleid);
entity.getRoles().add(bean);
}
return entity;
}
}