package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemServiceFlowClass;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/8/19.
 */
@Repository("flowClassDao")
public class FlowClassDao extends SQLGeneralDAO {
    public static final String BEAN_ID = "flowClassDao";


    public List<SystemServiceFlowClass> getFlowClassByFlowId(Integer flowId) throws Exception {
        List<SystemServiceFlowClass> flowClasses = (List<SystemServiceFlowClass>) super.hibernateTemplate.find("from SystemServiceFlowClass s where s.flowId=? ", flowId);
        if (flowClasses != null && flowClasses.size() > 0) {
            return flowClasses;
        }
        return new ArrayList<>();
    }

    public List<SystemServiceFlowClass> getFlowClass(Integer flowId, String type) throws Exception {
        List<SystemServiceFlowClass> flowClasses = (List<SystemServiceFlowClass>) super.hibernateTemplate.find("from SystemServiceFlowClass s where s.flowId=? and type=? ", flowId, type);
        if (flowClasses != null && flowClasses.size() > 0) {
            return flowClasses;
        }
        return new ArrayList<>();
    }

    public boolean deleteFlowClassByFlowId(Integer flowId) {
        try {
            Session session = getCurrentSession();
            String sql = "delete from system_service_flow_class where flow_id = :flowId";
            Query query = session.createSQLQuery(sql);
            query.setInteger("flowId", flowId);
            query.executeUpdate();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public List<Integer> getFlowClassIds(Integer flowId) {
        Session session = getCurrentSession();
        String sql = "SELECT id from system_service_flow_class where flow_id = :flowId";
        Query query = session.createSQLQuery(sql);
        query.setInteger("flowId", flowId);
        return query.list();
    }
}
