package com.engine.htsc.workflow.ld;

import com.engine.integration.util.StringUtils;
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;

/**
 * <p>分支机构文号</p>
 * @title FzjgWHUtil
 * @author 李丹
 * @updateTime 2020/11/10 14:18
 */
public class FzjgWHUtil {

    private static String bustype="JGLX09";

    public static synchronized String createWH(String wfid,String reqid,String szbm,String uid,String htlx){
        String wh = "1";
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String year = TimeUtil.getCurrentDateString().substring(0,4);
        //---获取最大文号---
        String maxlsh = "";
        //字号
        String zh = "";
        String sql="";
        //流水号
        int lsh = 1;
        //---获取部门对应字号---
        sql = "select docnumber from ht_hrmdept_abbre_view where id='"+szbm+"'";
        rs.executeQuery(sql);
        if(rs.next()){
            zh = Util.null2String(rs.getString("docnumber"));
        }
        //业务部的发文字号和子公司工号一直
        szbm=getdeptbus(szbm);
        sql = "select max(wh) as maxwh from uf_bmfwbh where nf='"+year+"' and zh='"+szbm+"' and gwz='"+zh+"'";
        rs.executeQuery(sql);
        if(rs.next()){
            maxlsh = Util.null2String(rs.getString("maxwh"));
        }
        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"));
        }

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

    /**
     * <p>分支机构发文</p>
     * @title getdeptbus
     * @author K1810015
     * @updateTime 2022/8/25 14:39
     */
    public static String getdeptbus(String deptid){
        if(StringUtils.isBlank(deptid)){
            return "";
        }
        String sql="select t.depttypecode,(select supdepid from hrmdepartment ht where ht.id=t.deptid ) as supdepid from hrmdepartmentdefined t where 1=1 ";
        Map<String,String> wheremap=new HashMap<>(1);
        wheremap.put("deptid",deptid);
        Map<String,Object> map=JAVATools.GetTableToMap(sql,wheremap);
        if(map==null || map.isEmpty()){
            return deptid;
        }
        String deptTypeCode=Util.null2String(map.get("depttypecode"));
        String supdepid=Util.null2String(map.get("supdepid"));
        if(StringUtils.isBlank(deptTypeCode) || StringUtils.isBlank(supdepid)){
            return deptid;
        }
        if(bustype.equals(deptTypeCode)){
            return supdepid;
        }
        return deptid;
    }
}
