package com.engine.htsc.workflow.ld;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class FoundationWHUtil {
    public static synchronized String FoundationWH(String wfid,String reqid,String zhtype,String uid){
        String wh = "";
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String year = TimeUtil.getCurrentDateString().substring(0,4);
        //---获取最大文号---
        String maxlsh = "";//最大流水号
        String zh = "";//字号
        int lsh = 1;//流水号
        String sql = "select max(wh) as maxwh from uf_jjhfwbh where nf='"+year+"' and zh='"+zhtype+"'";
        rs.executeQuery(sql);
        if(rs.next()){
            maxlsh = Util.null2String(rs.getString("maxwh"));
        }
        if(zhtype.equals("0")){
            zh = "华泰基字";
        }else{
            zh = "华泰基任字";
        }
        String whid=whvf(reqid,zh);
        if(!"".equals(whid)){
            return whid;
        }
        if(!maxlsh.equals("")){
            lsh = Integer.valueOf(maxlsh)+1;
        }
        wh = zh+"〔"+year+"〕"+lsh+"号";
        //---更新流程表---
        String tablename = "";
        sql = "select b.tablename from workflow_base a,workflow_bill b" +
                " where a.formid = b.id and a.id = "+wfid;
        rs.executeQuery(sql);
        if(rs.next()){
            tablename = Util.null2String(rs.getString("tablename"));
        }
        //new BaseBean().writeLog(tablename+"   "+sql+"  "+wfid);
        if(!tablename.equals("")){
            sql = "update "+tablename+" set wh='"+wh+"' where requestid='"+reqid+"'";
            rs.execute(sql);
            //---获取formmodeid---
            String gsfwbh_formmodeid = "";
            try {
                gsfwbh_formmodeid  = new String(Util.null2String(bb.getPropValue("JMdevelop", "gsfwbh_formmodeid")).getBytes("ISO-8859-1"),"UTF-8").trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //---插入建模表---
            String currTime = TimeUtil.getCurrentTimeString();
            String time = currTime.substring(0,currTime.length()-3);
            sql = "insert into uf_jjhfwbh(zh,nf,wh,cjsj,lcid,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) "+
                    " values('" + zhtype + "','" + year + "','"+lsh+"','" + time + "','"+reqid+"',"+gsfwbh_formmodeid+","+uid+",0,'" + TimeUtil.getCurrentDateString() + "','" + TimeUtil.getOnlyCurrentTimeString() + "')";;
            boolean flag = rs.execute(sql);
            if (flag) {
                //---权限重构---
                String maxidsql = "select max(id) as id from uf_jjhfwbh";
                rs.execute(maxidsql);
                if (rs.next()) {
                    ModeRightInfo ModeRightInfo = new ModeRightInfo();
                    ModeRightInfo.setNewRight(true);
                    ModeRightInfo.editModeDataShare(Integer.valueOf(uid), Integer.valueOf(gsfwbh_formmodeid), rs.getInt("id"));
                }
            }
        }

        return wh;
    }


    public static String whvf(String lcid,String zh){
        String sql=" select wh,nf from uf_jjhfwbh where 1=1 ";
        Map<String,String> map=new HashMap<>();
        map.put("lcid",lcid);
        Map<String,Object> map2= JAVATools.GetTableToMap(sql,map);
        if(map2==null ||map2.size()<=0){
            return "";
        }
        String wh=Util.null2String(map2.get("wh"));
        String nf=Util.null2String(map2.get("nf"));
        if("".equals(wh) || "".equals(nf) ){
            return "";
        }else{
            return  zh+"〔"+nf+"〕"+wh+"号";
        }
    }
}
