package com.ejie.ab63.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab63.dao.SalonComedorDao;
import com.ejie.ab63.model.EstablecimientoData;
import com.ejie.ab63.model.SalonComedor;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * Ab6327s00ServiceImpl  
 * 
 *  
 */

@Service(value = "salonComedorService")
public class SalonComedorServiceImpl implements SalonComedorService {

	@Autowired
	private ReloadableResourceBundleMessageSource appMessageSource;

	@Autowired
	private SalonComedorDao ab6327s00Dao;

	@Autowired
	EstablecimientoDataService establecimientoDataService;

	/**
	 * Inserts a single row in the Ab6327s00 table.
	 *
	 * @param ab6327s00
	 *            Ab6327s00
	 * @return Ab6327s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public SalonComedor add(SalonComedor ab6327s00) {
		return this.ab6327s00Dao.add(ab6327s00);
	}

	/**
	 * Updates a single row in the Ab6327s00 table.
	 *
	 * @param ab6327s00
	 *            Ab6327s00
	 * @return Ab6327s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public SalonComedor update(SalonComedor ab6327s00) {
		return this.ab6327s00Dao.update(ab6327s00);
	}

	/**
	 * Finds a single row in the Ab6327s00 table.
	 *
	 * @param ab6327s00
	 *            Ab6327s00
	 * @return Ab6327s00
	 */
	public SalonComedor find(SalonComedor ab6327s00) {
		return (SalonComedor) this.ab6327s00Dao.find(ab6327s00);
	}

	/**
	 * Deletes a single row in the Ab6327s00 table.
	 *
	 * @param ab6327s00
	 *            Ab6327s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(SalonComedor ab6327s00, Integer idEstablecimiento) {
		this.ab6327s00Dao.remove(ab6327s00);
		EstablecimientoData establecimientoData = new EstablecimientoData(
				ab6327s00.getEstablecimientoData().getIdEstablecimiento());

		this.establecimientoDataService.updateTotalesHoteles(establecimientoData);
	}

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Ab6327s00 table.
	 *
	 * @param filterAb6327s00
	 *            Ab6327s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(SalonComedor filterAb6327s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		this.ab6327s00Dao.removeMultiple(filterAb6327s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Ab6327s00 table.
	 *
	 * @param filterAb6327s00
	 *            Ab6327s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Ab6327s00>
	 */
	public JQGridResponseDto<SalonComedor> filter(SalonComedor filterAb6327s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		List<SalonComedor> listaAb6327s00 = this.ab6327s00Dao.findAllLike(filterAb6327s00, jqGridRequestDto, false);

		for (SalonComedor salonComedor : listaAb6327s00) {

			if (null != salonComedor.getIdTipo() && !"".equals(salonComedor.getIdTipo())) {

				if ("A".equals(salonComedor.getIdTipo())) {
					salonComedor
							.setDescTipo(appMessageSource.getMessage("comedor", null, LocaleContextHolder.getLocale()));
				} else if ("B".equals(salonComedor.getIdTipo())) {
					salonComedor
							.setDescTipo(appMessageSource.getMessage("salon", null, LocaleContextHolder.getLocale()));
				} else if ("C".equals(salonComedor.getIdTipo())) {
					salonComedor.setDescTipo(
							appMessageSource.getMessage("salonComedor", null, LocaleContextHolder.getLocale()));
				} else {
					salonComedor
							.setDescTipo(appMessageSource.getMessage("cocina", null, LocaleContextHolder.getLocale()));
				}
			}

		}

		Long recordNum = this.ab6327s00Dao
				.findAllLikeCount(filterAb6327s00 != null ? filterAb6327s00 : new SalonComedor(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<SalonComedor>> reorderSelection = this.ab6327s00Dao.reorderSelection(filterAb6327s00,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<SalonComedor>(jqGridRequestDto, recordNum, listaAb6327s00, reorderSelection);
		}
		return new JQGridResponseDto<SalonComedor>(jqGridRequestDto, recordNum, listaAb6327s00);
	}

	/**
	 * Searches rows in the Ab6327s00 table.
	 *
	 * @param filterAb6327s00
	 *            Ab6327s00
	 * @param searchAb6327s00
	 *            Ab6327s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Ab6327s00>>
	 */
	public List<TableRowDto<SalonComedor>> search(SalonComedor filterAb6327s00, SalonComedor searchAb6327s00,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.ab6327s00Dao.search(filterAb6327s00, searchAb6327s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in Ab6327s00 table.
	 *
	 * @param filterAb6327s00
	 *            Ab6327s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(SalonComedor filterAb6327s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return this.ab6327s00Dao.reorderSelection(filterAb6327s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public SalonComedor save(SalonComedor ab6327s00) {
		if (null != ab6327s00.getIdSalonComedor() && ab6327s00.getIdSalonComedor() > 0) {
			ab6327s00 = this.update(ab6327s00);
		} else {
			ab6327s00 = this.add(ab6327s00);
		}

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

		this.establecimientoDataService.updateTotalesHoteles(establecimientoData);

		return ab6327s00;
	}

	@Override
	public Long getTotalSalonesByIdEst(Integer idEstablecimiento) {
		return this.ab6327s00Dao.getTotalSalonesByIdEst(idEstablecimiento);
	}

	@Override
	public Long getTotalSalonesComedorByIdEst(Integer idEstablecimiento) {
		return this.ab6327s00Dao.getTotalSalonesComedorByIdEst(idEstablecimiento);
	}

	@Override
	public Long getTotalComedoresByIdEst(Integer idEstablecimiento) {
		return this.ab6327s00Dao.getTotalComedoresByIdEst(idEstablecimiento);
	}

}
