package weaver.interfaces.htsc.wdzx;

import org.apache.commons.lang3.StringUtils;
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.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

public class ModeSaveUser extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String,String> result  =  new HashMap<String,String>();
        try{

            RecordSet rs = new RecordSet();
            int billid = -1;
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            User user = (User)param.get("user");
            if(requestInfo != null ){
                billid = Util.getIntValue(requestInfo.getRequestid());
                if(billid>0){
                    String users = "select * from uf_wdzx where id = " + billid;
                    JAVATools.SetCmdwritelog("modesaveuser billid====" + billid);
                    rs.execute(users);
                    rs.next();
                    String gly = rs.getString("gly");
                    JAVATools.SetCmdwritelog("modesaveuser gly ====" + gly);
                    String cjr = rs.getString("cjr");
                    JAVATools.SetCmdwritelog("modesaveuser cjr ====" + cjr);
                    StringBuffer glybuf = new StringBuffer();
                    StringBuffer cjrbuf = new StringBuffer();
                    String glysql = "select * from hrmresource where workcode in (select workcode  from hrmresource where id in ("+gly+"))";
                    JAVATools.SetCmdwritelog("modesaveuser glysql ====" + glysql);
                    rs.execute(glysql);
                    while(rs.next()){
//                        String loginid = rs.getString("loginid");
                        String loginid = rs.getString("id");
                        glybuf.append(loginid).append(",");
                    }
                    String glyfzzd =null;
                    if(glybuf !=null&& StringUtils.isNotBlank(glybuf.toString())){
                        glyfzzd = glybuf.toString().substring(0,glybuf.lastIndexOf(","));
                    }

                    JAVATools.SetCmdwritelog("modesaveuser glyfzzd ====" + glyfzzd);
                    String cjrsql = "select * from hrmresource where workcode in (select workcode  from hrmresource where id in ("+cjr+"))";
                    rs.execute(cjrsql);
                    while(rs.next()){
//                        String loginid = rs.getString("loginid");
                        String loginid = rs.getString("id");
                        cjrbuf.append(loginid).append(",");
                    }
                    String cjrfzzd = cjrbuf.toString().substring(0,cjrbuf.lastIndexOf(","));
                    JAVATools.SetCmdwritelog("modesaveuser cjrfzzd ====" + cjrfzzd);
                    String update = "update uf_wdzx set glyfzzd = '"+glyfzzd+"',cjrfzzd  =  '"+ cjrfzzd +"' where id = " + billid;
                    JAVATools.SetCmdwritelog("modesaveuser update ====" + update);
                    rs.execute(update);
                    result.put("flag", "true");
                }
            }
            return result;
        }catch (Exception e ){
            result.put("flag", "false");
            return result;
        }

    }

}
