/*******************************************************************************
 * 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.reports.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.SecurityUtils;
import com.ffii.core.web.AbstractService;

/**
 * @author fung
 */
@Service
public class AuditLogExportService extends AbstractService {

	@Autowired
	private JdbcDao jdbcDao;

	@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"
				+ " al.*,"
				+ " (SELECT name FROM users WHERE id = al.modifiedBy) 'modifiedUser'"
				+ " FROM audit_log al"
				+ " WHERE true");
		if (args != null) {
			if (args.containsKey("tableName")) sql.append(" AND al.tableName = :tableName");
			if (args.containsKey("dateFrom")) sql.append(" AND al.modified >= :dateFrom");
			if (args.containsKey("dateTo")) sql.append(" AND al.modified < :dateTo");
		}

		sql.append(" ORDER BY al.modified DESC");

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