package cn.estsh.i3plus.core.apiservice.daoimpl;

import cn.estsh.i3plus.core.apiservice.dao.IReportDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * @Description :
 * @Reference :
 * @Author : Adair Peng
 * @CreateDate : 2019-01-20 16:14
 * @Modify:
 **/
@Service
public class ReportDaoImpl implements IReportDao {

    public static final Logger LOGGER = LoggerFactory.getLogger(ReportDaoImpl.class);

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<Object[]> findByHqlObjects(String hql, String[] paramName, Object[] paramValue) throws Exception {
        LOGGER.info(" find HQL:{} paramName:{} paramValue:{}", hql, paramName, paramValue);
        Query queryObject = entityManager.createQuery(hql);
        if(paramName != null && paramValue != null){
            for (int i = 0; i < paramName.length; i++) {
                if (paramValue[i] != null) {
                    queryObject.setParameter(paramName[i], paramValue[i].toString());
                }
            }
        }
        entityManager.clear();
        return queryObject.getResultList();
    }
}
