package com.yihu.hos.resource.dao;

import com.yihu.hos.resource.model.RsResourceMetadata;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/21.
 */
@Repository("resourceMetadataDao")
public class RsResourceMetadataDaoImpl extends SQLGeneralDAO{

    public static final String BEAN_ID = "resourceMetadataDao";

  
    public void deleteMetadatasByDataSetCode(String dataSetCode) throws Exception {

        super.hibernateTemplate.deleteAll(super.hibernateTemplate.find("from RsResourceMetadata s where s.datasetId=?", dataSetCode));
    }

  
    public void deleteLogicDataSupportMetaDataByLocalDataSetId(String datasetId) throws Exception {
        super.hibernateTemplate.deleteAll(super.hibernateTemplate.find("from RsResourceMetadata s where s.datasetId=? and s.dataSupportType=1", datasetId));
    }

  
    public List<RsResourceMetadata> getMetadataByLocalDatssetId(String datasetId) {
        return (List<RsResourceMetadata>) super.hibernateTemplate.find("from RsResourceMetadata r where r.datasetId=?", datasetId);
    }

  
    public Result getResourceAuthorize(Map<String, Object> params) throws Exception {
        StringBuilder sb = new StringBuilder("from RsResourceMetadata t where 1=1 ");

        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));

    }

  
    public List<RsResourceMetadata> getMetadataByResourceId(String code) throws Exception {
        return (List<RsResourceMetadata>) super.hibernateTemplate.find("from RsResourceMetadata r where r.resourceId=?", code);
    }

  
    public RsResourceMetadata getMetadataByCodeAndResourceId(String code, String resourceId) throws Exception {
        List<RsResourceMetadata> rms = (List<RsResourceMetadata>) super.hibernateTemplate.find("from RsResourceMetadata r where r.code=? and r.resourceId=?", code, resourceId);
        if (rms != null && rms.size() > 0) {
            return rms.get(0);
        }
        return null;
    }

  
    public RsResourceMetadata getMetadataByCodeAndResourceIdWithOutId(String code, String resourceId, String id) throws Exception {
        List<RsResourceMetadata> rms = (List<RsResourceMetadata>) super.hibernateTemplate.find("from RsResourceMetadata r where r.code=? and r.resourceId=? and r.id!=?", code, resourceId,id);
        if (rms != null && rms.size() > 0) {
            return rms.get(0);
        }
        return null;
    }
}
