package com.yihu.hos.system.dao;

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

    public static final String BEAN_ID = "menuDao";

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

  
    public void activityMenu(String menuid, String activityFlag) throws Exception {
        String sql = "update System_menu set valid='" + activityFlag + "' where Id='" + menuid + "'";
        super.execute(sql);
    }

  
    public void deleteMenu(String id) throws Exception {
        //删除父菜单
        SystemMenu systemMenu = this.getEntity(SystemMenu.class, id);
        deleteEntity(systemMenu);
    }

  
    public SystemMenu getMenuByCodeWithOutId(String code, String id) throws Exception {
        List<SystemMenu> sms = (List<SystemMenu>) super.hibernateTemplate.find("from SystemMenu s where s.code=? and s.id!=?", code, id);
        if (sms != null && sms.size() > 0) {
            return sms.get(0);
        }
        return null;
    }

  
    public SystemMenu getMenuByCode(String code) throws Exception {
        List<SystemMenu> sms = (List<SystemMenu>) super.hibernateTemplate.find("from SystemMenu s where s.code=?", code);
        if (sms != null && sms.size() > 0) {
            return sms.get(0);
        }
        return null;
    }
}
