package weaver.interfaces.htsc.sfxzdj;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

public class WorkflowEndingWriteCenter extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestinfo) {
        RecordSet rs = new RecordSet();

        RecordSet dt4rs = new RecordSet();
        String requestid = requestinfo.getRequestid();//请求ID
        JAVATools.writelog("办结写数据");
        String workflowid = requestinfo.getWorkflowid(); //流程ID
        String tablename = requestinfo.getRequestManager().getBillTableName();//表单名称
        int billid = requestinfo.getRequestManager().getBillid();//表单数据ID
        JAVATools.writelog("司法协助写数据billid"+billid);
        int formid = requestinfo.getRequestManager().getFormid();

//
//        String requestid = "5706734";//请求ID
//        JAVATools.writelog("会签部门触发流程action开始");
//        String workflowid = requestinfo.getWorkflowid(); //流程ID
//        String tablename = "formtable_main_518";//表单名称
//        int billid = -518;//表单数据ID


//        User usr = requestinfo.getRequestManager().getUser();//获取当前操作用户对象
        String sql = "select * from "+ tablename + " where requestid = '"+requestid+"'";
        rs.execute(sql);
        rs.next();
        String id = rs.getString("id");//流程编号(OA)
        String lcbh = rs.getString("lcbh");//流程编号(OA)
        String sfcxsx = rs.getString("sfcxsx");//司法协助事项
        String cxjg = rs.getString("cxjg");//司法机关
        String wsh = rs.getString("wsh");//文书号
        String zfryxm = rs.getString("zfryxm");//执法人员姓名
        String zfrylxfs = rs.getString("zfrylxfs");//执法人员联系方式
        String bz = rs.getString("bz"); //备注
        String cxnr = rs.getString("cxnr");//申请事项
        String ngr = rs.getString("ngr");
        String sfjglxValue = rs.getString("sfjglx");
        String xzcxxzValue = rs.getString("xzcxxz");
        RecordSet hrmRs = new RecordSet();
        String hrmSql = "select * from hrmresource where id = " + ngr;
        hrmRs.execute(hrmSql);
        hrmRs.next();
        String departmentid = hrmRs.getString("departmentid");
        String loginid = hrmRs.getString("loginid");//拟稿人   	//拟稿人OA登录名
        String deptSql = "select * from  HRMDEPARTMENT where id = "+departmentid;
        hrmRs.execute(deptSql);
        hrmRs.next();
        String departmentname = hrmRs.getString("departmentname");//拟稿人部门名称
        String ngrq = rs.getString("ngrq");				//拟稿日期
        String xzcxsj = rs.getString("xzcxsj");		//司法查询时间
        RecordSet billRs = new RecordSet();
        String billSql = "select * from WORKFLOW_BILLFIELD where  billid = '"+formid+"' and fieldname = 'sfjglx'";
        billRs.execute(billSql);
        billRs.next();
        String sfjglx = billRs.getString("id");//司法机关类型
//        billSql = "select * from WORKFLOW_BILLFIELD where  billid = '"+billid+"' and selectvalue = '"+sfjglx+"'";
        billSql ="select * from workflow_selectitem where fieldid = "+sfjglx+" and selectvalue ='"+sfjglxValue+"'";
        billRs.execute(billSql);
        billRs.next();
        sfjglx = billRs.getString("selectname");//司法机关类型
        billSql = "select * from WORKFLOW_BILLFIELD where  billid = '"+formid+"' and fieldname = 'xzcxxz'";
        billRs.execute(billSql);
        billRs.next();
        String xzcxxz = billRs.getString("id");//司法协查性质
//        billSql = "select * from WORKFLOW_BILLFIELD where  billid = '"+billid+"' and selectvalue = '"+xzcxxz+"'";
        billSql ="select * from workflow_selectitem where fieldid = "+xzcxxz +" and selectvalue = '"+xzcxxzValue+"'";
        billRs.execute(billSql);
        billRs.next();
        xzcxxz = billRs.getString("selectname");//司法协查性质
        RecordSetDataSource write = new RecordSetDataSource("sfxzdj_write_data");
        sql = "select * from "+ tablename + "_dt4 where mainid = " +id;
        String mainInsertSql = "insert into judicature  (judid,JudicialMatters,JudicialOffice,documentNo,lawName,lawTelephone,matters,remark,workflowNum,APPLICANT,APPLICANTDEPARTMENT,APPLICATIONTIME,JUDICIALOOFFICE_LX,JUDICIALCOORDINATION_XZ,JUDICIALINQUIRIES_TIME) values " +
                "( jud_seq.nextval,'"+sfcxsx+"','"+cxjg+"','"+wsh+"','"+zfryxm+"','"+zfrylxfs+"','"+cxnr+"','"+bz+"','"+lcbh+"','"+loginid+"','"+departmentname+"','"+ngrq+"','"+sfjglx+"','"+xzcxxz+"','"+xzcxsj+"')";

        write.execute(mainInsertSql);
        rs.execute(sql);
        String dt4FieldSql ="select * from WORKFLOW_BILLFIELD where  billid = '"+formid+"' and fieldname = 'cxdxzhlx'";
        JAVATools.writelog("司法协助写数据dt4FieldSql"+dt4FieldSql);
        dt4rs.execute(dt4FieldSql);
        dt4rs.next();
        String dt4FieldId = dt4rs.getString("id");
        JAVATools.writelog("司法协助写数据dt4FieldId"+dt4FieldId);
        while(rs.next()){
            String cxdxxmmc = rs.getString("cxdxxmmc");
            String cxdxzhlx = rs.getString("cxdxzhlx");
            billSql ="select * from workflow_selectitem where fieldid = "+dt4FieldId +" and selectvalue = '"+cxdxzhlx+"'";
            JAVATools.writelog("司法协助写数据billSql"+billSql);
            dt4rs.execute(billSql);
            dt4rs.next();
            cxdxzhlx = dt4rs.getString("selectname");
            JAVATools.writelog("司法协助写数据cxdxzhlx"+cxdxzhlx);
            String zhhm = rs.getString("zhhm");
            String insetSql = "insert into custominfo (cusid,cusname,accountType,accountNum,workflowNum)values (cus_seq.nextval ,'"+cxdxxmmc+"','"+cxdxzhlx+"','"+zhhm+"','"+lcbh+"')";
            write.execute(insetSql);
        }
        return Action.SUCCESS;
    }
}
