package com.engine.htsc.workflow.ld;

import com.alibaba.fastjson.JSONObject;
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 weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * @Description: TODO(部门发文文号创建方法)
 * <p>
 * Development(开发部门): OA项目组-泛微
 * <p>
 * Copyright(版权):
 * @date(创建时间): 2020/11/11 10:51
 * @author(作者): 李丹
 * <p>
 * Last modified(最后修改时间): 2020/11/11 10:51
 * <p>
 * Modified by(修改人): 李丹
 */
public class DeptWHUtil {

    public static synchronized String deptWH(String wfid,String reqid,String szbm,String uid,String htlx){
        String wh = "1";
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        PropBean pb=new PropBean();
        String year = TimeUtil.getCurrentDateString().substring(0,4);
        //---获取最大文号---
        String maxlsh = "";//最大流水号
        String zh = "";//字号
        String bmzh="";
        String sql="";
        int lsh = 1;//流水号
    //    String sql = "select max(wh) as maxwh from uf_bmfwbh where nf='"+year+"' and zh='"+szbm+"'";
        bb.writeLog("当前红头文件是："+htlx);
        if(htlx!=null&&htlx!=""){
            //按照红头文件来
            //如果传递的红头文件是空的话走老逻辑
            //---获取部门对应字号---
            //获取字号
            sql = "select docnumber from ht_hrmdept_abbre_view where id='"+szbm+"'";
            rs.executeQuery(sql);
            if(rs.next()){
                bmzh = Util.null2String(rs.getString("docnumber"));
            }

             sql = "select max(wh) as maxwh from uf_bmfwbh where nf='"+year+"' and htlx='"+htlx+"' and zh='"+szbm+"' and gwz='"+bmzh+"'";
            String whvalue=Util.null2String(pb.getPropName("zhfw.whcount"));
            if((whvalue.indexOf(","+htlx+","))>=0){
                sql = "select max(wh) as maxwh from uf_bmfwbh where  nf='"+year+"' and htlx='"+htlx+"'";
            }
            bb.writeLog("根据红头流水号查询："+sql);
            rs.executeQuery(sql);
            if(rs.next()){
                    maxlsh = Util.null2String(rs.getString("maxwh"));
            }
            if(htlx.equals("5")){
             zh="华泰证董字";
            }else if(htlx.equals("6")){
                zh="华泰证监字";
            }else if(htlx.equals("13")){
                zh="华泰证董办字";
            }else if(htlx.equals("14")){
                zh="华泰证监办字";
            }else if(htlx.equals("7")){
                zh="苏纪驻华泰发";
            }else if(htlx.equals("1")){
                //---获取部门对应字号---
                sql = "SELECT deptTypecode,docnumabbreviation from hrmdepartmentdefined where deptid='"+szbm+"'";
                bb.writeLog("根据红头类型查询："+sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    if (Util.null2String(rs.getString("docnumabbreviation"))!=""){
                        zh=  "华泰证" + Util.null2String(rs.getString("docnumabbreviation")) + "字";
                    }else{
                        zh="华泰证字";
                    }
                }
            }


        }else{

            //---获取部门对应字号---
            sql = "select docnumber from ht_hrmdept_abbre_view where id='"+szbm+"'";
            rs.executeQuery(sql);
            if(rs.next()){
                bmzh = Util.null2String(rs.getString("docnumber"));
            }
            //如果传递的红头文件是空的话走老逻辑
             sql = "select max(wh) as maxwh from uf_bmfwbh where nf='"+year+"' and zh='"+szbm+"' and gwz='"+bmzh+"'";
            rs.executeQuery(sql);
            if(rs.next()){
                maxlsh = Util.null2String(rs.getString("maxwh"));
            }


        }





        String whid=whvf(reqid,zh,bmzh);
        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 bmfwbh_formmodeid = "";
            try {
              //  bmfwbh_formmodeid  = new String(Util.null2String(bb.getPropValue("JMdevelop", "bmfwbh_formmodeid")).getBytes("ISO-8859-1"),"UTF-8").trim();
                String modePropBean = pb.getPropName("bmfwbh_cubeuuid");
                JSONObject jsonObject_bh = pb.getModeInfoByUUID(modePropBean);
                 bmfwbh_formmodeid = Util.null2String(jsonObject_bh.getString("modeid"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            //---插入建模表---
            String currTime = TimeUtil.getCurrentTimeString();
            String time = currTime.substring(0,currTime.length()-3);
            sql = "insert into uf_bmfwbh(zh,nf,wh,cjsj,lcid,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,htlx,gwz) "+
                    " values('" + szbm + "','" + year + "','"+lsh+"','" + time + "','"+reqid+"',"+bmfwbh_formmodeid+","+uid+",0,'" + TimeUtil.getCurrentDateString() + "','" + TimeUtil.getOnlyCurrentTimeString() + "','"+htlx+ "','"+bmzh+"')";;
            boolean flag = rs.execute(sql);
            if (flag) {
                //---权限重构---
                String maxidsql = "select max(id) as id from uf_bmfwbh";
                rs.execute(maxidsql);
                if (rs.next()) {
                    ModeRightInfo ModeRightInfo = new ModeRightInfo();
                    ModeRightInfo.setNewRight(true);
                    ModeRightInfo.editModeDataShare(Integer.valueOf(uid), Integer.valueOf(bmfwbh_formmodeid), rs.getInt("id"));
                }
            }
        }
        return wh;
    }

    public static String whvf(String lcid,String zh,String gwz){
        String sql=" select wh,nf from uf_bmfwbh where 1=1 ";
        Map<String,String> map=new HashMap<>();
        map.put("lcid",lcid);
        map.put("gwz",gwz);
        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+"号";
        }
    }
}
