All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.quhaodian.notice.data.service.impl.UserNotificationServiceImpl Maven / Gradle / Ivy

package com.quhaodian.notice.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.notice.data.dao.UserNotificationDao;
import com.quhaodian.notice.data.dao.UserNotificationMemberDao;
import com.quhaodian.notice.data.dao.UserNotificationNumDao;
import com.quhaodian.notice.data.dao.UserNotificationTimeDao;
import com.quhaodian.notice.data.entity.UserNotification;
import com.quhaodian.notice.data.entity.UserNotificationMember;
import com.quhaodian.notice.data.entity.UserNotificationNum;
import com.quhaodian.notice.data.entity.UserNotificationTime;
import com.quhaodian.notice.data.enums.NotificationCategory;
import com.quhaodian.notice.data.enums.NotificationState;
import com.quhaodian.notice.data.service.UserNotificationService;
import com.quhaodian.user.data.dao.UserInfoDao;
import com.quhaodian.user.data.entity.UserInfo;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by imake on 2018年01月02日11:12:13.
 */


@Scope("prototype")
@Service
@Transactional
public class UserNotificationServiceImpl implements UserNotificationService {
  
  private UserNotificationDao dao;
  
  @Autowired
  private UserNotificationMemberDao notificationMemberDao;
  
  @Autowired
  private UserInfoDao userInfoDao;
  
  @Autowired
  UserNotificationTimeDao userNotificationTimeDao;
  
  @Autowired
  UserNotificationMemberDao userNotificationMemberDao;
  
  @Autowired
  UserNotificationNumDao userNotificationNumDao;
  
  
  @Override
  @Transactional
  public void updateNotices(Long user) {
    UserNotificationTime time = userNotificationTimeDao.findByUser(user);
    Finder findern = Finder.create();
    findern.append("from UserNotification u where u.addDate >:adddate and u.category=:category");
    findern.append(" and u.addDate <=:nowDate");
    findern.setParam("nowDate", new Date());
    findern.setParam("adddate", time.getLastDate());
    findern.setParam("category", NotificationCategory.pull);
    findern.append(" order by u.addDate asc ");
    findern.setMaxResults(20);
    List ns = dao.find(findern);
    x(user, time, ns);
    userNotificationTimeDao.update(time);
  }
  
  @Override
  public void updateNoticesByNum(Long user) {
    UserNotificationNum num = userNotificationNumDao.findByUser(user);
    Finder findern = Finder.create();
    findern.append("from UserNotification u where u.id >:mid and u.category=:category");
    findern.setParam("mid", num.getMid());
    findern.setParam("category", NotificationCategory.pull);
    findern.append(" order by u.id asc ");
    findern.setMaxResults(20);
    List ns = dao.find(findern);
    x(user, num, ns);
    userNotificationNumDao.update(num);
  }
  
  private void x(Long user, UserNotificationTime time, List ns) {
    if (ns != null) {
      for (UserNotification userNotification : ns) {
        UserNotificationMember notificationMember = new UserNotificationMember();
        notificationMember.setNotification(userNotification);
        notificationMember.setState(NotificationState.unread);
        notificationMember.setUser(UserInfo.fromId(user));
        userNotificationMemberDao.add(notificationMember);
        if (time.getLastDate().before(userNotification.getAddDate())) {
          time.setLastDate(userNotification.getAddDate());
        }
      }
    }
  }
  
  private void x(Long user, UserNotificationNum time, List ns) {
    if (ns != null) {
      for (UserNotification userNotification : ns) {
        UserNotificationMember notificationMember = new UserNotificationMember();
        notificationMember.setNotification(userNotification);
        notificationMember.setState(NotificationState.unread);
        notificationMember.setUser(UserInfo.fromId(user));
        userNotificationMemberDao.add(notificationMember);
        if (time.getMid() < userNotification.getId()) {
          time.setMid(userNotification.getId());
        }
      }
    }
  }
  
  @Override
  @Transactional
  public void updateNotices(Long user, Integer catalog) {
    UserNotificationTime time = userNotificationTimeDao.findByUser(user);
    Finder findern = Finder.create();
    findern.append("from UserNotification u where u.addDate >:adddate and u.category=:category and u.catalog.id=:catalog");
    findern.setParam("adddate", time.getLastDate());
    findern.setParam("catalog", catalog);
    findern.setParam("category", NotificationCategory.pull);
    
    findern.append(" order by u.addDate asc ");
    List ns = dao.find(findern);
    x(user, time, ns);
    userNotificationTimeDao.update(time);
  }
  
  
  @Override
  @Transactional(readOnly = true)
  public UserNotification findById(Long id) {
    return dao.findById(id);
  }
  
  
  @Override
  @Transactional
  public UserNotification push(UserNotification bean) {
    bean.setCategory(NotificationCategory.push);
    dao.save(bean);
    
    Pageable pageable = new Pageable();
    pageable.setPageSize(200);
    Page page = userInfoDao.page(pageable);
    xx(bean, page);
    int totalPage = page.getTotalPages();
    for (int i = 2; i <= totalPage; i++) {
      pageable.setPageNumber(i);
      Page pagex = userInfoDao.page(pageable);
      xx(bean, pagex);
    }
    
    return bean;
  }
  
  private void xx(UserNotification bean, Page pagex) {
    List userInfos = pagex.getContent();
    for (UserInfo userInfo : userInfos) {
      UserNotificationMember member = new UserNotificationMember();
      member.setNotification(bean);
      member.setState(NotificationState.unread);
      member.setUser(userInfo);
      notificationMemberDao.save(member);
    }
  }
  
  @Override
  public UserNotification pull(UserNotification bean) {
    bean.setCategory(NotificationCategory.pull);
    dao.save(bean);
    return bean;
  }
  
  @Override
  public UserNotification send(Long user, UserNotification bean) {
    bean.setCategory(NotificationCategory.single);
    dao.save(bean);
    UserNotificationMember member = new UserNotificationMember();
    member.setNotification(bean);
    member.setState(NotificationState.unread);
    member.setUser(UserInfo.fromId(user));
    notificationMemberDao.save(member);
    return bean;
  }
  
  @Override
  public UserNotification send(UserNotification bean, Long... users) {
    return dao.send(bean,users);
  }
  
  @Override
  @Transactional
  public UserNotification update(UserNotification bean) {
    Updater updater = new Updater(bean);
    return dao.updateByUpdater(updater);
  }
  
  @Override
  @Transactional
  public UserNotification deleteById(Long id) {
    UserNotification bean = dao.findById(id);
    dao.deleteById(id);
    return bean;
  }
  
  @Override
  @Transactional
  public UserNotification[] deleteByIds(Long[] ids) {
    UserNotification[] beans = new UserNotification[ids.length];
    for (int i = 0, len = ids.length; i < len; i++) {
      beans[i] = deleteById(ids[i]);
    }
    return beans;
  }
  
  
  @Autowired
  public void setDao(UserNotificationDao 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);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy