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.DesResInsDao;
import com.ejie.ab79.model.inspecciones.DesResIns;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableResponseDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * DesResInsServiceImpl  
 *  
 */

@Service(value = "desResInsService")
public class DesResInsServiceImpl implements DesResInsService {

	@Autowired
	private DesResInsDao desResInsDao;

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

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

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

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

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

