package com.ag.web.agCommon;

import com.wb.util.DbUtil;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/14.
 */
public class AgCommonService {
    /**
     * Description: 核对菜单位置
     * @param request
     * @param menuId
     * @param tenancyId
     * @param datalist
     * @return
     * Note: Nothing much.
     */
    public  List check(HttpServletRequest request, String menuId,String tenancyId,List datalist) throws Exception{
        //定义两个变量
        String postion = String.valueOf(0);
        String parentMenu = "";
        Map dataMap = new HashMap();
        if (!StringUtils.isEmpty(menuId)) {
            dataMap.put("menuId", menuId);
        }
        if (!StringUtils.isEmpty(tenancyId)){
            dataMap.put("tenancyId",tenancyId);
        }
        String sql = " select * from wb_menu where parent_menu = " +
                " (select parent_menu from wb_menu where menu_id='"+menuId+"' )";
        if (!StringUtils.isEmpty(tenancyId)){
            sql+=" and tenancyId='\"+tenancyId+\"'";
        }

               sql+= "         order by order_index";
        ResultSet data = (ResultSet) DbUtil.run(request,sql);
        int i=0;
        while (data.next()){
            if(menuId.equals(data.getString("MENU_ID"))){
                postion = String.valueOf(i);
                parentMenu = (String) data.getString("PARENT_MENU");
                break;
            }
            i++;
        }
        if(!parentMenu.equals("-1")){
            datalist.add(postion);
            check(request,parentMenu,tenancyId,datalist);
        }else{
            datalist.add(postion);
        }
        return datalist;
    }
}
