package com.ejie.ab79.service.actuaciones;

import java.util.List;

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

import com.ejie.ab79.dao.actuaciones.RelActDocDao;
import com.ejie.ab79.model.actuaciones.RelActDoc;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableResponseDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * RelActDocServiceImpl  
 * 
 *  
 */

@Service(value = "relActDocService")
public class RelActDocServiceImpl implements RelActDocService {

	@Autowired
	private RelActDocDao relActDocDao;

	/**
	 * Inserts a single row in the RelActDoc table.
	 *
	 * @param relActDoc
	 *            RelActDoc
	 * @return RelActDoc
	 */
	@Transactional(rollbackFor = Throwable.class)
	public RelActDoc add(RelActDoc relActDoc) {
		return this.relActDocDao.add(relActDoc);
	}

	/**
	 * Finds a single row in the RelActDoc table.
	 *
	 * @param relActDoc
	 *            RelActDoc
	 * @return RelActDoc
	 */
	public RelActDoc find(RelActDoc relActDoc) {
		return (RelActDoc) this.relActDocDao.find(relActDoc);
	}

	/**
	 * Deletes a single row in the RelActDoc table.
	 *
	 * @param relActDoc
	 *            RelActDoc
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(RelActDoc relActDoc) {
		this.relActDocDao.remove(relActDoc);
	}

	@Override
	public void removeByIdDocumentoSubido(RelActDoc relActDoc) {
		this.relActDocDao.removeByIdDocumentoSubido(relActDoc);
	}

	/**
	 * Finds a list of rows in the RelActDoc table.
	 *
	 * @param relActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @return List<RelActDoc>
	 */
	public List<RelActDoc> findAll(RelActDoc relActDoc, TableRequestDto tableRequestDto) {
		return (List<RelActDoc>) this.relActDocDao.findAll(relActDoc, tableRequestDto);
	}

	/**
	 * Finds rows in the RelActDoc table using like.
	 *
	 * @param relActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<RelActDoc>
	 */
	public List<RelActDoc> findAllLike(RelActDoc relActDoc, TableRequestDto tableRequestDto, Boolean startsWith) {
		return (List<RelActDoc>) this.relActDocDao.findAllLike(relActDoc, tableRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the RelActDoc table.
	 *
	 * @param filterRelActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(RelActDoc filterRelActDoc, TableRequestDto tableRequestDto, Boolean startsWith) {
		this.relActDocDao.removeMultiple(filterRelActDoc, tableRequestDto, startsWith);
	}

	/**
	 * Filter method in the RelActDoc table.
	 *
	 * @param filterRelActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return TableResponseDto<RelActDoc>
	 */
	public TableResponseDto<RelActDoc> filter(RelActDoc filterRelActDoc, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		List<RelActDoc> listaRelActDoc = this.relActDocDao.findAllLike(filterRelActDoc, tableRequestDto, false);
		Long recordNum = this.relActDocDao.findAllLikeCount(filterRelActDoc != null ? filterRelActDoc : new RelActDoc(),
				false);

		TableResponseDto<RelActDoc> usuarioDto = new TableResponseDto<RelActDoc>(tableRequestDto, recordNum,
				listaRelActDoc);

		if (tableRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<RelActDoc>> reorderSelection = this.relActDocDao.reorderSelection(filterRelActDoc,
					tableRequestDto, startsWith);
			usuarioDto.setReorderedSelection(reorderSelection);
			usuarioDto.addAdditionalParam("reorderedSelection", reorderSelection);
			usuarioDto.addAdditionalParam("selectedAll", tableRequestDto.getMultiselection().getSelectedAll());
		}
		if (tableRequestDto.getSeeker().getSelectedIds() != null) {
			tableRequestDto.setMultiselection(tableRequestDto.getSeeker());
			List<TableRowDto<RelActDoc>> reorderSeeker = this.relActDocDao.reorderSelection(filterRelActDoc,
					tableRequestDto, startsWith);
			usuarioDto.setReorderedSeeker(reorderSeeker);
			usuarioDto.addAdditionalParam("reorderedSeeker", reorderSeeker);
		}
		return usuarioDto;
	}

	/**
	 * Searches rows in the RelActDoc table.
	 *
	 * @param filterRelActDoc
	 *            RelActDoc
	 * @param searchRelActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelActDoc>>
	 */
	public List<TableRowDto<RelActDoc>> search(RelActDoc filterRelActDoc, RelActDoc searchRelActDoc,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		return this.relActDocDao.search(filterRelActDoc, searchRelActDoc, tableRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in RelActDoc table.
	 *
	 * @param filterRelActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(RelActDoc filterRelActDoc, TableRequestDto tableRequestDto, Boolean startsWith) {
		return this.relActDocDao.reorderSelection(filterRelActDoc, tableRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

	/**
	 * Finder method in the hierarchical the RelActDoc table.
	 *
	 * @param filterRelActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return TableResponseDto<JerarquiaDto<RelActDoc>>
	 */
	public TableResponseDto<JerarquiaDto<RelActDoc>> jerarquia(RelActDoc filterRelActDoc,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		List<JerarquiaDto<RelActDoc>> listaRelActDoc = this.relActDocDao.findAllLikeJerarquia(filterRelActDoc,
				tableRequestDto);
		Long recordNum = this.relActDocDao.findAllLikeCountJerarquia(filterRelActDoc, tableRequestDto);
		return new TableResponseDto<JerarquiaDto<RelActDoc>>(tableRequestDto, recordNum, listaRelActDoc);
	}

	/**
	 * Finder method for siblings rows in the hierarchical the RelActDoc table.
	 *
	 * @param filterRelActDoc
	 *            RelActDoc
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @return TableResponseDto<JerarquiaDto<RelActDoc>>
	 */
	public TableResponseDto<JerarquiaDto<RelActDoc>> jerarquiaChildren(RelActDoc filterRelActDoc,
			TableRequestDto tableRequestDto) {
		TableResponseDto<JerarquiaDto<RelActDoc>> tableResponseDto = new TableResponseDto<JerarquiaDto<RelActDoc>>();
		tableResponseDto.addAdditionalParam(TableResponseDto.CHILDREN,
				this.relActDocDao.findAllChild(filterRelActDoc, tableRequestDto));
		return tableResponseDto;
	}

	/**
	 * Exporta Datos al clipBoard
	 *
	 */
	public List<RelActDoc> getMultiple(RelActDoc filterRelActDoc, TableRequestDto tableRequestDto, Boolean startsWith) {
		return this.relActDocDao.getMultiple(filterRelActDoc, tableRequestDto, startsWith);
	}

	/**
	 * 
	 *
	 * @param idInspeccion
	 *            Integer
	 * 
	 * @return Long
	 */
	@Transactional(readOnly = true)
	public Long documentosSinEnviar(Integer idInspeccion) {
		return this.relActDocDao.documentosSinEnviar(idInspeccion);
	}

	/**
	 * Finds a list of rows in the RelActDoc table.
	 *
	 * @param relActDoc
	 *            RelActDoc
	 * 
	 * @return List<RelActDoc>
	 */
	public List<RelActDoc> findAllPropuestaSancion(RelActDoc relActDoc) {
		return this.relActDocDao.findAllPropuestaSancion(relActDoc);
	}

	@Override
	public RelActDoc updateEnviadoProSan(RelActDoc relActDoc) {
		return this.relActDocDao.updateEnviadoProSan(relActDoc);
	}
}
