package weaver.interfaces.htsc.sfxzdj;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SfxzdjModeExpandTemplate extends AbstractModeExpandJavaCodeNew {
    /**
     * 执行模块扩展动作
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User)param.get("user");
            JAVATools.writelog("司法协助查询name"+user.getLastname());
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RecordSet rs = new RecordSet();
            RecordSet updaters = new RecordSet();
            ArrayList<String> list = new ArrayList<String>();
            PropBean pb = new PropBean();
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            JAVATools.writelog("司法协助查询requestInfo"+(requestInfo == null));
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                JAVATools.writelog("司法协助查询billid"+billid);
                JAVATools.writelog("司法协助查询modeid"+modeid);
                if(billid>0&&modeid>0){
                    //------请在下面编写业务逻辑代码------
                    JAVATools.writelog("司法协助查询billid"+billid);
                    JAVATools.writelog("司法协助查询modeid"+modeid);
                    String sql = "select * from uf_zzhgry where id = '"+billid+"'";
                    rs.execute(sql);
                    rs.next();
                    String hgry = rs.getString("hgry");
                    String sfxzdj_roleId = pb.getPropName("sfxzdj_roleId");
                    RecordSet rolers = new RecordSet();
                    //判断当前新增或修改的人员是不是已经存在在权限人员关联表
//                    String selectSql = "select * from hrmrolemembers where resourceid = '" +hgry + "' and roleid = '"+sfxzdj_roleId+"'";
                    boolean flag = false;
//                    rolers.execute(selectSql);
//                    while(rolers.next()){
//                        flag=true;
//                    }

                    sql = "select * from uf_zzhgry";
                    rs.execute(sql);
                    while(rs.next()){
                        list.add(rs.getString("hgry"));
                    }
                    //不存在则插入
//                    if(!flag){
//
//                        String str = UUID.randomUUID().toString();
//                        String appsecret = str.trim().replaceAll("-","").toUpperCase();
//                        String insertSql = "insert into hrmrolemembers  (roleid,resourceid,rolelevel,resourcetype,uuid) values ('"+sfxzdj_roleId+"','"+hgry+"','2','1','"+appsecret+"')";
//                        JAVATools.writelog("司法协助查询insertSql"+insertSql);
//                        rs.execute(insertSql);
//
//                    }
                    //更新人员权限表
                    for (String userId:list) {
                        String updateSql = "select * from hrmrolemembers where resourceid = '" +userId + "' and roleid = '"+sfxzdj_roleId+"'";
                        flag = false;
                        updaters.execute(updateSql);
                        while(updaters.next()){
                            flag=true;
                        }
                        if(!flag){
                            String str = UUID.randomUUID().toString();
                            String appsecret = str.trim().replaceAll("-","").toUpperCase();
                            String insertSql = "insert into hrmrolemembers  (roleid,resourceid,rolelevel,resourcetype,uuid) values ('"+sfxzdj_roleId+"','"+userId+"','2','1','"+appsecret+"')";
                            JAVATools.writelog("司法协助查询insertSql"+insertSql);
                            rs.execute(insertSql);
                        }
                    }
                }
            }
            result.put("flag", "true");
        } catch (Exception e) {
            JAVATools.writelog("司法协助查询"+e.getMessage());
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}
