package com.engine.htsc.mail.service;


import com.alibaba.fastjson.JSON;
import com.engine.core.impl.Service;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.workflow.WorkflowCreateHandler;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MailAutoCreateService extends Service {

    public void create(String requestid){
        PropBean pb = new PropBean();
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();

        try{
            String tableName = JAVATools.getMaintable(requestid);
            String workflowId = pb.getWorkflowIdsByUnNumber("ydxxgzfk");
            Map<String,String> requestMainMap = new HashMap<>();


            String lcbh = "";
            String lcbt = "";
            String sql = "select requestname,requestmark from workflow_requestbase where requestid = '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                lcbh = Util.null2String(rs.getString("requestmark"));
                lcbt = Util.null2String(rs.getString("requestname"));
            }
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
            String requestName = "邮递信息跟踪-"+lcbh+"-"+lcbt+"-"+sdf1.format(new Date());

            String creatorId = "";
            String szbm = "";
            String szjg = "";
            String lxfs = "";
            String wjhdfs = "";
            String gllc = "";
            sql = "select ngr,szbm,szjg,lxfs,wjhdfs,gllc from "+tableName+" where requestid =  '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                creatorId = Util.null2String(rs.getString("ngr"));
                szbm = Util.null2String(rs.getString("szbm"));
                szjg = Util.null2String(rs.getString("szjg"));
                lxfs = Util.null2String(rs.getString("lxfs"));
                wjhdfs = Util.null2String(rs.getString("wjhdfs"));
                gllc = Util.null2String(rs.getString("gllc"));
            }
            requestMainMap.put("ngr",creatorId);//拟稿人
            requestMainMap.put("szbm",szbm);//所在部门
            requestMainMap.put("szjg",szjg);//所在机构
            requestMainMap.put("jjcd","0");//缓急程度
            requestMainMap.put("lxfs",lxfs);//联系方式
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            requestMainMap.put("ngrq",sdf2.format(new Date()));//拟稿时间
            SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
            requestMainMap.put("kdfcrq",sdf3.format(new Date()));//快递发出日期
            requestMainMap.put("sfzdcf","1");//是否自动触发
            requestMainMap.put("gllc",requestid);//关联流程
            requestMainMap.put("lxr",creatorId);//联系人
            requestMainMap.put("sfyjcysjr","0");//是否邮寄常用收件人
            if("0".equals(wjhdfs)){
                requestMainMap.put("cdfs","0");//传递方式
            }else if("1".equals(wjhdfs)){
                requestMainMap.put("cdfs","1");
            }else{
                requestMainMap.put("cdfs","4");
            }



            String sjrxm = "";
            sql = "select sjrxm,lxdh,lxdz from uf_fzjgcysjr where ssjg = '"+szbm+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                sjrxm = Util.null2String(rs.getString("sjrxm"));
                requestMainMap.put("sjrllan",sjrxm);//收件人
                requestMainMap.put("jlsjr",sjrxm);//记录收件人
                requestMainMap.put("lxdh",Util.null2String(rs.getString("lxdh")));//联系电话
                requestMainMap.put("sjdz",Util.null2String(rs.getString("lxdz")));//收件地址
            }

            if(StringUtils.isNotEmpty(sjrxm)){
                String szjg1 = "";
                sql = "select departmentid from hrmresource where id = '"+sjrxm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    szjg1 = Util.null2String(rs.getString("departmentid"));
                }
                requestMainMap.put("szjg1",szjg1);//收件人所在机构
            }




            bs.writeLog("requestMainMap:"+ JSON.toJSONString(requestMainMap));
            WorkflowCreateHandler workflowCreateHandler = new WorkflowCreateHandler();
            String newRequestid=  workflowCreateHandler.WorkflowCreateByRequestMap(creatorId, workflowId, requestName, "1", requestMainMap);
            bs.writeLog("New Requestid:"+newRequestid);

            //更新原流程的关联流程
            String newGllc = "";
            if(StringUtils.isNotEmpty(gllc)){
                newGllc = gllc+","+newRequestid;
            }else {
                newGllc = newRequestid;
            }
            String update_sql = "update "+tableName+" set gllc = '"+newGllc+"' where requestid =  '"+requestid+"'";
            bs.writeLog(update_sql);
            rs.executeUpdate(update_sql);

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



}
