/*******************************************************************************
 * Copyright 2019 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.log.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.web.AbstractService;
import com.ffii.tbms.log.ScheduledTaskLog;
import com.ffii.tbms.log.dao.ScheduledTaskLogDao;

@Service
public class ScheduledTaskLogService extends AbstractService {

	@Autowired
	private JdbcDao jdbcDao;

	@Autowired
	private ScheduledTaskLogDao scheduledTaskLogDao;

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

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

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

		StringBuilder sql = new StringBuilder("SELECT"
				+ " *"
				+ " FROM scheduled_task_log"
				+ " WHERE deleted = 0");

		handleSearchArgs(sql, args);

		sql.append(" ORDER BY startDateTime 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.READ_COMMITTED, rollbackFor = Exception.class, readOnly = true)
	public Map<String, Object> searchTotalCount(Map<String, Object> args) {

		StringBuilder sql = new StringBuilder("SELECT"
				+ " COUNT(*) AS count"
				+ " FROM scheduled_task_log"
				+ " WHERE deleted = 0");

		handleSearchArgs(sql, args);

		return jdbcDao.queryForMap(sql.toString(), args);
	}

	public void handleSearchArgs(StringBuilder sql, Map<String, Object> args) {
		if (args != null) {
			if (args.containsKey("module")) sql.append(" AND module = :module");
			if (args.containsKey("task")) sql.append(" AND task LIKE :task");
			if (args.containsKey("dateFrom")) sql.append(" AND startDateTime >= :dateFrom");
			if (args.containsKey("dateTo")) sql.append(" AND startDateTime < :dateTo");
			if (args.containsKey("result")) sql.append(" AND result = :result");
		}
	}
}