package com.ejie.ab39a.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab39a.constantes.Constantes;
import com.ejie.ab39a.dao.ElementosArbolDao;
import com.ejie.ab39a.dao.PlanesEstrategicosDao;
import com.ejie.ab39a.model.ElementosPe;
import com.ejie.ab39a.model.Entidades;
import com.ejie.ab39a.model.PlanesEstrategicos;
import com.ejie.ab39a.seguridad.UtilSeguridad;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * PlanesEstrategicos.
 * 
 *  
 */
@Service(value = "planesEstrategicosService")
public class PlanesEstrategicosServiceImpl implements PlanesEstrategicosService {

    @Autowired
    private PlanesEstrategicosDao planesEstrategicosDao;
    @Autowired
    private ElementosArbolDao elementosArbolDao;

    /**
     * Inserts a single row in the PlanesEstrategicos table.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     * @return PlanesEstrategicos
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public PlanesEstrategicos add(PlanesEstrategicos planesEstrategicos) {
        PlanesEstrategicos planEs = this.planesEstrategicosDao.add(planesEstrategicos);
        this.planesEstrategicosDao.generarPlanEstrategico(planEs);

        return planEs;
    }

    /**
     * Updates a single row in the PlanesEstrategicos table.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     * @return PlanesEstrategicos
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public PlanesEstrategicos update(PlanesEstrategicos planesEstrategicos) {
        return this.planesEstrategicosDao.update(planesEstrategicos);
    }

    /**
     * Updates a single row in the PlanesEstrategicos table.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     * @return PlanesEstrategicos
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public PlanesEstrategicos guardarPlanCompleto(PlanesEstrategicos planesEstrategicos, List<ElementosPe> elementos) {
        PlanesEstrategicos plan = this.planesEstrategicosDao.updateDatosPlan(planesEstrategicos);
        this.elementosArbolDao.guardarDatosArbol(elementos);
        return plan;
    }

    /**
     * Finds a single row in the PlanesEstrategicos table.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     * @return PlanesEstrategicos
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public PlanesEstrategicos find(PlanesEstrategicos planesEstrategicos) {
        return this.planesEstrategicosDao.find(planesEstrategicos);
    }

    /**
     * Deletes a single row in the PlanesEstrategicos table.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public void remove(PlanesEstrategicos planesEstrategicos) {
        this.planesEstrategicosDao.remove(planesEstrategicos);
    }

    /**
     * Finds a list of rows in the PlanesEstrategicos table.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     * @param jqGridRequestDto
     *            JQGridRequestDto
     * @return List<PlanesEstrategicos>
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public List<PlanesEstrategicos> findAll(PlanesEstrategicos planesEstrategicos, JQGridRequestDto jqGridRequestDto) {
        return this.planesEstrategicosDao.findAll(planesEstrategicos, jqGridRequestDto);
    }

    /**
     * Finds rows in the PlanesEstrategicos table using like.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     * @param jqGridRequestDto
     *            JQGridRequestDto
     * @param startsWith
     *            Boolean
     * @return List<PlanesEstrategicos>
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public List<PlanesEstrategicos> findAllLike(PlanesEstrategicos planesEstrategicos,
            JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
        String usuarioConectado = null;
        if (UtilSeguridad.getUsuarioConectado().isTecnicoEntidad()) {
            usuarioConectado = Constantes.USUARIO_TEC_ENTIDAD;
            Entidades entidad = UtilSeguridad.getUsuarioConectado().getTecnicosEntidad().getEntidadPrincipal();
            Integer tipoEntidad = entidad.getTipoEntidad();
            planesEstrategicos.setEntidad(new Entidades());
            planesEstrategicos.getEntidad().setTipoEntidad(tipoEntidad);
        } else if (UtilSeguridad.getUsuarioConectado().isTecnicoHps()) {
            usuarioConectado = Constantes.USUARIO_HPS;
        }
        planesEstrategicos.setUsuarioConectado(usuarioConectado);

        return this.planesEstrategicosDao.findAllLike(planesEstrategicos, jqGridRequestDto, startsWith);
    }

    /*
     * OPERACIONES RUP_TABLE
     */

