package com.engine.htsc.seal.web;

import  com.weaver.general.Util;
import jxl.read.biff.Record;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

public class SignStatusutil {


    /**
     * 如果是用印方式是自动用印的话，判读有写入的选项
     * */
    public  void SetSussmage(String tablename,String detail,String dzyyfs,String docid,String reqid,int zzyy){
        synchronized(SignStatusutil.class) {
            JAVATools.SetCmdwritelog("查看用印参数", "tablename:" + tablename + " detail:" + detail + " dzyyfs:" + dzyyfs + " docid:" + docid + " reqid:" + reqid + " zzyy:" + zzyy);
            RecordSet rs = new RecordSet();

            boolean flag = false;
            if ("0".equals(dzyyfs)) {
                //首先判断一下防伪件有没有传参，如果有
                flag = isManualauto(tablename, detail, "1");
            } else if ("1".equals(dzyyfs)) {
                flag = isManualauto(tablename, detail, "0");
            }
            String sqlvalue = "";
            //只需要写入当前字段值
            boolean isboolean = isyywjisnull(reqid, detail, docid);
            // if(flag){
            //另外一个是非成功，只需要改变自己的状态
            if ("0".equals(dzyyfs)) {
                if (isboolean) {
                    sqlvalue = "update " + tablename + "_dt1" + " set dzjyyzt=0,yyhwj='" + zzyy + "' where id=" + detail;
                } else {
                    sqlvalue = "update " + tablename + "_dt1" + " set dzjyyzt=0,yyhwj=yyhwj||','||'" + zzyy + "' where id=" + detail;
                }
            } else {
                if (isboolean) {
                    sqlvalue = "update " + tablename + "_dt1" + " set fwjyyzt=0,yyhwj='" + zzyy + "' where id=" + detail;
                } else {
                    sqlvalue = "update " + tablename + "_dt1" + " set fwjyyzt=0,yyhwj=yyhwj||','||'" + zzyy + "' where id=" + detail;
                }
            }
            JAVATools.SetCmdwritelog("查看sql是否为空：" + sqlvalue);
            if (!"".equals(sqlvalue)) {
                JAVATools.SetCmdwritelog("查看sql：" + sqlvalue);
                rs.execute(sqlvalue);
            }
            // }
            if (!flag) {
                //将数据保存到主表中
                String formcount = "";
                String SqlmainUpdate = "";
                String sqlmainvalue = "select count(*) as formcount from  " + tablename + " " + " where dzyyj is not null and requestid=" + reqid;
                JAVATools.SetCmdwritelog("sqlmain是:" + sqlmainvalue + "req:" + reqid);
                rs.execute(sqlmainvalue);
                if (rs.next()) {
                    formcount = com.weaver.general.Util.null2String(rs.getString("formcount"));
                }
                String dzyyj = JAVATools.GetDetailfield(tablename + "_dt1", detail, "yyhwj");
                JAVATools.SetCmdwritelog("查看电子用印件值", "dzyyj:" + dzyyj);
                if ("".equals(dzyyj)) {
                    dzyyj = String.valueOf(zzyy);
                }
                if ("".equals(formcount) || "0".equals(formcount)) {
                    SqlmainUpdate = "update " + tablename + " set dzyyj='" + dzyyj + "' where requestid=" + reqid;
                } else {
                    SqlmainUpdate = "update " + tablename + " set dzyyj=dzyyj||','||'" + dzyyj + "' where requestid=" + reqid;
                }
                JAVATools.SetCmdwritelog("sqlmainupdate是:" + SqlmainUpdate + "req:" + reqid);
                rs.execute(SqlmainUpdate);

                String sqltzvalue = "update " + tablename + "_dt1" + " set yyzt=3 where id=" + detail;
                JAVATools.SetCmdwritelog("最后修改状态:" + sqltzvalue + "detail:" + detail);
                rs.execute(sqltzvalue);
            }
        }

      //  return  flag;
    }



    public  void SetMmainable(String reqid,String detailid,int zzyy,String docid){
        synchronized(SignStatusutil.class) {
            boolean flag = true;
            BaseBean bbbasebean = new BaseBean();
            try {
                RecordSet rs = new RecordSet();
                String formid = JAVATools.getMaintable(reqid);
                boolean isboolean = isyywjisnull(reqid, detailid, docid);
                String sqlvalue = "";
                if (isboolean) {
                    sqlvalue = "update " + formid + "_dt1" + " set yyzt=3,yyhwj='" + zzyy + "' where id=" + detailid;
                } else {
                    sqlvalue = "update " + formid + "_dt1" + " set yyzt=3, yyhwj=yyhwj||','||'" + zzyy + "' where id=" + detailid;
                }
                bbbasebean.writeLog("用印调用反馈回来的数据是ypfk:" + sqlvalue);
                rs.execute(sqlvalue);

                String formcount = "";
                String sqlupdate = "select count(*) as formcount from  " + formid + " " + " where dzyyj is not null and requestid=" + reqid;
                JAVATools.SetCmdwritelog("sqlmain是:" + sqlupdate + "req:" + reqid);
                rs.execute(sqlupdate);
                if (rs.next()) {
                    formcount = weaver.general.Util.null2String(rs.getString("formcount"));
                }
                if ("".equals(formcount) || "0".equals(formcount)) {
                    sqlupdate = "update " + formid + " set dzyyj='" + zzyy + "' where requestid=" + reqid;
                } else {
                    sqlupdate = "update " + formid + " set dzyyj=dzyyj||','||'" + zzyy + "' where requestid=" + reqid;
                }
                JAVATools.SetCmdwritelog("sqlmainupdate是:" + sqlupdate + "req:" + reqid);
                rs.execute(sqlupdate);

            } catch (Exception exo) {
                flag = false;
                bbbasebean.writeLog("用印调用反馈报错ypfkbc:" + exo.getMessage());
            }
        }
      //  return flag;
    }



