package com.yihu.hos.crawler.dao;

import com.yihu.hos.crawler.model.flow.CrawlerDataSetModel;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

/**
 * Created by HZY on 2016/4/27.
 */
@Repository("CrawlerDatasetDao")
public class CrawlerDatasetDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "CrawlerDatasetDao";

    public List<CrawlerDataSetModel> getCrawlerDatasetList(Integer versionId) throws Exception {
        String sql = "select * from crawler_dataset where scheme_version_id='" + versionId + "'";
        return super.queryListBySql(sql, CrawlerDataSetModel.class);
    }

    public void deleteCrawlerDatasetList(Integer versionId) throws Exception {
        String sql = "delete from crawler_dataset where scheme_version_id= :scheme_version_id";
        Query query = getCurrentSession().createSQLQuery(sql);
        query.setInteger("scheme_version_id", versionId);
        query.executeUpdate();
    }

    public Integer getTotalRows() throws Exception {
        String countSql = "SELECT COUNT(*) FROM (SELECT a.scheme_id, a.scheme_version_id, GROUP_CONCAT(a.dataset_id SEPARATOR ',') AS datasetId, GROUP_CONCAT(a.dataset_name SEPARATOR ',') AS datasetName" +
                "  FROM crawler_dataset a " +
                "  GROUP BY a.scheme_id, a.scheme_version_id ) b WHERE 1= 1";
        Query query = getCurrentSession().createSQLQuery(countSql);
        List<BigInteger> list = query.list();
        int count = list.get(0).intValue();
        return count;
    }
}