    /**
     * Removes rows from the PlanesEstrategicos table.
     *
     * @param filterPlanesEstrategicos
     *            PlanesEstrategicos
     * @param jqGridRequestDto
     *            JQGridRequestDto
     * @param startsWith
     *            Boolean
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public void removeMultiple(PlanesEstrategicos filterPlanesEstrategicos, JQGridRequestDto jqGridRequestDto,
            Boolean startsWith) {
        this.planesEstrategicosDao.removeMultiple(filterPlanesEstrategicos, jqGridRequestDto, startsWith);
    }

    /**
     * Filter method in the PlanesEstrategicos table.
     *
     * @param filterPlanesEstrategicos
     *            PlanesEstrategicos
     * @param jqGridRequestDto
     *            JQGridRequestDto
     * @param startsWith
     *            Boolean
     * @return JQGridResponseDto<PlanesEstrategicos>
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public JQGridResponseDto<PlanesEstrategicos> filter(PlanesEstrategicos filterPlanesEstrategicos,
            JQGridRequestDto jqGridRequestDto, Boolean startsWith) {

        List<PlanesEstrategicos> listaPlanesEstrategicos = this.planesEstrategicosDao
                .findAllLike(filterPlanesEstrategicos, jqGridRequestDto, false);
        Long recordNum = this.planesEstrategicosDao.findAllLikeCount(
                filterPlanesEstrategicos != null ? filterPlanesEstrategicos : new PlanesEstrategicos(), false);
        if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
            List<TableRowDto<PlanesEstrategicos>> reorderSelection = this.planesEstrategicosDao
                    .reorderSelection(filterPlanesEstrategicos, jqGridRequestDto, startsWith);
            return new JQGridResponseDto<PlanesEstrategicos>(jqGridRequestDto, recordNum, listaPlanesEstrategicos,
                    reorderSelection);
        }
        return new JQGridResponseDto<PlanesEstrategicos>(jqGridRequestDto, recordNum, listaPlanesEstrategicos);
    }

    /**
     * Filter method in the PlanesEstrategicos table.
     *
     * @param filterPlanesEstrategicos
     *            PlanesEstrategicos
     * @param jqGridRequestDto
     *            JQGridRequestDto
     * @param startsWith
     *            Boolean
     * @return JQGridResponseDto<PlanesEstrategicos>
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public JQGridResponseDto<PlanesEstrategicos> filterCopiar(PlanesEstrategicos filterPlanesEstrategicos,
            JQGridRequestDto jqGridRequestDto, Boolean startsWith) {

        List<PlanesEstrategicos> listaPlanesEstrategicos = this.planesEstrategicosDao
                .findAllLikeCopiar(filterPlanesEstrategicos, jqGridRequestDto, false);
        Long recordNum = this.planesEstrategicosDao.findAllLikeCopiarCount(
                filterPlanesEstrategicos != null ? filterPlanesEstrategicos : new PlanesEstrategicos(), false);
        if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
            List<TableRowDto<PlanesEstrategicos>> reorderSelection = this.planesEstrategicosDao
                    .reorderSelection(filterPlanesEstrategicos, jqGridRequestDto, startsWith);
            return new JQGridResponseDto<PlanesEstrategicos>(jqGridRequestDto, recordNum, listaPlanesEstrategicos,
                    reorderSelection);
        }
        return new JQGridResponseDto<PlanesEstrategicos>(jqGridRequestDto, recordNum, listaPlanesEstrategicos);
    }

    /**
     * 
     * @param filterPlanesEstrategicos
     * @param jqGridRequestDto
     * @param startsWith
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public JQGridResponseDto<PlanesEstrategicos> getVersionesByPlanE(Integer codplane,
            JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
        List<PlanesEstrategicos> listaPlanesEstrategicos = this.planesEstrategicosDao.findVersionesByPlanE(codplane,
                jqGridRequestDto, false);
        Long recordNum = this.planesEstrategicosDao.findVersionesByPlanECount(codplane, false);

        return new JQGridResponseDto<PlanesEstrategicos>(jqGridRequestDto, recordNum, listaPlanesEstrategicos);
    }

    /**
     * Reorder the selection made in PlanesEstrategicos table.
     *
     * @param filterPlanesEstrategicos
     *            PlanesEstrategicos
     * @param jqGridRequestDto
     *            JQGridRequestDto
     * @param startsWith
     *            Boolean
     * @return Object
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public Object reorderSelection(PlanesEstrategicos filterPlanesEstrategicos, JQGridRequestDto jqGridRequestDto,
            Boolean startsWith) {
        return this.planesEstrategicosDao.reorderSelection(filterPlanesEstrategicos, jqGridRequestDto, startsWith);
    }

    /**
     * Operacion CRUD Edit. Modificacion del bean indicado. Solicitar informe
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos Bean que contiene la informacion a modificar.
     * @return PlanesEstrategicos Bean resultante de la modificacion.
     */