    //

    public static boolean isAutoSingetType(String tablename,String detail){
        boolean flag=false;
        RecordSet rs=new RecordSet();
        String sql="select yyfs from "+tablename+"_dt1 where id="+detail;
        rs.execute(sql);
        if(rs.next()){
            String yyfs=  Util.null2String(rs.getString("yyfs"));
            if("3".equals(yyfs)){
                flag=true;
            }
        }
        return  flag;
    }

    /**
     * 如果是手动触发全路径的话
     * */
    public  static void UpdateSignError(String tablename,String dzyyfs,String detailid,String status){

        RecordSet rs=new RecordSet();
        String  sqlvalue="";
        if ("0".equals(dzyyfs)) {
            sqlvalue = "update " + tablename+"_dt1" + " set dzjyyzt=? where id=" + detailid;

        }else if("1".equals(dzyyfs)){
            sqlvalue = "update " + tablename+"_dt1" + " set fwjyyzt=? where id=" + detailid;
        }
          rs.executeQuery(sqlvalue,status);
    }

    public static boolean isyywjisnull(String reqid,String detailid,String wjmc){
        RecordSet rs = new RecordSet();
        String formid =JAVATools.getMaintable(reqid);
        String formcount="";
        String sqlvalue = "select count(*) as formcount from  " + formid +"_dt1 " +" where yyhwj is not null and id=" + detailid ;
        new BaseBean().writeLog("sql是:"+sqlvalue+"req:"+reqid+"detailid"+detailid);
        rs.execute(sqlvalue);
        if (rs.next()) {
            formcount = com.weaver.general.Util.null2String(rs.getString("formcount"));
        }
        if("".equals(formcount)||"0".equals(formcount)){
            return  true;
        }
        return  false;
    }


    //   //判断有没有成功的，如果是重新发送那么成功的则不送用印
    public static Boolean isSendManualauto(String tablename,String detailid,String dzyyfs){
        RecordSet rs=new RecordSet();
        boolean flag=false;
        String sql="select * from "+tablename+"_dt1 where id="+detailid;
        JAVATools.SetCmdwritelog("用印登记单","查看手动触发的流程："+sql);
        rs.execute(sql);
        String dzjyyzt="";
        String fwjyyzt="";
        if(rs.next()){
            dzjyyzt= rs.getString("dzjyyzt");
            fwjyyzt= rs.getString("fwjyyzt");
        }
        if("0".equals(dzyyfs)){

            //如果是0表示成功
            if(!"0".equals(dzjyyzt)&&!"1".equals(dzjyyzt)){
                flag=true;
            }

        }else if("1".equals(dzyyfs)){

            if(!"0".equals(fwjyyzt)&&!"1".equals(fwjyyzt)){
                flag=true;
            }

        }
        return  flag;
    }




    //   //判断有没有成功的，如果是重新发送那么成功的则不送用印
    public static Boolean isManualauto(String tablename,String detailid,String dzyyfs){
        RecordSet rs=new RecordSet();
        boolean flag=true;
        String sql="select * from "+tablename+"_dt1 where id="+detailid;
        JAVATools.SetCmdwritelog("用印登记单","查看手动触发的流程："+sql);
        rs.execute(sql);
        String dzjyyzt="";
        String fwjyyzt="";
        if(rs.next()){
            dzjyyzt= rs.getString("dzjyyzt");
            fwjyyzt= rs.getString("fwjyyzt");
        }
        if("0".equals(dzyyfs)){

            //如果是0表示成功
            if("0".equals(dzjyyzt)){
                flag=false;
            }
        }else if("1".equals(dzyyfs)){
            if("0".equals(fwjyyzt)){
                flag=false;
            }
        }
        return  flag;
    }


    public String IsParper(String tablename,String detail){
        String dzyyfs="";
        RecordSet rs=new RecordSet();
        String sql="select dzyyfs from "+tablename+"_dt1 where id="+detail;
        rs.execute(sql);
        if(rs.next()){
             dzyyfs=  Util.null2String(rs.getString("yyfs"));

        }
        return  dzyyfs;
    }


    //判断非公文电子用印件是否成功
    public static  boolean  ISsucess(String tablename,String id){
        boolean flag=false;
        RecordSet rs=new RecordSet();
        String sumnumber="";
        String sql="select cont(*) as counnumber from "+tablename+"_dt1 where yyzt=3 and id=?";
        rs.executeQuery(sql,id);
        if(rs.next()){
            sumnumber= rs.getString("counnumber");
        }
        if(!"0".equals(sumnumber)){
            flag=true;
        }
        return  flag;
    }



    public static boolean IsPAPERSucess(String table,String detailid,String dzyyfs){
        RecordSet rs=new RecordSet();
        String value="";
        boolean flag=false;
        String sql="";
        if("0".equals(dzyyfs)){
            sql="select count(*) as counumber from formtable_main_158_dt1  where  dzjyyzt=0 and id=?";
        }else if("1".equals(dzyyfs)){
            sql="select count(*) as counumber from formtable_main_158_dt1  where  fwjyyzt=0 and id=?";
        }
        rs.executeQuery(sql,detailid);
        if(rs.next()){
            value=rs.getString("counumber");
        }
        if(!"0".equals(value)){
            flag=true;
        }

        return flag;
    }

}
