ChatLogService.java 3.2 KB
package com.w1hd.zzhnc.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.testng.collections.Maps;
import org.testng.internal.annotations.Sets;

import com.beust.jcommander.internal.Lists;
import com.w1hd.zzhnc.dao.ChatlogDao;
import com.w1hd.zzhnc.dao.FansDao;
import com.w1hd.zzhnc.model.Chatlog;
import com.w1hd.zzhnc.util.PageResults;
import com.w1hd.zzhnc.vo.ChatLog_DistinctFansIdVO;
import com.w1hd.zzhnc.vo.Chatlog_Vo;
import com.w1hd.zzhnc.vo.Vo_msg;

import tk.mybatis.mapper.entity.Example;
@Service
public class ChatLogService {

	@Autowired
	ChatlogDao chatlogDao;

	@Autowired
	FansDao fansDao;
	
	
	public PageResults<Chatlog_Vo> getChatLogList(Integer page, Integer replytype, String date1, String date2,
			String keyword) {
		List<Chatlog_Vo> list = chatlogDao.getChatlogList((page - 1) * 10, 10, replytype, date1, date2, keyword);
		Integer total = chatlogDao.getChatlogCount(replytype, date1, date2, keyword);
		PageResults<Chatlog_Vo> pageResults = new PageResults<>();
		pageResults.setPage(page);
		pageResults.setPageSize(10);
		pageResults.setRows(list);
		pageResults.setTotal(total);
		return pageResults;
	}

	
	public int insertChatLog(Chatlog log) {
		log.setId(null);
		return chatlogDao.insertSelective(log);
	}

	
	public void updateChatLogUnReaded(Integer... ids) {
		Example example = new Example(Chatlog.class);
		List<Integer> asList = Arrays.asList(ids);
		example.createCriteria().andIn("id", asList);
		Chatlog log = new Chatlog();
		log.setReaded(true);
		log.setReadTime(new Date());
		chatlogDao.updateByExampleSelective(log, example);
	}

	public Vo_msg getChatListDistinctFansIdIn(int page,int pageRow,Integer salesId) {
		List<ChatLog_DistinctFansIdVO> data = Lists.newArrayList();
		data = fansDao.getFansListForChat(page,pageRow,salesId);
		return new Vo_msg(0, data);
	}

	
	public Vo_msg getChatLogListBySalesId(Integer fromId, Integer fansid, int page,int pageSize) {
		Set<Integer> fansIds = null;

		if (fansid == null || fansid <= 0) {
			return getChatListDistinctFansIdIn((page-1)*pageSize,pageSize,fromId);
		} else {
			fansIds = Sets.newHashSet();
			fansIds.add(fansid);
		}

		Map<String, Object> result = Maps.newHashMap();
		List<Integer> ids =Lists.newArrayList();
		ids.add(fromId);
		List<Chatlog_Vo> chatLogs = Lists.newArrayList();
		for (Integer integer : fansIds) {
			List<Chatlog_Vo> logListBySalesId = chatlogDao.getChatLogListBySalesId((page - 1) * pageSize, pageSize, ids,
					integer);
			Integer chatlogCountBySalesId = chatlogDao.getChatlogCountBySalesId(fromId, integer);
			if (!logListBySalesId.isEmpty()) {
				chatLogs.addAll(logListBySalesId);
				result.put("data", chatLogs);
				result.put("count", chatlogCountBySalesId);
			}
		}

		return new Vo_msg(0, result);

	}

	
	public void updateChatLogUnReaded(Integer salesId, Integer fansid) {
		Example example = new Example(Chatlog.class);
		example.createCriteria().andEqualTo("fansid", fansid).andEqualTo("readed",
				false);
		Chatlog log = new Chatlog();
		log.setReaded(true);
		log.setReadTime(new Date());
		chatlogDao.updateByExampleSelective(log, example);
	}

}