    @Override
    public PlanesEstrategicos editInforme(PlanesEstrategicos planesEstrategicos) {
        planesEstrategicos = this.planesEstrategicosDao.editInforme(planesEstrategicos);
        this.planesEstrategicosDao.crearVersionPlanEstrategico(planesEstrategicos);
        return planesEstrategicos;
    }

    /**
     * Operacion CRUD Edit. Modificacion del bean indicado. Solicitar informe
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos Bean que contiene la informacion a modificar.
     * @return PlanesEstrategicos Bean resultante de la modificacion.
     */

    @Override
    public void darBajaplanE(PlanesEstrategicos planesEstrategicos) {
        this.planesEstrategicosDao.darBajaplanE(planesEstrategicos);
    }

    /**
     * Operacion CRUD Edit. Modificacion del bean indicado. Solicitar informe
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos Bean que contiene la informacion a modificar.
     * @return Integer Integer
     */

    @Override
    public Long borraPlanTieneGestion(PlanesEstrategicos planesEstrategicos) {
        return this.planesEstrategicosDao.borraPlanTieneGestion(planesEstrategicos);
    }

    @Override
    public List<PlanesEstrategicos> findAllCombo(PlanesEstrategicos planesEstrategicos,
            JQGridRequestDto jqGridRequestDto) {
        return this.planesEstrategicosDao.findAllCombo(planesEstrategicos, jqGridRequestDto);
    }

    /**
     * Finds a single row in the PlanesEstrategicos table.
     *
     * @param planesEstrategicos
     *            PlanesEstrategicos
     * @return PlanesEstrategicos
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public PlanesEstrategicos findFechas(PlanesEstrategicos planesEstrategicos) {
        return this.planesEstrategicosDao.findFechas(planesEstrategicos);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ejie.ab39a.service.PlanesEstrategicosService#
     * crearCopiaPlanEstrategico(com.ejie.ab39a.model.PlanesEstrategicos)
     */

    /**
     * Crea una copia del plan estratégico
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public PlanesEstrategicos crearCopiaPlanEstrategico(PlanesEstrategicos planesEstrategicos) {

        Integer codplaneorigen = planesEstrategicos.getCodplane();
        planesEstrategicos = this.planesEstrategicosDao.add(planesEstrategicos);
        Integer codplanedestino = planesEstrategicos.getCodplane();
        Integer nuevoPlanId = 0;
        if (codplanedestino != null && codplanedestino.intValue() != 0) {
            nuevoPlanId = this.planesEstrategicosDao.crearCopiaPlanEstrategico(codplaneorigen, codplanedestino);
        }
        planesEstrategicos.setCodplane(nuevoPlanId);
        return planesEstrategicos;
    }

    /**
     * 
     * @param plan
     *            PlanesEstrategicos
     * @return PlanesEstrategicos plan
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public PlanesEstrategicos findPlanMaxVersionByCodPlan(PlanesEstrategicos plan) {
        return this.planesEstrategicosDao.findPlanMaxVersionByCodPlan(plan);
    }

    /**
     * 
     * @param plan
     *            PlanesEstrategicos
     * @return PlanesEstrategicos plan
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public void solicitarInformeNo(PlanesEstrategicos plan) {
        this.planesEstrategicosDao.solicitarInformeNo(plan);
    }
}