package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemMenuAction;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

/**
 * Created by chenweida on 2016/1/18.
 */
@Repository("menuActionDao")
public class MenuActionDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "menuActionDao";

   
    public List<SystemMenuAction> getAllMenutAction(Map<String, Object> params) throws Exception {
        StringBuilder sb = new StringBuilder("from SystemMenuAction s where 1=1");
        if (!StringUtils.isEmpty(params.get("name"))) {
            sb.append(" and  s.name like '%" + params.get("name") + "%'");
        }
        if (!StringUtils.isEmpty(params.get("menuId"))) {
            sb.append(" and  s.menuId = '" + params.get("menuId") + "'");
        }
        return (List<SystemMenuAction>) super.hibernateTemplate.find(sb.toString());
    }

   
    public void activityMenuAction(String menuactionId, String activityFlag) throws Exception {
        String sql = "update System_menu_action set flag='" + activityFlag + "' where Id='" + menuactionId + "'";
        super.execute(sql);
    }

   
    public SystemMenuAction getMenuActionByNameAndMenuId(String name, String menuid) throws Exception {
        List<SystemMenuAction> ss = (List<SystemMenuAction>) super.hibernateTemplate.find("from SystemMenuAction s where s.name=? and s.menuId=?", name, menuid);
        if (ss != null && ss.size() > 0) {
            return ss.get(0);
        }
        return null;
    }

   
    public SystemMenuAction getMenuActionByNameAndMenuIdWithOutId(String name, String menuid, String id) throws Exception {
        List<SystemMenuAction> ss = (List<SystemMenuAction>) super.hibernateTemplate.find("from SystemMenuAction s where s.name=?  and s.menuId=? and s.id!=?", name, menuid, id);
        if (ss != null && ss.size() > 0) {
            return ss.get(0);
        }
        return null;
    }
}
