package com.ejie.ab79.service.inspecciones;
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.inspecciones.DesEstInsDao;
import com.ejie.ab79.model.inspecciones.DesEstIns;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableResponseDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * DesEstInsServiceImpl  
 *  
 */

@Service(value = "desEstInsService")
public class DesEstInsServiceImpl implements DesEstInsService {

	@Autowired
	private DesEstInsDao desEstInsDao;

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

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

	/**
	 * Finds a single row in the DesEstIns table.
	 *
	 * @param desEstIns DesEstIns
	 * @return DesEstIns
	 */
	public DesEstIns find(DesEstIns desEstIns) {
		return (DesEstIns) this.desEstInsDao.find(desEstIns);
	}
	
	/**
	 * Deletes a single row in the DesEstIns table.
	 *
	 * @param desEstIns DesEstIns
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(DesEstIns desEstIns) {
		this.desEstInsDao.remove(desEstIns);
	}

	/**
	 * Finds a list of rows in the DesEstIns table.
	 *
	 * @param desEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
	 * @return List<DesEstIns>
	 */
	public List<DesEstIns> findAll(DesEstIns desEstIns, TableRequestDto tableRequestDto){
		return (List<DesEstIns>) this.desEstInsDao.findAll(desEstIns, tableRequestDto);
	}
    
	/**
	 * Finds rows in the DesEstIns table using like.
	 *
	 * @param desEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
     * @param startsWith Boolean	 
	 * @return List<DesEstIns>
	 */
	public List<DesEstIns> findAllLike(DesEstIns desEstIns, TableRequestDto tableRequestDto, Boolean startsWith){
		return (List<DesEstIns>) this.desEstInsDao.findAllLike(desEstIns, tableRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */
	 
	/**
	 * Removes rows from the DesEstIns table.
	 *
	 * @param filterDesEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith Boolean
	 */	
	public void removeMultiple(DesEstIns filterDesEstIns, TableRequestDto tableRequestDto,  Boolean startsWith){
		this.desEstInsDao.removeMultiple(filterDesEstIns, tableRequestDto, startsWith);
	}
        
	/**
	 * Filter method in the DesEstIns table.
	 *
	 * @param filterDesEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith Boolean
	 * @return TableResponseDto<DesEstIns>
	 */	
	public TableResponseDto< DesEstIns> filter(DesEstIns filterDesEstIns, TableRequestDto tableRequestDto,  Boolean startsWith){
		List<DesEstIns> listaDesEstIns =  this.desEstInsDao.findAllLike(filterDesEstIns, tableRequestDto, false);
		Long recordNum =  this.desEstInsDao.findAllLikeCount(filterDesEstIns != null ? filterDesEstIns: new DesEstIns (),false);
		
		TableResponseDto<DesEstIns> usuarioDto = new TableResponseDto<DesEstIns>(tableRequestDto, recordNum, listaDesEstIns);
		
		if (tableRequestDto.getMultiselection().getSelectedIds()!=null){
			List< TableRowDto< DesEstIns>> reorderSelection = this.desEstInsDao.reorderSelection(filterDesEstIns, 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< DesEstIns>> reorderSeeker = this.desEstInsDao.reorderSelection(filterDesEstIns, tableRequestDto, startsWith);
			usuarioDto.setReorderedSeeker(reorderSeeker);
			usuarioDto.addAdditionalParam("reorderedSeeker", reorderSeeker);
		}
		return usuarioDto;   
	}
    /**
	 * Searches rows in the DesEstIns table.
	 *
	 * @param filterDesEstIns DesEstIns
	 * @param searchDesEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith Boolean
	 * @return List<TableRowDto<DesEstIns>>
	 */	
    public List< TableRowDto< DesEstIns>> search(DesEstIns filterDesEstIns, DesEstIns searchDesEstIns, TableRequestDto tableRequestDto, Boolean startsWith){
		return this.desEstInsDao.search(filterDesEstIns, searchDesEstIns, tableRequestDto, startsWith);
	}
    
    /**
	 * Reorder the selection made in DesEstIns table.
	 *
	 * @param filterDesEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith Boolean
	 * @return Object
	 */	
    public Object reorderSelection(DesEstIns filterDesEstIns, TableRequestDto tableRequestDto, Boolean startsWith){
		return this.desEstInsDao.reorderSelection(filterDesEstIns, tableRequestDto, startsWith);
	}
    
    /*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */
	 
	/**
	 * Finder method in the hierarchical the DesEstIns table.
	 *
	 * @param filterDesEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith Boolean
	 * @return TableResponseDto<JerarquiaDto<DesEstIns>>
	 */	
	public TableResponseDto<JerarquiaDto<DesEstIns>> jerarquia(DesEstIns filterDesEstIns, TableRequestDto tableRequestDto, Boolean startsWith){
		List<JerarquiaDto<DesEstIns>> listaDesEstIns =  this.desEstInsDao.findAllLikeJerarquia(filterDesEstIns, tableRequestDto);
		Long recordNum = this.desEstInsDao.findAllLikeCountJerarquia(filterDesEstIns, tableRequestDto);
		return new TableResponseDto<JerarquiaDto<DesEstIns>>(tableRequestDto, recordNum, listaDesEstIns);
	}
	
	/**
	 * Finder method for siblings rows in the hierarchical the DesEstIns table.
	 *
	 * @param filterDesEstIns DesEstIns
	 * @param tableRequestDto TableRequestDto
	 * @return TableResponseDto<JerarquiaDto<DesEstIns>>
	 */	
	public TableResponseDto<JerarquiaDto<DesEstIns>> jerarquiaChildren(DesEstIns filterDesEstIns, TableRequestDto tableRequestDto){
		TableResponseDto<JerarquiaDto<DesEstIns>> tableResponseDto = new TableResponseDto<JerarquiaDto<DesEstIns>>();
		tableResponseDto.addAdditionalParam(TableResponseDto.CHILDREN, this.desEstInsDao.findAllChild(filterDesEstIns, tableRequestDto));
		return tableResponseDto;
	}
	
	/**
    * Exporta Datos al clipBoard
    *
    */
    public List<DesEstIns> getMultiple(DesEstIns filterDesEstIns, TableRequestDto tableRequestDto,  Boolean startsWith){
		return this.desEstInsDao.getMultiple(filterDesEstIns, tableRequestDto, startsWith);
	}
      
	
	
}

