/*******************************************************************************
 * Copyright 2Fi Business Solutions Ltd.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.message.service;

import java.util.List;
import java.util.Map;

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

import com.ffii.core.dao.JdbcDao;
import com.ffii.core.utils.BooleanUtils;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.web.AbstractService;
import com.ffii.tbms.message.Message;
import com.ffii.tbms.message.dao.MessageDao;

@Service
public class MessageService extends AbstractService {

	@Autowired
	private JdbcDao jdbcDao;

	@Autowired
	private MessageDao messageDao;

	@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class, readOnly = true)
	public Message find(Integer id) {
		return messageDao.find(id);
	}

	@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = false)
	public Integer saveOrUpdate(Message instance) {
		return messageDao.saveOrUpdate(instance);
	}

	@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = true)
	public List<Map<String, Object>> search(Map<String, Object> args) {
	
		SecurityUtils.authArgs(args);

		StringBuilder sql = new StringBuilder(" SELECT m.* "
		+" FROM message m "
		+" WHERE m.deleted = 0 ");
		if (args != null) {
			if (args.containsKey("unRead") && BooleanUtils.isTrue(args.get("unRead")))
				sql.append(" AND readTime IS NULL ");

			if(args.containsKey("query")) sql.append(" AND m.content LIKE :query ");
			if(args.containsKey("sysGroupId")) sql.append(" AND m.sysGroupId = :sysGroupId ");
		}
		sql.append(" ORDER BY m.created DESC ");
		if (args != null) {
			if (args.containsKey("start") && args.containsKey("limit"))
				sql.append(" LIMIT :start, :limit ");
		}
		return jdbcDao.queryForList(sql.toString(), args);
	}

	@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = true)
	public Map<String, Object> searchCount(Map<String, Object> args) {
		
		SecurityUtils.authArgs(args);

		StringBuilder sql = new StringBuilder(" SELECT count(m.id) AS count "
		+" FROM message m "
		+" WHERE m.deleted = 0 ");
		if (args != null) {
			if (args.containsKey("unRead") && BooleanUtils.isTrue(args.get("unRead")))
				sql.append(" AND readTime IS NULL ");
				
			if(args.containsKey("query")) sql.append(" AND m.content LIKE :query ");
			if(args.containsKey("sysGroupId")) sql.append(" AND m.sysGroupId = :sysGroupId ");
		}
		return jdbcDao.queryForMap(sql.toString(), args);
	}

}
