package weaver.interfaces.htsc.wdzx;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocImageManager;
import weaver.docs.docs.VersionIdUpdate;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.util.UploadDocUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.wps.officed.AcceptRevision;

import java.util.*;


public class DocumentCenterAction  extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            String requestid = requestInfo.getRequestid();
            //表单名称
            String tablename = requestInfo.getRequestManager().getBillTableName();
            //查询主表
            String sql1 = "select * from "+ tablename + " where requestid = " +requestid;
            RecordSet rs1 = new RecordSet();
            rs1.execute(sql1);
            rs1.next();
            String mianid = rs1.getString("id");
            //拟稿人
            String ngr = rs1.getString("ngr");
            //操作类型
            String czlx = rs1.getString("czlx");
            //业务场景
            String ywcj = rs1.getString("ywcj");
            //查询明细表
            //0:文件入库 1：文件变更 2：文件出库
            RecordSet upzt = new RecordSet();
            RecordSet rsblank = new RecordSet();
            RecordSet rs3 = new RecordSet();
            RecordSet rs = new RecordSet();
            RecordSet gllrs =  new RecordSet();
            RecordSet rs8 = new RecordSet();
            if(Objects.equals("0",czlx)){
                String sql2 = "select * from "+ tablename + "_DT1 where mainid = " + mianid;
                RecordSet rs2 = new RecordSet();
                rs2.execute(sql2);
                while(rs2.next()){
                    //从明细表中获取字段判断是否选择已有模板 sfxzyymb 1是选择 0未选择
                    String dt1Id= rs2.getString("id");
                    //是否选择已有模板
                    String sfxzyymb = rs2.getString("sfxzyymb");
                    //文件
                    String xwj = rs2.getString("xwj");
                    xwj = qingGao(xwj);
                    JAVATools.SetCmdwritelog("变更清稿文件后的id="+xwj);
                    String qgid = rs2.getString("id");
                    String updateQgWjSql = "update " + tablename+ "_dt1 set qgwj = '"+xwj+"' where id = "+qgid;
                    JAVATools.SetCmdwritelog("变更清稿文件sql="+xwj);
                    rs2.execute(updateQgWjSql);
                    //版本号
                    String bbh = rs2.getString("bbh");
                    //生效日期
                    String sxrq = rs2.getString("sxrq");
                    //失效日期
                    String sxrq1 = rs2.getString("sxrq1");
                    //模板名称
                    String mbmc = rs2.getString("mbmc");
                    //模板名称1
                    String mbmc1 = rs2.getString("mbmc1");
                    //模板描述
                    String mbms = rs2.getString("mbms");
                    //有权使用系统
                    String yqsyxt = rs2.getString("yqsyxt");
                    //有权使用系统
                    String yqsyxtfx = rs2.getString("yqsyxtfx");
                    String gly = rs2.getString("gly");
                    Date date = new Date();
                    DateTime parse = null ;
                    String zt= null ;
                    if(StringUtils.isBlank(sxrq1)){
                        parse = DateUtil.parse(sxrq);
                        zt = "0";
                        DateUtil.today();
                        if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                            zt ="1";
                        }
                    }else{
                        parse = DateUtil.parse(sxrq);
                        DateTime parse1 = DateUtil.parse(sxrq1);
                        zt = "0";
                        if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                            zt ="1";
                            //12-01当生效时间和失效时间相等算失效
                            if(DateUtil.between(parse1, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>0 || DateUtil.between(parse,parse1, DateUnit.DAY,false)==0 ){
                                zt ="2";
                            }
                        }

                    }
                    if(Objects.equals("1",sfxzyymb)){
                        //将明细表的数据更新到建模表中
                        String uuid = UUID.randomUUID().toString();
                        //旧插入新的明细与旧明细生效做对比 12-03
                        String upztsql = "select * from uf_wdzx_dt1 where mainid = "+ mbmc + " and zt = 1";
                        upzt.execute(upztsql);
                        upzt.next();
                        String  ztsxrq1 = upzt.getString("sxrq1");
                        String  ztsxrq = upzt.getString("sxrq");
                        if(StringUtils.isNotBlank(upzt.getString("id"))){
                            String id = upzt.getString("id");
                            if(Objects.equals("1",zt) ){
                                if(StringUtils.isBlank(ztsxrq1) && DateUtil.between(DateUtil.parse(ztsxrq), DateUtil.parse(sxrq), DateUnit.DAY,false)>=0 ){
                                    String upblank = "update uf_wdzx_dt1 set zt = 2 , sxrq1 = '"+upzt.getString("sxrq")+"' where id = "+id ;
                                    rsblank.execute(upblank);
                                }else if(StringUtils.isNotBlank(ztsxrq1) && DateUtil.between(DateUtil.parse(ztsxrq1), DateUtil.parse(sxrq), DateUnit.DAY,false)<=0  ){
                                    String upblank = "update uf_wdzx_dt1 set zt = 2 , sxrq1 = '"+upzt.getString("sxrq")+"' where id = "+id;
                                    rsblank.execute(upblank);
                                }else{
                                    String upblank = "update uf_wdzx_dt1 set zt = 2  where id = "+id;
                                    rsblank.execute(upblank);
                                }

                            }else{
                                if(!StringUtils.isBlank(ztsxrq1)){
                                    if(DateUtil.between(DateUtil.parse(upztsql), DateUtil.parse(sxrq), DateUnit.DAY,false)<=0){
                                        String upblank = "update uf_wdzx_dt1 set sxrq1 = '"+upzt.getString("sxrq")+"' where id = "+id;
                                        rsblank.execute(upblank);
                                    }
                                }
                            }
                        }
                        //插入建模明细表的数据
                        HashMap<String, String> map1 = new HashMap<>();
                        map1.put("uuid",uuid);
                        map1.put("mainid",mbmc);
                        map1.put("bbh",bbh);
                        map1.put("wj",xwj);
                        map1.put("cjr",ngr);
                        map1.put("cjsj", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                        map1.put("sxrq",sxrq);
                        map1.put("sxrq1",sxrq1);
                        map1.put("zt",zt);
                        map1.put("gllc",requestid);
                        JAVATools.Excetesql(map1,"uf_wdzx_dt1");
                        String dt1 = "select * from uf_wdzx_dt1 where uuid = '"+uuid+"'";
                        RecordSet rsdt1 = new RecordSet();
                        rsdt1.execute(dt1);
                        rsdt1.next();
                        String wjcjsj = rsdt1.getString("cjsj");

                        //更新建模主表的数据
                        if(StringUtils.isBlank(sxrq1)){
                            parse = DateUtil.parse(sxrq);
                            if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                                String sql3 = "update uf_wdzx set bb = '"+bbh.substring(1)+"',cjsj = '"+ DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")+"', wj = '"+xwj+ "' ,wjcjsj= '"+ wjcjsj+"' ,sxrq= '"+sxrq +"', yqsyxtfx = '"+yqsyxtfx + "',gly='"+gly+"',mbms='"+mbms+"',yqsyxt='"+yqsyxt+"',sxrq1='"+sxrq1+"' where id = " + mbmc;
                                try{
                                    rs3.execute(sql3);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        }else{
                            parse = DateUtil.parse(sxrq);

                            if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                                String sql3 ="update uf_wdzx set bb = '"+bbh.substring(1)+"',cjsj = '"+ DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")+"',wj = '"+xwj+ "' ,wjcjsj= '"+ wjcjsj+"' ,sxrq= '"+sxrq +"', yqsyxtfx = '"+yqsyxtfx + "',gly='"+gly+"',mbms='"+mbms+"',yqsyxt='"+yqsyxt+"',sxrq1='"+sxrq1+"' where id = " + mbmc;
                                try{
                                    rs3.execute(sql3);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }

                            }
                        }
                        //更新建模主表的状态
                        String ztsqsl = "select * from uf_wdzx_dt1 where mainid = " + mbmc;
                        RecordSet ztrs = new RecordSet();
                        ztrs.execute(ztsqsl);
                        ArrayList<String> list = new ArrayList<>();
                        while(ztrs.next()){
                            list.add(ztrs.getString("zt"));
                        }
                        if(list.contains("1")){
                            String sql3 = "update uf_wdzx set zt = 1"+" where id = " + mbmc;
                            rs3.execute(sql3);
                        }else if(list.contains("0")){
                            String sql3 = "update uf_wdzx set zt = 0"+" where id = " + mbmc;
                            rs3.execute(sql3);
                        }else{
                            String sql3 = "update uf_wdzx set zt = 2"+" where id = " + mbmc;
                            rs3.execute(sql3);
                        }
                    }else{

                        //自动生成模板编号
                        String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
                        //查询当前明细表中是否存在数据
                        String nowDate = DateUtil.today();
                        String firstSql = "select * from uf_wdzx where mbbh is not null and modedatacreatedate = '"+nowDate+"' order by mbbh desc";
                        List<Map<String,String>> list = JAVATools.GetTableToListMap(firstSql, new HashMap<String, String>());
                        Long mbbh = 0L;
                        if(list != null && list.size()>0){
                            mbbh = Long.parseLong(list.get(0).get("mbbh")) ;
                            mbbh = mbbh+1;

                        }else{
                            mbbh = Long.parseLong(String.valueOf(DateUtil.format(new Date(),"yyyyMMdd"))+"000001") ;
                        }

                        //插入主表 mbmc1,gly,mbms,yqsyxt,wj,bbh,sxrq,sxrq1
                        String uuid = UUID.randomUUID().toString();
                        if(StringUtils.isBlank(sxrq1)){
                            parse = DateUtil.parse(sxrq);
                            DateUtil.today();
                            if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                                Map<String,String> map=new HashMap<>();
                                map.put("uuid", uuid);
                                map.put("mbmc",mbmc1);
                                map.put("mbms",mbms);
                                map.put("mbbh",String.valueOf(mbbh));
                                map.put("yqsyxt",yqsyxt);
                                map.put("yqsyxtfx",yqsyxtfx);
                                map.put("wj",xwj);
                                map.put("sxrq",sxrq);
                                map.put("cjr",ngr);
                                map.put("formmodeid",new PropBean().getPropName("wdzxModeId"));
                                map.put("modedatacreater",ngr);
                                map.put("modedatacreatedate", DateUtil.today());
                                map.put("gly", gly);
                                map.put("sxrq1", sxrq1);
                                map.put("wjcjsj", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                                map.put("ywcj",ywcj);
                                map.put("cjsj", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                                map.put("yqsyxtfx", yqsyxtfx);
                                map.put("bb", bbh.substring(1));
                                JAVATools.Excetesql(map,"uf_wdzx");
                            }
                        }else{
                            parse = DateUtil.parse(sxrq);
                            if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                                Map<String,String> map=new HashMap<>();
                                map.put("uuid", uuid);
                                map.put("gly", gly);
                                map.put("mbmc",mbmc1);
                                map.put("mbms",mbms);
                                map.put("yqsyxt",yqsyxt);
                                map.put("mbbh",String.valueOf(mbbh));
                                map.put("wj",xwj);
                                map.put("sxrq",sxrq);
                                map.put("cjr",ngr);
                                map.put("formmodeid",new PropBean().getPropName("wdzxModeId"));
                                map.put("modedatacreater",ngr);
                                map.put("modedatacreatedate", DateUtil.today());
                                map.put("wjcjsj", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                                map.put("sxrq1", sxrq1);
                                map.put("yqsyxtfx", yqsyxtfx);
                                map.put("ywcj",ywcj);
                                map.put("cjsj", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                                map.put("bb", bbh.substring(1));
                                JAVATools.Excetesql(map,"uf_wdzx");
                            }
                        }
                        String mainid = "select * from uf_wdzx where uuid = '"+uuid+"'";
                        RecordSet mianrs = new RecordSet();
                        mianrs.execute(mainid);
                        mianrs.next();
                        String id =mianrs.getString("id");
                        String wjcjsj =mianrs.getString("wjcjsj");
                        //12-07
                        String users = "select * from uf_wdzx where id = " + id;
                        JAVATools.SetCmdwritelog("modesaveuser billid====" + id);
                        rs.execute(users);
                        rs.next();
                        String gly1 = rs.getString("gly");
                        JAVATools.SetCmdwritelog("modesaveuser gly ====" + gly1);
                        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 ("+gly1+"))";
                        JAVATools.SetCmdwritelog("modesaveuser glysql ====" + glysql);
                        rs.execute(glysql);
                        while(rs.next()){
                            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("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 = " + id;
                        JAVATools.SetCmdwritelog("modesaveuser update ====" + update);
                        rs.execute(update);
                        //12-07
                        //权限重构
                        ModeRightInfo modeRightInfo = new ModeRightInfo();
                        PropBean pb = new PropBean();
                        String rwmodeid = pb.getPropName("WDZX.MODELCUBEUUID");
                        JSONObject jsonObject_rw = pb.getModeInfoByUUID(rwmodeid);
                        int modeid = Util.getIntValue(jsonObject_rw.getString("modeid"));
                        modeRightInfo.editModeDataShare(1,modeid,Integer.parseInt(id));
                        //如果旧的明细表的失效日期
                        String olddt = "update uf_wdzx_dt1 set sxrq1 = '"+sxrq+"' where mainid = "+mbmc+" and  sxrq1 is null";
                        RecordSet oldrs =  new RecordSet();
                        oldrs.execute(olddt);
                        //插入明细表
                        HashMap<String, String> map1 = new HashMap<>();
                        map1.put("mainid",id);
                        map1.put("bbh",bbh);
                        map1.put("wj",xwj);
                        map1.put("cjr",ngr);
                        map1.put("cjsj",wjcjsj);
                        map1.put("sxrq",sxrq);
                        map1.put("sxrq1",sxrq1);
                        map1.put("zt",zt);
                        map1.put("gllc",requestid);
                        JAVATools.Excetesql(map1,"uf_wdzx_dt1");
                        //更新建模主表的状态
                        String ztsqsl = "select * from uf_wdzx_dt1 where mainid = " + id;
                        RecordSet ztrs = new RecordSet();
                        ztrs.execute(ztsqsl);
                        ArrayList<String> listALL = new ArrayList<>();
                        while(ztrs.next()){
                            listALL.add(ztrs.getString("zt"));
                        }
                        if(listALL.contains("1")){
                            String sql3 = "update uf_wdzx set zt = 1"+" where id = " + id;
                            rs3.execute(sql3);
                        }else if(listALL.contains("0")){
                            String sql3 = "update uf_wdzx set zt = 0"+" where id = " + id;
                            rs3.execute(sql3);
                        }else{
                            String sql3 = "update uf_wdzx set zt = 2"+" where id = " + id;
                            rs3.execute(sql3);
                        }
                        //2022-03-14未选择模板文件时，流程办结后将模板编号更新到流程中
                        String updateDt1Sql = "update "+tablename+"_dt1  set mbbh = '"+mbbh+"' where id = " + dt1Id;
                        System.out.println(updateDt1Sql);
                        rs.execute(updateDt1Sql);


                    }

                }
            }else if(Objects.equals("1",czlx)){
                String sqldt3 = "select * from "+ tablename + "_DT3 where mainid = " + mianid;
                RecordSet rsdt3 = new RecordSet();
                rsdt3.execute(sqldt3);
                while(rsdt3.next()){
                    String mbmcdt3 = rsdt3.getString("mbmc");
                    String sfxzyymbdt3 = rsdt3.getString("sfxzyymb");
                    String xwjdt3 = rsdt3.getString("xwj");
                    xwjdt3 = qingGao(xwjdt3);
                    JAVATools.SetCmdwritelog("变更清稿文件后的id="+xwjdt3);
                    String qgid = rsdt3.getString("id");
                    String updateQgWjSql = "update " + tablename+ "_dt3 set qgwj = '"+xwjdt3+"' where id = "+qgid;
                    JAVATools.SetCmdwritelog("变更清稿文件sql="+xwjdt3);
                    rsdt3.execute(updateQgWjSql);
                    String bbhdt3 = rsdt3.getString("bbh");
                    String sxrqdt3 = rsdt3.getString("sxrq");
                    String sxrq1dt3 = rsdt3.getString("sxrq1");
                    String mbmc1dt3 = rsdt3.getString("mbmc1");
                    String mbmsdt3 = rsdt3.getString("mbms");
                    String yqsyxtdt3 = rsdt3.getString("yqsyxt");
                    String dqsxwj = rsdt3.getString("dqsxwj");
                    Date date = new Date();
                    DateTime parse = null ;
                    String zt= "0";
                    if(StringUtils.isBlank(sxrq1dt3)){
                        parse = DateUtil.parse(sxrqdt3);
                        zt = "0";
                        DateUtil.today();
                        if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                            zt ="1";
                        }
                    }else{
                        parse = DateUtil.parse(sxrqdt3);
                        DateTime parse1 = DateUtil.parse(sxrq1dt3);
                        zt = "0";
                        if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                            zt ="1";
                            if(DateUtil.between(parse1, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>0 || DateUtil.between(parse,parse1, DateUnit.DAY,false)==0){
                                zt ="2";
                            }
                        }

                    }
                    //11-26
                    String gllcSql ="select * from  uf_wdzx_dt1 where mainid = " + mbmcdt3 + " and zt = 1";

                    gllrs.execute(gllcSql);
                    gllrs.next();

                    String gllc = gllrs.getString("gllc");
                    if(StringUtils.isNotBlank(gllc)){
                        StringBuffer stringBuffer = new StringBuffer(gllc);
                        stringBuffer.append(",");
                        stringBuffer.append(requestid);

                        String upgll = "update uf_wdzx_dt1 set gllc = '"+stringBuffer.toString()+"' where mainid = " + mbmcdt3 +" and zt = 1";
                        RecordSet upgllrs =  new RecordSet();
                        upgllrs.execute(upgll);
                    }else{
                        String upgll = "update uf_wdzx_dt1 set gllc = '"+requestid+"' where mainid = " + mbmcdt3 +" and zt = 1";
                        RecordSet upgllrs =  new RecordSet();
                        upgllrs.execute(upgll);
                    }


                    String sql9 = "select * from uf_wdzx_dt1 where  mainid = " + mbmcdt3;
                    RecordSet rs9 = new RecordSet();
                    rs9.execute(sql9);
                    String now = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
                    RecordSet gllcrs = new RecordSet();
                    while(rs9.next()){
                        String sxrq = rs9.getString("sxrq");
                        String sxrq1 = rs9.getString("sxrq1");
                        String id = rs9.getString("id");
                        //日期相撞
                        if(DateUtil.between(DateUtil.parse(sxrq), DateUtil.parse(sxrqdt3), DateUnit.DAY,false)==0){
                            //旧明细表,当前文件的生效时间更新到旧文件的失效时间
                            String sql8 = "update uf_wdzx_dt1 set zt = 2 ,sxrq1='"+sxrqdt3+"' where id = " + id;

                            rs8.execute(sql8);

                            if(Objects.equals("1",zt)){
                                String sql3 = "update uf_wdzx set bb = '"+bbhdt3.substring(1)+"',wj = '" + xwjdt3 + "' ,wjcjsj= '" + now + "' ,sxrq= '" + sxrqdt3 + "',sxrq1='"+sxrq1dt3+"' where id = " + mbmcdt3;
                                rs3.execute(sql3);
                            }else{
                                String sql3 = "update uf_wdzx set bb = '',wj = '',wjcjsj = '' ,sxrq = '' ,sxrq1=''  where id = " + mbmcdt3;
                                rs3.execute(sql3);
                            }
                        }else{
                            //旧明细表
                            String oldzt = rs9.getString("zt");
                            if(!"2".equals(oldzt)){
                                if(StringUtils.isBlank(sxrq1)){
                                    parse = DateUtil.parse(sxrq);
                                    oldzt = "0";
                                    DateUtil.today();
                                    if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                                        oldzt ="1";
                                        //变更的新文件是未生效的，当前生效文件的日期为空
                                        if(zt.equals("0")){
                                            String sql = "update uf_wdzx_dt1 set sxrq1 = '"+sxrqdt3+"' where id = "+id ;
                                            rs.execute(sql);
                                            String sql3 = "update uf_wdzx set sxrq1='"+sxrqdt3+"' where id = " + mbmcdt3;
                                            rs.execute(sql3);
                                        }else{
                                            String sql = "update uf_wdzx_dt1 set sxrq1 = '"+sxrqdt3+"',zt=2 where id = "+id ;
                                            rs.execute(sql);

                                            String sql3 = "update uf_wdzx set bb = '"+bbhdt3.substring(1)+"', wj = '" + xwjdt3 + "' ,wjcjsj= '" + now + "' ,sxrq= '" + sxrqdt3 + "',sxrq1='"+sxrq1dt3+"' where id = " + mbmcdt3;
                                            rs3.execute(sql3);
                                        }
                                    }
                                }else{
                                    parse = DateUtil.parse(sxrq);
                                    DateTime parse1 = DateUtil.parse(sxrq1);
                                    oldzt = "0";
                                    if(DateUtil.between(parse, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>=0){
                                        oldzt ="1";
                                        //变更的新文件是未生效的，当前生效文件的日期为空，或者当前生文件的日期大于变更文件的日期
                                        if(zt.equals("0")){

                                            if(DateUtil.between(DateUtil.parse(sxrqdt3),parse1, DateUnit.DAY,false)>0){
                                                String sql = "update uf_wdzx_dt1 set sxrq1 = '"+sxrqdt3+"' where id = "+id ;
                                                rs.execute(sql);
                                                String sql3 = "update uf_wdzx set sxrq1='"+sxrqdt3+"' where id = " + mbmcdt3;
                                                rs.execute(sql3);
                                            }

                                        }else{
                                            if(DateUtil.between(DateUtil.parse(sxrqdt3),parse1, DateUnit.DAY,false)>0){
                                                String sql = "update uf_wdzx_dt1 set sxrq1 = '"+sxrqdt3+"',zt=2 where id = "+id ;
                                                rs.execute(sql);
                                            }else{
                                                String sql = "update uf_wdzx_dt1 set zt=2 where id = "+id ;
                                                rs.execute(sql);
                                            }


                                            String sql3 = "update uf_wdzx set bb = '"+bbhdt3.substring(1)+"',wj = '" + xwjdt3 + "' ,wjcjsj= '" + now + "' ,sxrq= '" + sxrqdt3 + "',sxrq1='"+sxrq1dt3+"' where id = " + mbmcdt3;
                                            rs3.execute(sql3);
                                        }
                                    }
                                    if(DateUtil.between(parse1, DateUtil.parse(DateUtil.today()), DateUnit.DAY,false)>0){
                                        oldzt ="2";
                                        //更新建模主表的数据
                                        if(Objects.equals("1",zt)){
                                            String sql3 = "update uf_wdzx set bb = '"+bbhdt3.substring(1)+"',wj = '" + xwjdt3 + "' ,wjcjsj= '" + now + "' ,sxrq= '" + sxrqdt3 + "',sxrq1 ='"+sxrq1dt3+"' where id = " + mbmcdt3;
                                            rs3.execute(sql3);
                                        }else{
                                            String sql3 = "update uf_wdzx set bb = '',wj = '',wjcjsj = '' ,sxrq = '',sxrq1 =''  where id = " + mbmcdt3;
                                            rs3.execute(sql3);
                                        }

                                    }
                                }
                                if(StringUtils.isBlank(sxrq1)){
                                    String sql8 = "update uf_wdzx_dt1 set zt = "+oldzt+",sxrq1='"+sxrqdt3+"' where id = " + id;
                                    rs8.execute(sql8);
                                }else{
                                    String sql8 = "update uf_wdzx_dt1 set zt = "+oldzt+" where id = " + id;

                                    rs8.execute(sql8);
                                }
                            }
                        }
                    }
                    //插入建模明细表的数据
                    HashMap<String, String> map1 = new HashMap<>();
                    map1.put("mainid",mbmcdt3);
                    map1.put("bbh", bbhdt3);
                    map1.put("wj", xwjdt3);
                    map1.put("cjr", ngr);
                    map1.put("cjsj", now);
                    map1.put("sxrq", sxrqdt3);
                    map1.put("sxrq1", sxrq1dt3);
                    map1.put("zt", zt);
                    map1.put("gllc", requestid);
                    JAVATools.Excetesql(map1, "uf_wdzx_dt1");
                    //更新建模主表的状态
                    String ztsqsl = "select * from uf_wdzx_dt1 where mainid = " + mbmcdt3;
                    RecordSet ztrs = new RecordSet();
                    ztrs.execute(ztsqsl);
                    ArrayList<String> list = new ArrayList<>();
                    while(ztrs.next()){
                        list.add(ztrs.getString("zt"));
                    }
                    if(list.contains("1")){
                        String sql = "update uf_wdzx set zt = 1"+" where id = " + mbmcdt3;
                        rs.execute(sql);
                    }else if(list.contains("0")){
                        String sql = "update uf_wdzx set zt = 0"+" where id = " + mbmcdt3;
                        rs.execute(sql);
                    }else{
                        String sql = "update uf_wdzx set zt = 2"+" where id = " + mbmcdt3;
                        rs.execute(sql);
                    }
                }
            }else if(Objects.equals("2",czlx)){
                String sql2 = "select * from "+ tablename + "_DT4 where mainid = " + mianid;
                RecordSet rs2 = new RecordSet();
                rs2.execute(sql2);
                while(rs2.next()){
                    String fqrq = rs2.getString("fqrq");
                    String mbmc = rs2.getString("mbmc");
                    String id = rs2.getString("id");
                    String ckwj = rs2.getString("ckwj");
                    //dt1 sfck = 1 同步失效时间
                    JAVATools.SetCmdwritelog("出库文件==="+ckwj);
                    //12-03
                    String dtSql = "select * from  uf_wdzx_dt1 where wj = " + ckwj;
                    RecordSet dtrs = new RecordSet();
                    dtrs.execute(dtSql);
                    while(dtrs.next()){
                        if(DateUtil.between(DateUtil.parse(fqrq), DateUtil.parse(DateUtil.today()), DateUnit.DAY,false) == 0){
                            if("1".equals(dtrs.getString("zt"))){

                                String sql3 = "update uf_wdzx set wj = '',wjcjsj = '' ,sxrq = '',sxrq1=''  where id  = " + mbmc;
                                rs3.execute(sql3);
                                JAVATools.SetCmdwritelog("清空主表的当前生效文件，创建时间，生效日期" + sql3);
                            }
                            String gllc = dtrs.getString("gllc");
                            StringBuffer stringBuffer = new StringBuffer(gllc);
                            stringBuffer.append(",");
                            stringBuffer.append(requestid);
                            String dt3 = "update uf_wdzx_DT1 set sfck = 0 , zt = 2, sxrq1 = '"+fqrq+"' ,gllc = '"+stringBuffer.toString()+"'  where id  = " + dtrs.getString("id");
                            rs3.execute(dt3);
                        }else{
                            String gllc = dtrs.getString("gllc");
                            StringBuffer stringBuffer = new StringBuffer(gllc);
                            stringBuffer.append(",");
                            stringBuffer.append(requestid);
                            String dt3 = "update uf_wdzx_DT1 set sfck = 0 ,  sxrq1 = '"+fqrq+"' ,gllc = '"+stringBuffer.toString()+"'  where id  = " + dtrs.getString("id");
                            rs3.execute(dt3);
                            //判断当前的明细表是不是生效文件，若为生效文件更新失效时间
                            String sql = "select * from uf_wdzx_dt1 where wj = " + ckwj;
                            rs3.execute(sql);
                            rs3.next();
                            String zt = rs3.getString("zt");
                            if("1".equals(zt)){
                                String upsql = "update uf_wdzx set sxrq1 = '"+fqrq+"' where id = " + rs3.getString("mainid");
                                rs3.execute(upsql);
                            }
                        }

                    }
                    //更新建模主表的状态
                    String ztsqsl = "select * from uf_wdzx_dt1 where mainid = " + mbmc;
                    JAVATools.SetCmdwritelog("通过建模明细表更新建模主表的状态" + ztsqsl);
                    RecordSet ztrs = new RecordSet();
                    ztrs.execute(ztsqsl);
                    ArrayList<String> list = new ArrayList<>();
                    while(ztrs.next()){
                        list.add(ztrs.getString("zt"));
                    }
                    JAVATools.SetCmdwritelog("明细表的id= "+ list.toString());
                    if(list.contains("1")){
                        String sql = "update uf_wdzx set zt = 1"+" where id = " + mbmc;
                        rs.execute(sql);
                        System.out.println("更新主表状态" + sql);
                    }else if(list.contains("0")){
                        String sql = "update uf_wdzx set zt = 0"+" where id = " + mbmc;
                        rs.execute(sql);
                        System.out.println("更新主表状态" + sql);
                    }else{
                        String sql = "update uf_wdzx set zt = 2"+" where id = " + mbmc;
                        rs.execute(sql);
                        System.out.println("更新主表状态" + sql);
                    }
                }
            }
        }catch (Exception e ){
            JAVATools.SetCmdwritelog("文档中心明细转建模"+e.toString());
            e.printStackTrace();
        }

        return Action.SUCCESS;
    }
    //保存之前清稿
    public String qingGao(String docid){
        BaseBean bb = new BaseBean();
        int  newdocid = 0;
        String sql = "";
        try{
            RecordSet rs = new RecordSet();
            String docSql = "select * from (select * from  docimagefile where docid =  "+docid+"   order by imagefileid  desc ) where rownum = 1";
            bb.writeLog("根据docid查询最新的版本"+docSql);
            rs.execute(docSql);
            rs.next();
            String imageFileName =  rs.getString("imagefilename");
            bb.writeLog("清稿文件的名称"+imageFileName);
            String fileType = imageFileName.substring(imageFileName.lastIndexOf("."));
            bb.writeLog("清稿文件的类型"+fileType);
            //2022-1-12 清稿文件只能是doc和docx
            if(".DOC".equals(fileType.toUpperCase())||".DOCX".equals(fileType.toUpperCase())){
                String imagefileid =  rs.getString("imagefileid");
                String type =  imageFileName.substring(imageFileName.lastIndexOf(".")+1);
                HashMap<String, Object> map = new HashMap<>();
                map.put("doctype",type);
                bb.writeLog("文档中心清稿doctype"+type);
                //正文imagefileId
                map.put("imagefileId",Integer.parseInt(imagefileid));
                bb.writeLog("文档中心清稿imagefileId"+imagefileid);
                map.put("fileName",imageFileName);
                bb.writeLog("文档中心清稿fileName"+imageFileName);
                AcceptRevision acceptRevision=new AcceptRevision();
                String newImageFileId= Util.null2String(acceptRevision.acceptRevisions(map));
                Map<String,Object> filevalue=getdocimagefile(docid);
                if(filevalue==null || filevalue.size()<=0){
                    return null;
                }
                int seccategoryid = Util.getIntValue(Util.null2String(filevalue.get("seccategory")));
                int maincategoryid = Util.getIntValue(Util.null2String(filevalue.get("maincategory")));
                int subcategoryid = Util.getIntValue(Util.null2String(filevalue.get("subcategory")));
                String docextendname = Util.null2String(filevalue.get("docextendname"));
                String imagefilename = Util.null2String(filevalue.get("imagefilename"));
                String docfiletype = Util.null2String(filevalue.get("docfiletype"));
                String filerealpath= Util.null2String(filevalue.get("filerealpath"));
                String imagefiletype = Util.null2String(filevalue.get("imagefiletype"));
                String doccreaterid = Util.null2String(filevalue.get("doccreaterid"));
                String doctype = Util.null2String(filevalue.get("doctype"));
                String oldimagefileid= Util.null2String(filevalue.get("imagefileid"));
                int filesize = Util.getIntValue(Util.null2String(filevalue.get("filesize")));
                String content = "";
                String pubtime = TimeUtil.getCurrentDateString();
                newdocid = new UploadDocUtil().createDoc(imagefilename,content, Util.getIntValue(doccreaterid),pubtime,maincategoryid,subcategoryid,seccategoryid, Util.getIntValue(doctype),docextendname,"");

                DocImageManager dm = new DocImageManager();
                VersionIdUpdate versionIdUpdate = new VersionIdUpdate();

                int versionid = versionIdUpdate.getVersionNewId();
                sql = " insert into DocImageFile (id,docid,imagefileid,imagefilename,imagefilewidth,imagefileheight,isextfile,imagefielsize,docfiletype,versionid)"+
                        " values("+dm.getNextDocImageFileId()+","+newdocid+","+newImageFileId+",'"+imagefilename+"','0','0','1','0','"+docfiletype+"',"+versionid+")";
                rs.execute(sql);

                sql = " select count(1) as cou from DocImageFile where docid="+newdocid;
                rs.execute(sql);
                rs.next();
                int count = Util.getIntValue(Util.null2String(rs.getString("cou")));

                sql = " update docdetail set accessorycount="+count+" where id="+newdocid;

                rs.execute(sql);
                return String.valueOf(newdocid);
            }else {
                return docid;
            }

        }catch (Exception e){
            e.printStackTrace();
        }


        return "0";
    }
    public   Map<String,Object> getdocimagefile(String docid){
        String sql =" select t.maincategory,t.subcategory,t.seccategory,t.docextendname,l.imagefilename,t.doctype,p.filesize, " +
                " l.docfiletype,p.imagefiletype,p.imagefileid,p.filerealpath,t.doccreaterid " +
                " from docdetail t " +
                " inner join docimagefile l on l.docid = t.id " +
                " inner join imagefile p on p.imagefileid = l.imagefileid " +
                " where docid=? and versionid IN(select max(versionid) from DocImageFile where docid=? ) ";
        Map<String,String> map=new HashMap<>();
        return DBTools.getsqltomap(sql,docid,docid);

    }
}

