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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab63.comun.Constantes;
import com.ejie.ab63.dao.AseoDao;
import com.ejie.ab63.model.Aseo;
import com.ejie.ab63.model.TipoAseo;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * Ab6325s00ServiceImpl  
 * 
 *  
 */

@Service(value = "aseoService")
public class AseoServiceImpl implements AseoService {

	@Autowired
	private AseoDao ab6325s00Dao;

	@Autowired
	TipoAseoService tipoAseoService;

	/**
	 * Inserts a single row in the Ab6325s00 table.
	 *
	 * @param ab6325s00
	 *            Ab6325s00
	 * @return Ab6325s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Aseo add(Aseo ab6325s00) {
		return this.ab6325s00Dao.add(ab6325s00);
	}

	/**
	 * Updates a single row in the Ab6325s00 table.
	 *
	 * @param ab6325s00
	 *            Ab6325s00
	 * @return Ab6325s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Aseo update(Aseo ab6325s00) {
		return this.ab6325s00Dao.update(ab6325s00);
	}

	/**
	 * Finds a single row in the Ab6325s00 table.
	 *
	 * @param ab6325s00
	 *            Ab6325s00
	 * @return Ab6325s00
	 */
	public Aseo find(Aseo ab6325s00) {
		return (Aseo) this.ab6325s00Dao.find(ab6325s00);
	}

	/**
	 * Deletes a single row in the Ab6325s00 table.
	 *
	 * @param ab6325s00
	 *            Ab6325s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(Aseo ab6325s00) {
		this.ab6325s00Dao.remove(ab6325s00);
	}

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Ab6325s00 table.
	 *
	 * @param filterAb6325s00
	 *            Ab6325s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(Aseo filterAb6325s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		this.ab6325s00Dao.removeMultiple(filterAb6325s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Ab6325s00 table.
	 *
	 * @param filterAb6325s00
	 *            Ab6325s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Ab6325s00>
	 */
	public JQGridResponseDto<Aseo> filter(Aseo filterAb6325s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<Aseo> listaAb6325s00 = this.ab6325s00Dao.findAllLike(filterAb6325s00, jqGridRequestDto, false);

		for (Aseo aseo : listaAb6325s00) {

			if (null != aseo.getTipoAseo() && !"".equals(aseo.getTipoAseo().getIdTipoAseo())) {

				if (LocaleContextHolder.getLocale().getLanguage().equalsIgnoreCase(Constantes.CASTELLANO)) {
					aseo.setDescTipoAseo(aseo.getTipoAseo().getDescTipoEs());
				} else {
					aseo.setDescTipoAseo(aseo.getTipoAseo().getDescTipoEu());
				}
			}

		}
		Long recordNum = this.ab6325s00Dao.findAllLikeCount(filterAb6325s00 != null ? filterAb6325s00 : new Aseo(),
				false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Aseo>> reorderSelection = this.ab6325s00Dao.reorderSelection(filterAb6325s00,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Aseo>(jqGridRequestDto, recordNum, listaAb6325s00, reorderSelection);
		}
		return new JQGridResponseDto<Aseo>(jqGridRequestDto, recordNum, listaAb6325s00);
	}

	/**
	 * Searches rows in the Ab6325s00 table.
	 *
	 * @param filterAb6325s00
	 *            Ab6325s00
	 * @param searchAb6325s00
	 *            Ab6325s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Ab6325s00>>
	 */
	public List<TableRowDto<Aseo>> search(Aseo filterAb6325s00, Aseo searchAb6325s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return this.ab6325s00Dao.search(filterAb6325s00, searchAb6325s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in Ab6325s00 table.
	 *
	 * @param filterAb6325s00
	 *            Ab6325s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(Aseo filterAb6325s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.ab6325s00Dao.reorderSelection(filterAb6325s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public Aseo save(Aseo ab6325s00) {
		if (null != ab6325s00.getIdAseo() && ab6325s00.getIdAseo() > 0) {
			return this.update(ab6325s00);
		} else {
			return this.add(ab6325s00);
		}
	}

	@Override
	public void addMasivo(List<Aseo> listaAseos) {
		for (Aseo aseo : listaAseos) {
			TipoAseo tipoAseo = new TipoAseo();

			tipoAseo.setDescTipoEs(aseo.getDescTipoAseo());
			List<TipoAseo> listaTipoAseo = tipoAseoService.findAllLike(tipoAseo, null, false);
			if (!listaTipoAseo.isEmpty()) {
				aseo.setTipoAseo(listaTipoAseo.get(0));
			}
			if (null != aseo.getIdAseo()) {
				ab6325s00Dao.update(aseo);
			} else {
				ab6325s00Dao.add(aseo);
			}
		}
	}

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

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

}
