package com.ejie.ab63.service;

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.ab63.dao.SuiteDao;
import com.ejie.ab63.model.EstablecimientoData;
import com.ejie.ab63.model.Suite;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * Ab6331s00ServiceImpl  
 * 
 *  
 */

@Service(value = "suiteService")
public class SuiteServiceImpl implements SuiteService {

	@Autowired
	private SuiteDao ab6331s00Dao;

	@Autowired
	private EstablecimientoDataService establecimientoDataService;

	/**
	 * Inserts a single row in the Ab6331s00 table.
	 *
	 * @param ab6331s00
	 *            Ab6331s00
	 * @return Ab6331s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Suite add(Suite ab6331s00) {
		return this.ab6331s00Dao.add(ab6331s00);
	}

	/**
	 * Updates a single row in the Ab6331s00 table.
	 *
	 * @param ab6331s00
	 *            Ab6331s00
	 * @return Ab6331s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Suite update(Suite ab6331s00) {
		return this.ab6331s00Dao.update(ab6331s00);
	}

	/**
	 * Finds a single row in the Ab6331s00 table.
	 *
	 * @param ab6331s00
	 *            Ab6331s00
	 * @return Ab6331s00
	 */
	public Suite find(Suite ab6331s00) {
		return (Suite) this.ab6331s00Dao.find(ab6331s00);
	}

	/**
	 * Deletes a single row in the Ab6331s00 table.
	 *
	 * @param ab6331s00
	 *            Ab6331s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(Suite ab6331s00, Integer idEstablecimiento) {

		this.ab6331s00Dao.remove(ab6331s00);

		EstablecimientoData establecimientoData = new EstablecimientoData(idEstablecimiento);

		establecimientoData = this.establecimientoDataService.updateTotalesHoteles(establecimientoData);
	}

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Ab6331s00 table.
	 *
	 * @param filterAb6331s00
	 *            Ab6331s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(Suite filterAb6331s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		this.ab6331s00Dao.removeMultiple(filterAb6331s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Ab6331s00 table.
	 *
	 * @param filterAb6331s00
	 *            Ab6331s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Ab6331s00>
	 */
	public JQGridResponseDto<Suite> filter(Suite filterAb6331s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		List<Suite> listaAb6331s00 = this.ab6331s00Dao.findAllLike(filterAb6331s00, jqGridRequestDto, false);

		for (Suite suite : listaAb6331s00) {
			suite.setTotalPlazas(suite.getNumPlazas() * suite.getNumUnidades());
		}
		Long recordNum = this.ab6331s00Dao.findAllLikeCount(filterAb6331s00 != null ? filterAb6331s00 : new Suite(),
				false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Suite>> reorderSelection = this.ab6331s00Dao.reorderSelection(filterAb6331s00,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Suite>(jqGridRequestDto, recordNum, listaAb6331s00, reorderSelection);
		}
		return new JQGridResponseDto<Suite>(jqGridRequestDto, recordNum, listaAb6331s00);
	}

	/**
	 * Searches rows in the Ab6331s00 table.
	 *
	 * @param filterAb6331s00
	 *            Ab6331s00
	 * @param searchAb6331s00
	 *            Ab6331s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Ab6331s00>>
	 */
	public List<TableRowDto<Suite>> search(Suite filterAb6331s00, Suite searchAb6331s00,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.ab6331s00Dao.search(filterAb6331s00, searchAb6331s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in Ab6331s00 table.
	 *
	 * @param filterAb6331s00
	 *            Ab6331s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(Suite filterAb6331s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.ab6331s00Dao.reorderSelection(filterAb6331s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public Suite save(Suite ab6331s00) {

		if (null != ab6331s00.getIdSuite() && ab6331s00.getIdSuite() > 0) {
			ab6331s00 = this.update(ab6331s00);
		} else {
			ab6331s00 = this.add(ab6331s00);
		}

		EstablecimientoData establecimientoData = new EstablecimientoData(
				ab6331s00.getEstablecimientoData().getIdEstablecimiento());

		this.establecimientoDataService.updateTotalesHoteles(establecimientoData);

		return ab6331s00;
	}

	@Override
	public Long getTotalSuitesByIdEstablecimiento(Integer idEstablecimiento) {
		return this.ab6331s00Dao.getTotalSuitesByIdEstablecimiento(idEstablecimiento);
	}

	@Override
	public void addMasivo(List<Suite> listaSuites) {
		for (Suite suite : listaSuites) {
			if (null != suite.getIdSuite()) {
				this.ab6331s00Dao.update(suite);
			} else {
				this.ab6331s00Dao.add(suite);
			}
		}
	}

	@Override
	public Long getTotalUnidadesResumen(Integer idEstablecimiento) {
		return this.ab6331s00Dao.getTotalUnidadesResumen(idEstablecimiento);
	}

	@Override
	public Long getTotalUnidadesDetalle(Integer idEstablecimiento) {
		return this.ab6331s00Dao.getTotalUnidadesDetalle(idEstablecimiento);
	}

	@Override
	public Long getTotalPlazasResumen(Integer idEstablecimiento) {
		return this.ab6331s00Dao.getTotalPlazasResumen(idEstablecimiento);
	}

	@Override
	public Long getTotalPlazasDetalle(Integer idEstablecimiento) {
		return this.ab6331s00Dao.getTotalPlazasDetalle(idEstablecimiento);
	}
}
