⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 votetopicmngimpl.java

📁 JEECMS是JavaEE版网站管理系统(Java Enterprise Edition Content Manage System)的简称。 基于java技术开发
💻 JAVA
字号:
package com.jeecms.auxiliary.manager.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.auxiliary.dao.VoteTopicDao;
import com.jeecms.auxiliary.entity.VoteItem;
import com.jeecms.auxiliary.entity.VoteRecord;
import com.jeecms.auxiliary.entity.VoteTopic;
import com.jeecms.auxiliary.exception.VoteException;
import com.jeecms.auxiliary.manager.VoteRecordMng;
import com.jeecms.auxiliary.manager.VoteTopicMng;
import com.jeecms.core.JeeCoreManagerImpl;
import com.jeecms.core.entity.Member;
import com.ponyjava.common.hibernate3.Updater;
import com.ponyjava.common.util.ComUtils;

@Service
@Transactional
public class VoteTopicMngImpl extends JeeCoreManagerImpl<VoteTopic> implements
		VoteTopicMng {
	public VoteTopic updateTopic(VoteTopic bean, Set<VoteItem> items) {
		VoteTopic topic = findById(bean.getId());
		Set<VoteItem> oItems = topic.getItems();
		Set<VoteItem> rmItems = new HashSet<VoteItem>();
		// 删除
		boolean isContain = false;
		for (VoteItem oit : oItems) {
			for (VoteItem it : items) {
				if (oit.getId().equals(it.getId())) {
					isContain = true;
					break;
				}
			}
			if (!isContain) {
				rmItems.add(oit);
			}
			isContain = false;
		}
		oItems.removeAll(rmItems);
		// 更新或添加
		for (VoteItem it : items) {
			if (it.getId() == null) {
				oItems.add(it);
			} else {
				for (VoteItem oit : oItems) {
					if (it.getId().equals(oit.getId())) {
						merge(it);
					}
				}
			}
		}
		Updater updater = Updater.create(bean);
		updater.include("startTime");
		updater.include("endTime");
		return (VoteTopic) updateByUpdater(updater);
	}

	public VoteTopic getCurrentTopic(Long webId) {
		return getVoteTopicDao().getCurrentTopic(webId);
	}

	public VoteTopic vote(Long topicId, Long[] voteItems, Long memberId,
			String ip, String cookie) throws VoteException {
		VoteTopic topic = findById(topicId);
		if (topic.getDisabled()) {
			throw new VoteException("这个投票主题已经被关闭!");
		}
		if (voteItems == null || voteItems.length <= 0) {
			return topic;
		}
		if (voteItems.length > topic.getMultiSelect()) {
			throw new VoteException("您投票的选项个数大于允许的个数!");
		}
		long now = System.currentTimeMillis();
		Date start = topic.getStartTime();
		if (start != null && now < start.getTime()) {
			throw new VoteException("投票还没有开始!开始时间是:"
					+ ComUtils.dataFormatWhole(start));
		}
		Date end = topic.getEndTime();
		if (end != null && now > end.getTime()) {
			throw new VoteException("投票已经结束!结束时间是:"
					+ ComUtils.dataFormatWhole(end));
		}
		long repeat = topic.getRepeateHour() * 60 * 60 * 1000;
		long vtime = 0;
		if (topic.getRestrictMember()) {
			if (memberId == null) {
				throw new VoteException("这个主题需要登录才能投票,请您先登录!");
			}
			vtime = voteRecordMng.getTimeByMemberId(memberId, topicId);
			if (vtime + repeat > now) {
				throw new VoteException("该主题不能在" + topic.getRepeateHour()
						+ "小时内重复投票。您上次的投票时间是:"
						+ ComUtils.dataFormatWhole(new Date(vtime)));
			}
		}
		if (topic.getRestrictIp() || cookie == null) {
			vtime = voteRecordMng.getTimeByIp(ip, topicId);
			if (vtime + repeat > now) {
				throw new VoteException("该主题不能在" + topic.getRepeateHour()
						+ "小时内重复投票。您上次的投票时间是:"
						+ ComUtils.dataFormatWhole(new Date(vtime)));
			}
		}
		if (topic.getRestrictCookie() && cookie != null) {
			vtime = voteRecordMng.getTimeByCookie(cookie, topicId);
			if (vtime + repeat > now) {
				throw new VoteException("该主题不能在" + topic.getRepeateHour()
						+ "小时内重复投票。您上次的投票时间是:"
						+ ComUtils.dataFormatWhole(new Date(vtime)));
			}
		}
		topic.setTotalCount(topic.getTotalCount() + voteItems.length);
		for (VoteItem vi : topic.getItems()) {
			for (Long itemId : voteItems) {
				if (vi.getId().equals(itemId)) {
					vi.setVoteCount(vi.getVoteCount() + 1);
				}
			}
		}
		VoteRecord record = voteRecordMng.getVoteRecord(ip, cookie, memberId,
				topicId);
		if (record == null) {
			record = new VoteRecord();
			record.setTopic(topic);
		}
		record.setVoteCookie(cookie);
		record.setVoteIp(ip);
		if (memberId != null) {
			record.setMember(new Member(memberId));
		}
		record.setVoteTime(ComUtils.now());
		saveOrUpdate(record);
		return topic;
	}

	@Override
	public Object updateByUpdater(Updater updater) {
		VoteTopic topic = (VoteTopic) super.updateByUpdater(updater);
		return topic;
	}

	@Override
	public VoteTopic save(VoteTopic topic) {
		topic.setTotalCount(0L);
		super.save(topic);
		return topic;
	}

	@Override
	public VoteTopic findById(Serializable id) {
		VoteTopic topic = super.findById(id);
		return topic;
	}

	@Override
	public VoteTopic deleteById(Serializable id) {
		VoteTopic topic = super.deleteById(id);
		return topic;
	}

	@Autowired
	private VoteRecordMng voteRecordMng;

	@Autowired
	public void setVoteTopicDao(VoteTopicDao dao) {
		super.setDao(dao);
	}

	public VoteTopicDao getVoteTopicDao() {
		return (VoteTopicDao) super.getDao();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -