package com.ejie.ab79.service.inspecciones;

import com.ejie.ab79.dao.inspecciones.RelExpInsDao;
import com.ejie.ab79.model.inspecciones.Inspecciones;
import com.ejie.ab79.model.inspecciones.RelExpIns;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableResponseDto;
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;

/**
 * RelExpInsServiceImpl  
 * 
 *  
 */

@Service(value = "relExpInsService")
public class RelExpInsServiceImpl implements RelExpInsService {

	@Autowired
	private RelExpInsDao relExpInsDao;

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

	/**
	 * Finds a single row in the RelExpIns table.
	 *
	 * @param relExpIns
	 *            RelExpIns
	 * @return RelExpIns
	 */
	public RelExpIns find(RelExpIns relExpIns) {
		return (RelExpIns) this.relExpInsDao.find(relExpIns);
	}

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

	@Override
	public void removeByInspeccion(Inspecciones inspeccion) {
		this.relExpInsDao.removeByInspeccion(inspeccion);
	}

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

		TableResponseDto<RelExpIns> usuarioDto = new TableResponseDto<RelExpIns>(tableRequestDto, recordNum,
				listaRelExpIns);

		if (tableRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<RelExpIns>> reorderSelection = this.relExpInsDao.reorderSelection(filterRelExpIns,
					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<RelExpIns>> reorderSeeker = this.relExpInsDao.reorderSelection(filterRelExpIns,
					tableRequestDto, startsWith);
			usuarioDto.setReorderedSeeker(reorderSeeker);
			usuarioDto.addAdditionalParam("reorderedSeeker", reorderSeeker);
		}
		return usuarioDto;
	}

	/**
	 * Searches rows in the RelExpIns table.
	 *
	 * @param filterRelExpIns
	 *            RelExpIns
	 * @param searchRelExpIns
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpIns>>
	 */
	public List<TableRowDto<RelExpIns>> search(RelExpIns filterRelExpIns, RelExpIns searchRelExpIns,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		return this.relExpInsDao.search(filterRelExpIns, searchRelExpIns, tableRequestDto, startsWith);
	}

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

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

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

	@Override
	public RelExpIns updateFechaFinInspeccion(RelExpIns relExpIns) {
		return this.relExpInsDao.updateFechaFinInspeccion(relExpIns);
	}

}
