package com.engine.cube.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.formmode.cache.ModeComInfo;
import com.engine.cube.biz.moderight.CubeFieldRightInfo;
import com.engine.cube.entity.CardEntity;
import com.engine.cube.util.AddSeclevelUtil;
import com.weaver.formmodel.util.StringHelper;
import weaver.archives.util.ArchivesFModeUtil;
import weaver.conn.RecordSet;
import weaver.formmode.search.FormModeRightInfo;
import weaver.formmode.setup.ModeRightInfo;
import weaver.formmode.setup.ModeRightInfoExtend;
import weaver.formmode.setup.ModeSetUtil;
import weaver.formmode.view.ModeShareManager;
import weaver.formmode.view.ModeViewLog;
import weaver.formmode.virtualform.VirtualFormHandler;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.form.FormManager;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CardHelper {
	/**
	 * 显示页面快速编辑保存线程池,用于快速编辑异步任务处理
	 */
	private final static ExecutorService quickSavePool = Executors.newFixedThreadPool(3);
	
    public static ExecutorService getQuickSavePool() {
		return quickSavePool;
	}
	public static CardEntity initBaseCardEntity(Map params){
        CardEntity card = new CardEntity();
        card.setParams(params);
        ModeComInfo modeComInfo = new ModeComInfo();
        card.setLayoutId(ParamUtil.getInt(params, "layoutid"));
        card.setModeId(ParamUtil.getInt(params,"modeId"));
        int formid = Util.getIntValue(modeComInfo.getFormId(""+card.getModeId()),0);
        if(formid == 0){
            formid = Util.getIntValue(ParamUtil.get(params,"formId"),0);
        }
        card.setFormId(formid);
        card.setBillId(ParamUtil.get(params,"billid"));
        if(!StringHelper.isEmpty(Util.null2String(card.getParams().get("pkfield")))){
            card.setBillId(getRealBillid(card,params));
        }
        card.setVirtualForm(VirtualFormHandler.isVirtualForm(card.getFormId()));
        card.setCustomId(ParamUtil.getInt(params, "customid"));
        card.setType(ParamUtil.getInt(params, "type"));
        card.setParam_ip(ParamUtil.get(params,  "param_ip"));
        card.setModedatastatus(Util.getIntValue(ParamUtil.get(params, "modedatastatus"),0));
        card.setOldModedatastatus(Util.getIntValue(ParamUtil.get(params, "oldmodedatastatus"),0));
        return card;
    }
    public static CardEntity initCardEntity(Map params) {
        CardEntity card = new CardEntity();
        card.setParams(params);
        ModeComInfo modeComInfo = new ModeComInfo();
        card.setLayoutId(ParamUtil.getInt(params, "layoutid"));
        card.setModeId(ParamUtil.getInt(params,"modeId"));
        card.setFormId(Util.getIntValue(modeComInfo.getFormId(""+card.getModeId())));
        if(card.getFormId() == -1){//当表单未关联模块时，直接取params中的formid
            card.setFormId(Util.getIntValue(ParamUtil.get(params,"formId")));
        }
        String billid=ParamUtil.get(params,"billid");
        String copyid=ParamUtil.get(params,"copyid");
        if(("".equals(billid) || "-1".equals(billid))&&!"".equals(copyid)){
        	billid=copyid;
        }
        card.setBillId(billid);
        if(!StringHelper.isEmpty(Util.null2String(card.getParams().get("pkfield")))){
            card.setBillId(getRealBillid(card,params));
        }
        card.setVirtualForm(VirtualFormHandler.isVirtualForm(card.getFormId()));
        card.setCustomId(ParamUtil.getInt(params, "customid"));
        card.setType(ParamUtil.getInt(params, "type"));
        card.setIsfrontmultlang("1".equals(modeComInfo.getIsfrontmultlang(""+card.getModeId())));
        card.setAuthorizefieldid(ParamUtil.getInt(params,"authorizefieldid"));
        card.setAuthorizeformmodebillId(ParamUtil.getInt(params,"authorizeformmodebillId"));
        card.setAuthorizemodeId(ParamUtil.getInt(params, "authorizemodeId"));
        card.setFormmode_authorize(ParamUtil.get(params,"formmode_authorize"));

        card.setFormModeReplyid(Util.getIntValue(ParamUtil.get(params, "authorizeformModeReplyid"),0));
        card.setfMReplyFName(Util.null2String(ParamUtil.get(params,"authorizefMReplyFName")));


        card.setParam_ip(ParamUtil.get(params, "param_ip"));

        FormManager fManager = new FormManager();
        String tableName = fManager.getTablename(card.getFormId());


        boolean modeDataStatusFlag= false;
        modeDataStatusFlag=new ModeSetUtil().isHaveModedatastatusByformid(card.getFormId());
        if(modeDataStatusFlag&&StringHelper.isNotEmpty(billid)){
            RecordSet rs = new RecordSet();
            rs.executeQuery("select modedatastatus from "+tableName+" where id =?",billid);
            if(rs.next()) {
                int modedatastatus = rs.getInt("modedatastatus");
                card.setModedatastatus(modedatastatus);
            }
        }else {
            card.setModedatastatus(Util.getIntValue(ParamUtil.get(params, "modedatastatus"), 0));
        }

//        boolean modeDataStatusFlag=rs.executeQuery("select modedatastatus from "+tableName+" where id ="+billid);
//        if(modeDataStatusFlag){
//            if(rs.next()) {
//                int modedatastatus = rs.getInt("modedatastatus");
//                card.setModedatastatus(modedatastatus);
//            }
//        }else {
//            card.setModedatastatus(Util.getIntValue(ParamUtil.get(params, "modedatastatus"), 0));
//        }

        String JSONStr = ParamUtil.get(params, "JSONStr");
        card.setJo(JSONObject.parseObject(JSONStr));

        String docfileid = ParamUtil.get(params, "adddocfieldid");
        String newdocid = ParamUtil.get(params, "docid"); // 新建文档的工作流字段String
        String cardid = ParamUtil.get(params, "cardid"); 
        card.setCardid(cardid);
        Hashtable<String,String> docmap = new Hashtable<String,String>();
        docmap.put("docfileid", docfileid);
        docmap.put("newdocid", newdocid);
        card.setDocmap(docmap);
        return card;
    }

    public static String getRealBillid(CardEntity card,Map params){
        String billid = card.getBillId();
        int fromId = card.getFormId();
        int modeid = card.getModeId();
        String pkfield = Util.null2String(card.getParams().get("pkfield"));
        RecordSet rs = new RecordSet();
        String detailbillid = Util.null2String(card.getParams().get("detailbillid"));
        String detailtable = Util.null2String(card.getParams().get("detailtable"));
        if(!"id".equals(pkfield)&&!"".equals(pkfield)){//当设置了主键时，重新解析billid
            FormManager fManager = new FormManager();
            String table = fManager.getTablename(fromId);
            if(billid.isEmpty() && !detailbillid.isEmpty() && !detailtable.isEmpty()){
                rs.executeQuery("select t.mainid from "+detailtable+" t where t."+pkfield+"=?",detailbillid);
                if(rs.next()){
                    billid = Util.null2String(rs.getInt("mainid"));
                }
            }else{
                //虚拟表单处理
                if(VirtualFormHandler.isVirtualForm(fromId)){
                    table = VirtualFormHandler.getRealFromName(table);
                    Map<String, Object> vFormInfo = VirtualFormHandler.getVFormInfo(fromId);
                    String vformtype=Util.null2String(vFormInfo.get("vformtype"));
                    String vsql=Util.null2String(vFormInfo.get("vsql"));
                    if("2".equals(vformtype)){
                        table="("+vsql+")";
                    }
                }
                try {
                    if(!VirtualFormHandler.isVirtualForm(fromId)){
                        rs.executeQuery("select id from "+table+" where "+pkfield+"=? and formmodeid=?",billid,modeid);
                    }else{
                        rs.executeQuery("select id from "+table+" where "+pkfield+"=?",billid);
                    }
                    if(rs.next()){
                        billid = Util.null2String(rs.getInt("id"));
                    }
                } catch(Exception e) {}
            }
        }else{
            if(billid.isEmpty() && !detailbillid.isEmpty() && !detailtable.isEmpty()){
                rs.executeQuery("select t.mainid from "+detailtable+" t where t.id=?",detailbillid);
                if(rs.next()){
                    billid = Util.null2String(rs.getInt("mainid"));
                }
            }

        }
        return billid;
    }
    public static void checkRight(CardEntity card, User user){
        checkRight(card, user, false);
    }
    public static void checkRight(CardEntity card, User user, boolean forSubmit) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        FormModeRightInfo FormModeRightInfo = new FormModeRightInfo();
        ModeShareManager ModeShareManager = new ModeShareManager();
        RecordSet rs = new RecordSet();

        int MaxShare = 0;
        boolean isRight = false;
        boolean isDel = false;
        boolean isEdit = false;
        boolean isResultRight = false;

        if(!card.isVirtualForm()){
            List<User> lsUser = new ArrayList<User>();
            if(card.getType()==1){//新建时只取当前账户
                lsUser.add(user);
            }else{
                lsUser = modeRightInfo.getAllUserCountList(user);
            }
            modeRightInfo.setModeId(card.getModeId());
            for(int i=0;i<lsUser.size();i++){
                User tempUser = lsUser.get(i);
                modeRightInfo.setUser(tempUser);
                if(card.getType()==1){//新建：判断是否具有创建权限
                    isRight = modeRightInfo.checkUserRight(card.getType());
                }else if(card.getType()==3){//监控：首先判断查询列表中是否有监控权限，如果没有则判断全局监控权限
                    FormModeRightInfo.setUser(tempUser);
                    isRight = FormModeRightInfo.checkUserRight(card.getCustomId(),4);
                    if(!isRight){
                        isRight = modeRightInfo.checkUserRight(card.getType());
                    }
                    if(modeRightInfo.checkUserRight(card.getType())){
                        isDel = true;
                    }
                }else if(card.getType()==0 || card.getType() == 2){//查看、编辑：判断是否具有查看、编辑、删除权限
            		ModeShareManager.setModeId(card.getModeId());
            		ModeShareManager.setBillid(card.getBillId());
            		ModeShareManager.setFormid(card.getFormId());
            		ModeShareManager.setDel(true);
                    String rightStr = ModeShareManager.getShareDetailTableByUser("formmode",tempUser);
                    String billid = Util.null2String(card.getBillId());
                    rs.execute("select sourceid,max(sharelevel) as sharelevel from "+rightStr+" t where sourceid='"+billid+"' group by sourceid");
                    if(rs.next()){
                        MaxShare = rs.getInt("sharelevel");
                        isRight = true;
                        if(MaxShare > 1) {
                            isEdit = true;		//有编辑或完全控制权限的出现编辑按钮
                            if(MaxShare == 3) isDel = true;		//有完全控制权限的出现删除按钮
                        }
                    }
                    //角色受条件限制特殊处理
                    int roleLimitedMaxsharelevel = ModeShareManager.getRoleLimitedMaxsharelevel(user.getUID()+"");
                    if(roleLimitedMaxsharelevel>MaxShare){
                        MaxShare = roleLimitedMaxsharelevel;
                        isRight = true;
                        if(MaxShare > 1) {
                            isEdit = true;		//有编辑或完全控制权限的出现编辑按钮
                            if(MaxShare == 3) isDel = true;		//有完全控制权限的出现删除按钮
                }
                    }
                }
                //针对卡片权限补充校验
                if(MaxShare<3){
                	CubeFieldRightInfo cubeFieldRightInfo=new CubeFieldRightInfo(card.getModeId(), card.getFormId(), tempUser);
                	int sharelevle=cubeFieldRightInfo.maxShareLevelRolelimited(Util.null2String(card.getBillId()));
                	if(sharelevle>MaxShare){
                		if(sharelevle>-1){
                            isRight = true;
                            if(sharelevle > 1) {
                                isEdit = true;		//有编辑或完全控制权限的出现编辑按钮
                                if(sharelevle == 3) isDel = true;		//有完全控制权限的出现删除按钮
                            }
                		}
                	}
                }
                if(isRight){
                    isResultRight = true;
                    if(isDel){
                        break;
                    }
                }
            }
            isRight = isResultRight;
            if(forSubmit &&  card.getType() == 2) {
                isRight = isEdit;
            }
        }else{//虚拟表单权限
            ModeRightInfoExtend modeRightInfoExtend=new ModeRightInfoExtend();
            modeRightInfoExtend.setModeId(card.getModeId());
            modeRightInfoExtend.setUser(user);
            modeRightInfoExtend.setFormid(card.getFormId());
            modeRightInfoExtend.setBillid(card.getBillId());
            if(card.getType()==1){//新建
                isRight = modeRightInfoExtend.checkUserRight(1);
            }else if(card.getType()==0||card.getType()==2){//查看、编辑时判断是否显示编辑、删除按钮
                isRight=true;//查看权限
                isDel=modeRightInfoExtend.checkUserRightByRightType(3);//完全控制权限
                if(isDel){
                    isEdit=true;
                }else{
                    isEdit=modeRightInfoExtend.checkUserRightByRightType(2);//编辑权限
                }
            }else if(card.getType()==3){//监控
                isRight=true;
            }
        }

        if(card.getType()==4){
            isRight = true;
        }
        if(!forSubmit) {
            //表单建模判断关联授权
            String authorizeOpttype = "";
            String authorizeLayoutid = "";
            String authorizeLayoutlevel = "";
            //表单建模判断关联授权
            Map<String, String> formmodeAuthorizeInfo = new HashMap<String, String>();
            if ("formmode_authorize".equals(card.getFormmode_authorize())) {

                modeRightInfo = new ModeRightInfo();
                modeRightInfo.setUser(user);

                if (card.getFormModeReplyid() != 0) {
                    formmodeAuthorizeInfo = modeRightInfo.isFormModeAuthorize(card.getFormmode_authorize(), card.getAuthorizemodeId(), card.getAuthorizeformmodebillId(), card.getAuthorizefieldid(), 0, card.getFormModeReplyid(), card.getfMReplyFName());
                } else {
                    formmodeAuthorizeInfo = modeRightInfo.isFormModeAuthorize(
                            card.getFormmode_authorize(), card.getAuthorizemodeId(),
                            card.getAuthorizeformmodebillId(), card.getAuthorizefieldid(), Util.getIntValue(getRealBillid(card)));
                }
                if ("1".equals(formmodeAuthorizeInfo.get("AuthorizeFlag"))) {
                    isRight = true;
                    authorizeOpttype = formmodeAuthorizeInfo.get("opttype");
                    if ("2".equals(authorizeOpttype)) {
                        isEdit = true;
                    }
                    authorizeLayoutid = formmodeAuthorizeInfo.get("layoutid");
                    authorizeLayoutlevel = formmodeAuthorizeInfo.get("layoutlevel");
                }
            }

            card.setAuthorizeOpttype(authorizeOpttype);
            card.setAuthorizeLayoutid(authorizeLayoutid);
            card.setAuthorizeLayoutlevel(authorizeLayoutlevel);
        }
        // 兼容档案权限
        Map<String, Boolean> archiveRight = ArchivesFModeUtil.checkAndGetArchiveRight(String.valueOf(card.getModeId()), String.valueOf(card.getFormId()), card.getBillId(), user);
        card.setEdit(isEdit || archiveRight.getOrDefault("hasEditRight", false));
        card.setMaxShare(MaxShare);
        card.setDel(isDel || archiveRight.getOrDefault("hasDeleteRight", false));
        card.setRight(isRight || archiveRight.getOrDefault("hasRight", false));
    }

    /**
     * //当设置了主键时，重新解析billid
     * @return
     */
    private static String getRealBillid(CardEntity card){
    	String billid = card.getBillId();
    	String pkfield = Util.null2String(card.getParams().get("pkfield"));
    	if(!"id".equals(pkfield)&&!"".equals(pkfield)){//当设置了主键时，重新解析billid
    		RecordSet rs = new RecordSet();
    		FormManager fManager = new FormManager();
    		String table = fManager.getTablename(card.getFormId());
    		String detailbillid = Util.null2String(card.getParams().get("detailbillid"));
    		String detailtable = Util.null2String(card.getParams().get("detailtable"));
    		if(billid.isEmpty() && !detailbillid.isEmpty() && !detailtable.isEmpty()){
    		    rs.executeQuery("select t.mainid from "+detailtable+" t where t."+pkfield+"=?",detailbillid);
    			if(rs.next()){
    				billid = Util.null2String(rs.getInt("mainid"));
    			}
    		}else{
    			try {
                    rs.executeQuery("select id from "+table+" where "+pkfield+"=? and formmodeid="+card.getModeId(),card.getBillId());
    				if(rs.next()){
    					billid = Util.null2String(rs.getInt("id"));
    				}
    			} catch(Exception e) {}	
    		}
    	}
    	return billid;
    }
    
    
    public void recordLog(CardEntity card, User user){
        ModeViewLog ModeViewLog = new ModeViewLog();
        if(card.getType() ==0&&card.getModeId()!=0){
            String operatetype = "4";//操作的类型： 1：新建 2：修改 3：删除 4：查看
            String clientaddress = card.getParam_ip();
            String operatedesc = SystemEnv.getHtmlLabelName(367,user.getLanguage());//查看
            int relatedid = Util.getIntValue(card.getBillId());
            String relatedname = "";

            ModeViewLog.resetParameter();
            ModeViewLog.setClientaddress(clientaddress);
            ModeViewLog.setModeid(card.getModeId());
            ModeViewLog.setOperatedesc(operatedesc);
            ModeViewLog.setHtmllabelindex(367);//#887673
            ModeViewLog.setOperatetype(operatetype);
            ModeViewLog.setOperateuserid(user.getUID());
            ModeViewLog.setRelatedid(relatedid);
            ModeViewLog.setRelatedname(relatedname);
            try {
                ModeViewLog.setSysLogInfo();
            } catch (Exception e) {
            }
        }
    }

    public static void  getResolveFormMode(CardEntity card, User user) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        int layoutid = card.getLayoutId();
        
        //验证layoutid是否有效
        if(layoutid > 0 ){
        	String sql0="select id from modehtmllayout where modeid=? and type=? and formId=? and id=?";
        	rs.executeQuery(sql0, card.getModeId(), card.getType(), card.getFormId(), card.getLayoutId());
            if(!rs.next()){
                layoutid = 0;
            }
        }
        //如果是草稿直接返回创建人的新建布局
        if(card.getModedatastatus()==1&&card.getType()==2){
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setModeId(card.getModeId());
            modeRightInfo.setType(1);
            modeRightInfo.setUser(user);
            layoutid = modeRightInfo.getLayoutIdOfUserRight();
            if(layoutid<=0){
                rs.executeQuery("select * from modehtmllayout where modeid=? and type=? and formId=? and isdefault=1", card.getModeId(), 1, card.getFormId());
                if(rs.next()){
                    layoutid = Util.getIntValue(rs.getString("id"),0);
                }
            }
        }
        layoutid = layoutid < 0 ? 0:layoutid;
        if(layoutid==0){
            if(card.isVirtualForm()){
                ModeRightInfo modeRightInfo = new ModeRightInfo();
                modeRightInfo.setModeId(card.getModeId());
                modeRightInfo.setType(card.getType());
                modeRightInfo.setUser(user);
                modeRightInfo.setAuthorizeOpttype(card.getAuthorizeOpttype());
                modeRightInfo.setAuthorizeLayoutid(card.getAuthorizeLayoutid());
                modeRightInfo.setAuthorizeLayoutlevel(card.getAuthorizeLayoutlevel());
                layoutid =  modeRightInfo.getVirLayoutIdOfUserRight(card.getType());
            }else{
                ModeRightInfo modeRightInfo = new ModeRightInfo();
                modeRightInfo.setModeId(card.getModeId());
                modeRightInfo.setType(card.getType());
                modeRightInfo.setUser(user);
                if(card.getType() == 1){//新建
                    layoutid = modeRightInfo.getLayoutIdOfUserRight();
                }else if(card.getType() == 3){//监控权限判断
                    layoutid = modeRightInfo.getLayoutIdOfUserRight();
                }else if(card.getType() == 0 || card.getType() == 2){//查看、编辑权限
                	layoutid = modeRightInfo.getLayoutid(user, card.getModeId(), card.getType(), card.getBillId(),card.getAuthorizeLayoutlevel(), card.getAuthorizeLayoutid());
                }
            }
        }
        if(layoutid==0){
            //有授权取授权布局
            if(Util.getIntValue(card.getAuthorizeOpttype(),-1)>-1){
                layoutid = Util.getIntValue(card.getAuthorizeLayoutid(),0);
            }
        }
        if(layoutid==0){
            rs.executeQuery("select * from modehtmllayout where modeid=? and type=? and formId=? and isdefault=1", card.getModeId(), card.getType(), card.getFormId());
        }else{
            rs.executeQuery("select * from modehtmllayout where id= ? ",layoutid);
            //判断布局是否被删除，如果被删除，则查默认布局
            if(!rs.next()){
                if(card.getModedatastatus()==1&&card.getType()==2){
                    rs.executeQuery("select * from modehtmllayout where modeid=? and type=? and formId=? and isdefault=1", card.getModeId(), 1, card.getFormId());
                }else {
                    rs.executeQuery("select * from modehtmllayout where modeid=? and type=? and formId=? and isdefault=1", card.getModeId(), card.getType(), card.getFormId());
                }
            }
        }
        rs.beforFirst();
        if(rs.next()){
            layoutid = Util.getIntValue(Util.null2String(rs.getInt("id")), 0);

            //判断关联的模块有没有开启分级保护开启了没开启就过滤密级字段
            AddSeclevelUtil addSeclevelUtil = new AddSeclevelUtil();
            boolean isOpenClassprotect = false;
            Map<String, Object> params = new HashMap<>();
            params.put("modeid", card.getModeId());
            isOpenClassprotect = addSeclevelUtil.modeOpenClassProtect(params);

            JSONObject datajson = JSONObject.parseObject(Util.null2String(rs.getString("datajson")));
            if(!isOpenClassprotect){
                if (datajson != null) {
                    JSONObject eformdesign = (JSONObject) datajson.get("eformdesign");
                    if (eformdesign != null) {
                        JSONObject etables = (JSONObject) eformdesign.get("etables");
                        if (etables != null) {
                            JSONObject emaintable = (JSONObject) etables.get("emaintable");
                            if (emaintable != null) {
                                JSONArray ecarray = (JSONArray) emaintable.get("ec");
                                JSONArray newecarray = new JSONArray();
                                for (int i = 0; i < ecarray.size(); i++) {
                                    JSONObject temp = (JSONObject) ecarray.get(i);
                                    if (!"-9".equals(temp.get("field") + "")) {
                                        newecarray.add(temp);
                                    }
                                }
                                emaintable.put("ec", newecarray);
                                etables.put("emaintable", emaintable);
                                eformdesign.put("etables", etables);
                                datajson.put("eformdesign", eformdesign);
                            }
                        }
                    }
                }
            }


            card.setDatajson(null==datajson?"":datajson.toJSONString());
            card.setVersion(Util.getIntValue(rs.getString("version"),0));
            card.setLayoutId(layoutid);
            boolean secondPassword = "1".equals(Util.null2String(rs.getInt("secondPassword")));
            card.setSecondPassword(secondPassword);
            card.setIsquickedit("1".equals(Util.null2String(rs.getString("isquickedit"))));
        }
    }
    
    /**
     * 开启显示布局快速编辑时查询编辑布局id
     * @param card
     * @param user
     * @return
     */
    public static int  getEditLayoutidForView(CardEntity card, User user) {
    	int temptype=card.getType();
    	card.setType(2);
        RecordSet rs = new RecordSet();
         int layoutid = 0;
        if(card.isVirtualForm()){
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setModeId(card.getModeId());
            modeRightInfo.setType(card.getType());
            modeRightInfo.setUser(user);
            modeRightInfo.setAuthorizeOpttype(card.getAuthorizeOpttype());
            modeRightInfo.setAuthorizeLayoutid(card.getAuthorizeLayoutid());
            modeRightInfo.setAuthorizeLayoutlevel(card.getAuthorizeLayoutlevel());
            layoutid =  modeRightInfo.getVirLayoutIdOfUserRight(card.getType());
        }else{
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setModeId(card.getModeId());
            modeRightInfo.setType(card.getType());
            modeRightInfo.setUser(user);
            layoutid = modeRightInfo.getLayoutid(user, card.getModeId(),card.getType(), card.getBillId(),card.getAuthorizeLayoutlevel(), card.getAuthorizeLayoutid());
        }
        if(layoutid==0){
            //有授权取授权布局
            if(Util.getIntValue(card.getAuthorizeOpttype(),-1)>-1){
                layoutid = Util.getIntValue(card.getAuthorizeLayoutid(),0);
            }
        }
        if(layoutid==0){
        	checkRight(card, user);
        	if (card.isEdit()) {
        		rs.executeQuery("select * from modehtmllayout where modeid=? and type=? and formId=? and isdefault=1", card.getModeId(), card.getType(), card.getFormId());
			}
        }
//            rs.executeQuery("select * from modehtmllayout where id= ? ",layoutid);
//            //判断布局是否被删除，如果被删除，则查默认布局
//            if(!rs.next()){
//                rs.executeQuery("select * from modehtmllayout where modeid=? and type=? and formId=? and isdefault=1", card.getModeId(), type, card.getFormId());
//            }
//        }
//        rs.beforFirst();
        if(rs.next()){
            layoutid = Util.getIntValue(Util.null2String(rs.getInt("id")), 0);
        }
        card.setType(temptype);
        return layoutid;
    }

    public static boolean isShowReply(int modeId, Map<String, String> replayModeInfo){
        ModeComInfo modeComInfo = new ModeComInfo();
        if("1".equals(modeComInfo.getIsAllowReply(""+modeId))) {
            RecordSet rs = new RecordSet();
            rs.execute("select * from formEngineSet");
            if(rs.next()) {
                String replayModeId = rs.getString("modeid");
                replayModeInfo.put("id", replayModeId);
                replayModeInfo.put("formid", modeComInfo.getFormId(replayModeId));
                replayModeInfo.put("layoutid","0");
                return "1".equals(rs.getString("isEnFormModeReply"));
            }
        }
        return false;
    }
}
