package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemServiceFlowTemp;
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("flowTempDao")
public class FlowTempDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "flowTempDao";

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

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

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