package com.ejie.ab61.service.plantillas;

import com.ejie.ab61.dao.plantillas.PlantillasDao;
import com.ejie.ab61.model.plantillas.Plantillas;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * PlantillasServiceImpl  
 * 
 *  
 */

@Service(value = "plantillasService")
public class PlantillasServiceImpl implements PlantillasService {

	@Autowired
	private PlantillasDao plantillasDao;

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

	/**
	 * Updates a single row in the Plantillas table.
	 *
	 * @param plantillas
	 *            Plantillas
	 * @return Plantillas
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Plantillas update(Plantillas plantillas) {
		return this.plantillasDao.update(plantillas);
	}

	/**
	 * Finds a single row in the Plantillas table.
	 *
	 * @param plantillas
	 *            Plantillas
	 * @return Plantillas
	 */
	public Plantillas find(Plantillas plantillas) {
		return (Plantillas) this.plantillasDao.find(plantillas);
	}

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

	/**
	 * Finds a list of rows in the Plantillas table.
	 *
	 * @param plantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<Plantillas>
	 */
	public List<Plantillas> findAll(Plantillas plantillas, JQGridRequestDto jqGridRequestDto) {
		return (List<Plantillas>) this.plantillasDao.findAll(plantillas, jqGridRequestDto);
	}

	/**
	 * Finds rows in the Plantillas table using like.
	 *
	 * @param plantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<Plantillas>
	 */
	public List<Plantillas> findAllLike(Plantillas plantillas, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return (List<Plantillas>) this.plantillasDao.findAllLike(plantillas, jqGridRequestDto, startsWith);
	}

	public void updateIdDocumento(Integer idDocumento) {
		this.plantillasDao.updateIdDocumento(idDocumento);

	}

	/**
	 * Obtiene el numero de documentos existentes para el campo.
	 *
	 * @param idCampo
	 *            Integer
	 * @return int
	 */
	public Long obtenerNumDocumentosCampo(Integer idDocumento) {
		return this.plantillasDao.obtenerNumDocumentosCampo(idDocumento);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Plantillas table.
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(Plantillas filterPlantillas, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		this.plantillasDao.removeMultiple(filterPlantillas, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Plantillas table.
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Plantillas>
	 */
	public JQGridResponseDto<Plantillas> filter(Plantillas filterPlantillas, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		List<Plantillas> listaPlantillas = this.plantillasDao.findAllLike(filterPlantillas, jqGridRequestDto, false);
		Long recordNum = this.plantillasDao
				.findAllLikeCount(filterPlantillas != null ? filterPlantillas : new Plantillas(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Plantillas>> reorderSelection = this.plantillasDao.reorderSelection(filterPlantillas,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Plantillas>(jqGridRequestDto, recordNum, listaPlantillas, reorderSelection);
		}
		return new JQGridResponseDto<Plantillas>(jqGridRequestDto, recordNum, listaPlantillas);
	}

	/**
	 * Filter para el mantenimiento de plantillas
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Plantillas>
	 */
	public JQGridResponseDto<Plantillas> filterMantenimientoPlantillas(Plantillas filterPlantillas,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<Plantillas> listaPlantillas = this.plantillasDao.findMantenimientoPlantillas(filterPlantillas,
				jqGridRequestDto, false);
		Long recordNum = this.plantillasDao
				.findAllLikeCountManteniemiento(filterPlantillas != null ? filterPlantillas : new Plantillas(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Plantillas>> reorderSelection = this.plantillasDao.reorderSelection(filterPlantillas,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Plantillas>(jqGridRequestDto, recordNum, listaPlantillas, reorderSelection);
		}
		return new JQGridResponseDto<Plantillas>(jqGridRequestDto, recordNum, listaPlantillas);
	}

	/**
	 * Filter para el mantenimiento de plantillas por direccion
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Plantillas>
	 */
	public JQGridResponseDto<Plantillas> filterMantenimientoPlantillasPorDireccion(Plantillas filterPlantillas,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith, Integer direccion) {
		List<Plantillas> listaPlantillas = this.plantillasDao.findMantenimientoPlantillasPorDireccion(filterPlantillas,
				jqGridRequestDto, false, direccion);
		Long recordNum = this.plantillasDao.findAllLikeCountManteniemientoPorDireccion(
				filterPlantillas != null ? filterPlantillas : new Plantillas(), false, direccion);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Plantillas>> reorderSelection = this.plantillasDao.reorderSelection(filterPlantillas,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Plantillas>(jqGridRequestDto, recordNum, listaPlantillas, reorderSelection);
		}
		return new JQGridResponseDto<Plantillas>(jqGridRequestDto, recordNum, listaPlantillas);
	}

	/**
	 * Searches rows in the Plantillas table.
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param searchPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Plantillas>>
	 */
	public List<TableRowDto<Plantillas>> search(Plantillas filterPlantillas, Plantillas searchPlantillas,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.plantillasDao.search(filterPlantillas, searchPlantillas, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in Plantillas table.
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(Plantillas filterPlantillas, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.plantillasDao.reorderSelection(filterPlantillas, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

	/**
	 * Finder method in the hierarchical the Plantillas table.
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<JerarquiaDto<Plantillas>>
	 */
	public JQGridResponseDto<JerarquiaDto<Plantillas>> jerarquia(Plantillas filterPlantillas,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<JerarquiaDto<Plantillas>> listaPlantillas = this.plantillasDao.findAllLikeJerarquia(filterPlantillas,
				jqGridRequestDto);
		Long recordNum = this.plantillasDao.findAllLikeCountJerarquia(filterPlantillas, jqGridRequestDto);
		return new JQGridResponseDto<JerarquiaDto<Plantillas>>(jqGridRequestDto, recordNum, listaPlantillas);
	}

	/**
	 * Finder method for siblings rows in the hierarchical the Plantillas table.
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return JQGridResponseDto<JerarquiaDto<Plantillas>>
	 */
	public JQGridResponseDto<JerarquiaDto<Plantillas>> jerarquiaChildren(Plantillas filterPlantillas,
			JQGridRequestDto jqGridRequestDto) {
		JQGridResponseDto<JerarquiaDto<Plantillas>> jqGridResponseDto = new JQGridResponseDto<JerarquiaDto<Plantillas>>();
		jqGridResponseDto.addAdditionalParam(JQGridResponseDto.CHILDREN,
				this.plantillasDao.findAllChild(filterPlantillas, jqGridRequestDto));
		return jqGridResponseDto;
	}

}
