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.ParcelaDao;
import com.ejie.ab63.model.Parcela;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * Ab6334s00ServiceImpl  
 * 
 *  
 */

@Service(value = "parcelaService")
public class ParcelaServiceImpl implements ParcelaService {

	@Autowired
	private ParcelaDao ab6334s00Dao;

	@Autowired
	private ReloadableResourceBundleMessageSource appMessageSource;

	/**
	 * Inserts a single row in the Ab6334s00 table.
	 *
	 * @param ab6334s00
	 *            Ab6334s00
	 * @return Ab6334s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Parcela save(Parcela ab6334s00) {
		if (null != ab6334s00.getIdParcela() && ab6334s00.getIdParcela() > 0) {
			ab6334s00 = this.update(ab6334s00);
		} else {
			ab6334s00 = this.add(ab6334s00);
		}

		return ab6334s00;
	}

	/**
	 * Inserts a single row in the Ab6334s00 table.
	 *
	 * @param ab6334s00
	 *            Ab6334s00
	 * @return Ab6334s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Parcela add(Parcela ab6334s00) {
		return this.ab6334s00Dao.add(ab6334s00);
	}

	/**
	 * Updates a single row in the Ab6334s00 table.
	 *
	 * @param ab6334s00
	 *            Ab6334s00
	 * @return Ab6334s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Parcela update(Parcela ab6334s00) {
		return this.ab6334s00Dao.update(ab6334s00);
	}

	/**
	 * Finds a single row in the Ab6334s00 table.
	 *
	 * @param ab6334s00
	 *            Ab6334s00
	 * @return Ab6334s00
	 */
	public Parcela find(Parcela ab6334s00) {
		return (Parcela) this.ab6334s00Dao.find(ab6334s00);
	}

	/**
	 * Deletes a single row in the Ab6334s00 table.
	 *
	 * @param ab6334s00
	 *            Ab6334s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(Parcela ab6334s00) {
		this.ab6334s00Dao.remove(ab6334s00);
	}

	/**
	 * Finds a list of rows in the Ab6334s00 table.
	 *
	 * @param ab6334s00
	 *            Ab6334s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<Ab6334s00>
	 */
	public List<Parcela> findAll(Parcela ab6334s00, JQGridRequestDto jqGridRequestDto) {
		return (List<Parcela>) this.ab6334s00Dao.findAll(ab6334s00, jqGridRequestDto);
	}

	/**
	 * Finds rows in the Ab6334s00 table using like.
	 *
	 * @param ab6334s00
	 *            Ab6334s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<Ab6334s00>
	 */
	public List<Parcela> findAllLike(Parcela ab6334s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return (List<Parcela>) this.ab6334s00Dao.findAllLike(ab6334s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Ab6334s00 table.
	 *
	 * @param filterAb6334s00
	 *            Ab6334s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(Parcela filterAb6334s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		this.ab6334s00Dao.removeMultiple(filterAb6334s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Ab6334s00 table.
	 *
	 * @param filterAb6334s00
	 *            Ab6334s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Ab6334s00>
	 */
	public JQGridResponseDto<Parcela> filter(Parcela filterAb6334s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		List<Parcela> listaAb6334s00 = this.ab6334s00Dao.findAllLike(filterAb6334s00, jqGridRequestDto, false);

		// formater de accesibilidad de 1-0 a si-no
		if (listaAb6334s00 != null && !listaAb6334s00.isEmpty()) {

			for (Parcela parcela : listaAb6334s00) {

				if (parcela != null && parcela.getAccesibilidad() != null) {

					if (parcela.getAccesibilidad() > 0) {

						parcela.setDescAccesibilidad(
								appMessageSource.getMessage("afirmativo", null, LocaleContextHolder.getLocale()));

					} else {
						parcela.setDescAccesibilidad(
								appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));
					}
				}
			}
		}

		Long recordNum = this.ab6334s00Dao.findAllLikeCount(filterAb6334s00 != null ? filterAb6334s00 : new Parcela(),
				false);

		for (Parcela parcela : listaAb6334s00) {
			parcela.setTotalPlazas(parcela.getNumPlazas() * parcela.getNumUnidades());
		}

		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Parcela>> reorderSelection = this.ab6334s00Dao.reorderSelection(filterAb6334s00,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Parcela>(jqGridRequestDto, recordNum, listaAb6334s00, reorderSelection);
		}

		return new JQGridResponseDto<Parcela>(jqGridRequestDto, recordNum, listaAb6334s00);
	}

	/**
	 * Searches rows in the Ab6334s00 table.
	 *
	 * @param filterAb6334s00
	 *            Ab6334s00
	 * @param searchAb6334s00
	 *            Ab6334s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Ab6334s00>>
	 */
	public List<TableRowDto<Parcela>> search(Parcela filterAb6334s00, Parcela searchAb6334s00,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.ab6334s00Dao.search(filterAb6334s00, searchAb6334s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in Ab6334s00 table.
	 *
	 * @param filterAb6334s00
	 *            Ab6334s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(Parcela filterAb6334s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.ab6334s00Dao.reorderSelection(filterAb6334s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

	/**
	 * Finder method in the hierarchical the Ab6334s00 table.
	 *
	 * @param filterAb6334s00
	 *            Ab6334s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<JerarquiaDto<Ab6334s00>>
	 */
	public JQGridResponseDto<JerarquiaDto<Parcela>> jerarquia(Parcela filterAb6334s00,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<JerarquiaDto<Parcela>> listaAb6334s00 = this.ab6334s00Dao.findAllLikeJerarquia(filterAb6334s00,
				jqGridRequestDto);
		Long recordNum = this.ab6334s00Dao.findAllLikeCountJerarquia(filterAb6334s00, jqGridRequestDto);
		return new JQGridResponseDto<JerarquiaDto<Parcela>>(jqGridRequestDto, recordNum, listaAb6334s00);
	}

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

	@Override
	public Long getTotalPlazasParcelas(Parcela parcela) {
		List<Parcela> parcelas = this.ab6334s00Dao.findAllLike(parcela, null, false);

		Long total = (long) 0;
		for (Parcela p : parcelas) {
			p.setTotalPlazas(p.getNumPlazas() * p.getNumUnidades());
			total = total + p.getTotalPlazas();
		}

		return total;
	}

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

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

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

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

	@Override
	public void addMasivo(List<Parcela> listaParcelas) {
		for (Parcela parcela : listaParcelas) {
			if (null != parcela.getIdParcela()) {
				ab6334s00Dao.update(parcela);
			} else {
				ab6334s00Dao.add(parcela);
			}
		}
	}

}
