package com.ejie.ab61.service;

import com.ejie.ab61.dao.RelExpInsSinExpDao;
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;

import com.ejie.ab61.model.RelExpInsSinExp;

/**
 * RelExpInsSinExpServiceImpl  
 * 
 *  
 */

@Service(value = "relExpInsSinExpService")
public class RelExpInsSinExpServiceImpl implements RelExpInsSinExpService {

	@Autowired
	private RelExpInsSinExpDao relExpInsSinExpDao;

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

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

	/**
	 * Finds a single row in the RelExpInsSinExp table.
	 *
	 * @param relExpInsSinExp
	 *            RelExpInsSinExp
	 * @return RelExpInsSinExp
	 */
	public RelExpInsSinExp find(RelExpInsSinExp relExpInsSinExp) {
		return (RelExpInsSinExp) this.relExpInsSinExpDao.find(relExpInsSinExp);
	}

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

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

	/**
	 * Searches rows in the RelExpInsSinExp table.
	 *
	 * @param filterRelExpInsSinExp
	 *            RelExpInsSinExp
	 * @param searchRelExpInsSinExp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpInsSinExp>>
	 */
	public List<TableRowDto<RelExpInsSinExp>> search(RelExpInsSinExp filterRelExpInsSinExp,
			RelExpInsSinExp searchRelExpInsSinExp, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.relExpInsSinExpDao.search(filterRelExpInsSinExp, searchRelExpInsSinExp, jqGridRequestDto,
				startsWith);
	}

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

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

	@Override
	public JQGridResponseDto<RelExpInsSinExp> filterInspeccionesRelacionadas(Integer idExpediente,
			JQGridRequestDto jqGridRequestDto) {
		List<RelExpInsSinExp> listaInspeccionesRelacionadas = this.relExpInsSinExpDao.findByIdExpediente(idExpediente,
				jqGridRequestDto);

		Long recordNum = this.relExpInsSinExpDao.findByIdExpedienteCount(idExpediente);

		return new JQGridResponseDto<RelExpInsSinExp>(jqGridRequestDto, recordNum, listaInspeccionesRelacionadas);
	}

}
