package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBBlockDetailsService;
import vn.azteam.tpf.domain.TBBlockDetails;
import vn.azteam.tpf.repository.TBBlockDetailsRepository;
import vn.azteam.tpf.repository.search.TBBlockDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBBlockDetailsDTO;
import vn.azteam.tpf.service.dto.TBSuppliesInWarehouseDTO;
import vn.azteam.tpf.service.dto.WarehouseStockUpdateDTO;
import vn.azteam.tpf.service.mapper.TBBlockDetailsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TBBlockDetails.
 */
@Service
@Transactional
public class TBBlockDetailsServiceImpl implements TBBlockDetailsService {

    private final Logger log = LoggerFactory.getLogger(TBBlockDetailsServiceImpl.class);

    private final TBBlockDetailsRepository tBBlockDetailsRepository;

    private final TBBlockDetailsMapper tBBlockDetailsMapper;

    private final TBBlockDetailsSearchRepository tBBlockDetailsSearchRepository;

    public TBBlockDetailsServiceImpl(TBBlockDetailsRepository tBBlockDetailsRepository, TBBlockDetailsMapper tBBlockDetailsMapper, TBBlockDetailsSearchRepository tBBlockDetailsSearchRepository) {
        this.tBBlockDetailsRepository = tBBlockDetailsRepository;
        this.tBBlockDetailsMapper = tBBlockDetailsMapper;
        this.tBBlockDetailsSearchRepository = tBBlockDetailsSearchRepository;
    }

    /**
     * Save a tBBlockDetails.
     *
     * @param tBBlockDetailsDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBBlockDetailsDTO save(TBBlockDetailsDTO tBBlockDetailsDTO) {
        log.debug("Request to save TBBlockDetails : {}", tBBlockDetailsDTO);

        TBBlockDetails tBBlockDetails = tBBlockDetailsMapper.toEntity(tBBlockDetailsDTO);
        tBBlockDetails = tBBlockDetailsRepository.save(tBBlockDetails);
        TBBlockDetailsDTO result = tBBlockDetailsMapper.toDto(tBBlockDetails);
        tBBlockDetailsSearchRepository.save(tBBlockDetails);
        return result;
    }

    /**
     * Get all the tBBlockDetails.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBBlockDetailsDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TBBlockDetails");
        return tBBlockDetailsRepository.findAll(pageable)
            .map(tBBlockDetailsMapper::toDto);
    }


    /**
     * Get one tBBlockDetails by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TBBlockDetailsDTO> findOne(Long id) {
        log.debug("Request to get TBBlockDetails : {}", id);
        return tBBlockDetailsRepository.findById(id)
            .map(tBBlockDetailsMapper::toDto);
    }

    /**
     * Delete the tBBlockDetails by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBBlockDetails : {}", id);
        tBBlockDetailsRepository.deleteById(id);
        tBBlockDetailsSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBBlockDetails corresponding to the query.
     *
     * @param query the query of the search
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBBlockDetailsDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBBlockDetails for query {}", query);
        return tBBlockDetailsSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBBlockDetailsMapper::toDto);
    }


    @Transactional(readOnly = false)
    public TBBlockDetailsDTO getTBBlockDetailsByWarehouseStockUpdate(
        WarehouseStockUpdateDTO warehouseStockUpdateDTO,
        TBSuppliesInWarehouseDTO tBSuppliesInWarehouseDTO) {
        Optional<TBBlockDetails> optional = tBBlockDetailsRepository.getByCodeAndTbSuppliesInWarehouseId(
            warehouseStockUpdateDTO.getCode(),
            tBSuppliesInWarehouseDTO.getId()
        );

        if (optional.isPresent()){
            return tBBlockDetailsMapper.toDto(optional.get());
        } else {
            TBBlockDetailsDTO tBlockDetailsDTO = new TBBlockDetailsDTO();
            tBlockDetailsDTO.setCode(warehouseStockUpdateDTO.getCode());
            tBlockDetailsDTO.setQuantity(0.0);
            tBlockDetailsDTO.setMfgDate(warehouseStockUpdateDTO.getMfgDate());
            tBlockDetailsDTO.setExpDate(warehouseStockUpdateDTO.getExpDate());
            tBlockDetailsDTO.setTbSuppliesInWarehouseId(tBSuppliesInWarehouseDTO.getId());
            TBBlockDetailsDTO result = save(tBlockDetailsDTO);
            return result;
        }
    }
}
