package weaver.interfaces.htsc.yghmzczysq;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * 这个action是用来涨乐申请流程结束后，需要将数据写入数据中心数据库表white_list
 */
public class WriteToDataCenter extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        try{

            RecordSet rs = new RecordSet();
            RecordSetDataSource zysqRs = new RecordSetDataSource("zysq");
            String requestid = requestInfo.getRequestid();
            String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
//            String requestid= "5137210";
//            String tablename = "formtable_main_474";
            String sql1 = "select * from "+ tablename + " where requestid = " +requestid;
            rs.execute(sql1);
            rs.next();
            String ngr = rs.getString("ngr");//拟稿人
            String thatline = rs.getString("sqsx");//申请事项
            String workflownum = rs.getString("lcbh");//流程编号
            String lxfs = rs.getString("lxfs");//联系方式
            String zcsjh = rs.getString("zcsjh");//注册手机号
            if(Objects.equals(lxfs,zcsjh)){
                //通过拟稿人的id查询workcode
                String getCodeByIdSql = "select * from hrmresource where id = " +  ngr;
                rs.execute(getCodeByIdSql);
                rs.next();
                String badge = rs.getString("workcode");//工号业务code，非泛微人员id
                String workflowname = "ZhangYueApply";
                String effective = "开通";
                String begintime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                RecordSetDataSource rsd=new RecordSetDataSource("zysq");//涨乐申请数据库
                String insertSql = "insert into white_list (id,badge,telphone,thatline,workfflowname,workflownum,effective,begintime) values( white_list_seq.nextval,'"
                        +badge+"','"+lxfs+"','"+thatline+"','"+workflowname+"','"+workflownum+"','"+effective+"','"+begintime+"')";
                bb.writeLog("流程办结后，插入数据sql="+insertSql);
                System.out.println(insertSql);
                zysqRs.execute(insertSql);
                bb.writeLog("插入成功");
            }else{
                //通过拟稿人的id查询workcode
                String getCodeByIdSql = "select * from hrmresource where id = " +  ngr;
                rs.execute(getCodeByIdSql);
                rs.next();
                String badge = rs.getString("workcode");//工号业务code，非泛微人员id
                String workflowname = "ZhangYueApply";
                String effective = "开通";
                String begintime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                RecordSetDataSource rsd=new RecordSetDataSource("zysq");//涨乐申请数据库
                String lxfsSql = "insert into white_list (id,badge,telphone,thatline,workfflowname,workflownum,effective,begintime) values( white_list_seq.nextval,'"
                        +badge+"','"+lxfs+"','"+thatline+"','"+workflowname+"','"+workflownum+"','"+effective+"','"+begintime+"')";
                bb.writeLog("流程办结后，插入数据sql="+lxfsSql);
                System.out.println(lxfsSql);
                zysqRs.execute(lxfsSql);
                bb.writeLog("插入成功");
                String zcsjhSql = "insert into white_list (id,badge,telphone,thatline,workfflowname,workflownum,effective,begintime) values( white_list_seq.nextval,'"
                        +badge+"','"+zcsjh+"','"+thatline+"','"+workflowname+"','"+workflownum+"','"+effective+"','"+begintime+"')";
                bb.writeLog("流程办结后，插入数据sql="+zcsjhSql);
                System.out.println(zcsjhSql);
                zysqRs.execute(zcsjhSql);
                bb.writeLog("插入成功");
            }



        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("插入失败"+e.getMessage());
            bb.writeLog("插入失败"+e.toString());
        }
        return Action.SUCCESS;
    }
}
