package weaver.interfaces.zjrb.mode;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

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

public class ExaminationPaperModeExpand2Runnable implements Runnable{
    private String ksfw;
    private String billid;
    public ExaminationPaperModeExpand2Runnable(String ksfw,String billid){
        this.ksfw = ksfw;
        this.billid = billid;
    }

    @Override
    public void run() {

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();

        ResourceComInfo resourceComInfo = null;
        try {
            resourceComInfo = new ResourceComInfo();
            if(StringUtils.isNotBlank(ksfw)){
                Map<String, Map<String,String>> userMap = new HashMap<String,Map<String,String>>();
                String sql = " select id,zgs,fgs,sgs from picc_hrm_org_path_view";
                rs.executeQuery(sql);
                while (rs.next()){
                    String id = Util.null2String(rs.getString("id"));
                    String zgs = Util.null2String(rs.getString("zgs"));
                    String fgs = Util.null2String(rs.getString("fgs"));
                    String sgs = Util.null2String(rs.getString("sgs"));
                    Map<String,String> gsMap = new HashMap<String,String>();
                    gsMap.put("zgs",zgs);
                    gsMap.put("fgs",fgs);
                    gsMap.put("sgs",sgs);
                    userMap.put(id,gsMap);
                }

                sql = " delete from uf_ks_kjb_dt2 where mainid = "+billid ;
                boolean isTrue = rs.executeUpdate(sql);
                if(isTrue){
                    String[] userids = ksfw.split(",");
                    bb.writeLog("userids:"+userids.length);
                    List<List> batchList = new ArrayList<>(); //存放每条数据更新时需要的参数
                    for(int i=0;i<userids.length;i++){
                        String userid = userids[i];
                        String deptid = "" ;
                        if(StringUtils.isNotBlank(userid)){
                            deptid = resourceComInfo.getDepartmentID(userid);
                        }

                        String zgs = "";
                        String fgs = "";
                        String sgs = "";
                        if(userMap.containsKey(userid)){
                            Map<String,String> gsMap =userMap.get(userid);
                            if(gsMap.containsKey("zgs")){
                                zgs = gsMap.get("zgs");
                            }
                            if(gsMap.containsKey("fgs")){
                                fgs = gsMap.get("fgs");
                            }
                            if(gsMap.containsKey("sgs")){
                                sgs = gsMap.get("sgs");
                            }
                        }

                        List<String> userList = new ArrayList<String>();
                        userList.add(billid);
                        userList.add(userid);
                        userList.add(deptid);
                        userList.add("0");
                        userList.add(zgs);
                        userList.add(fgs);
                        userList.add(sgs);
                        batchList.add(userList);
                    }
                    bb.writeLog("batchList:"+batchList.size());
                    if(batchList.size()>0){
                        sql = " insert into uf_ks_kjb_dt2(mainid,ks,ksssbm,zt,kssszgs,ksssfgs,kssssgs) values(?,?,?,?,?,?,?)";
                        boolean isTrue2 = rs.executeBatchSql(sql,batchList);
                        bb.writeLog("isTrue2:"+isTrue2);
                    }
                }
            }
        }catch (Exception e){

        }
    }
}
