package com.engine.htxc.eseal.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htxc.eseal.dao.SendSignetDao;
import com.engine.htxc.eseal.service.SendSignetService;
import com.engine.integration.util.StringUtils;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.htsc.util.UploadDocUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class SendSignetServiceImpl extends Service implements SendSignetService {

    @Override
    public void sendsealworkflow(Map<String, Object> map) {
        BaseBean bb=new BaseBean();
        UploadDocUtil uploadDocUtil=new UploadDocUtil();
       String detailid= Util.null2String(map.get("detailid"));
       String signetdetailid= Util.null2String(map.get("signetdetailid"));
       String requestid= Util.null2String(map.get("requestid"));
       String requestname=Util.null2String(map.get("requestname"));
       if(StringUtils.isBlank(detailid) || StringUtils.isBlank(signetdetailid) ||
          StringUtils.isBlank(requestid) || StringUtils.isBlank(requestname)){
           bb.writeLog("触发用印登记单错误！参数："+"detailid:"+detailid+"signetdetailid:"+signetdetailid+"requestid:"+requestid+"requestname:"+requestname);
           return;
       }



        String formTable = SendSignetDao.getTableByRequestid(requestid);
        String detailtable = formTable + "_dt" + detailid;
        bb.writeLog("<br/>formTable:" + formTable);
        if (!"".equals(formTable)) {
            List<Map<String, String>> docList = new ArrayList<Map<String, String>>();
            List<Map<String, String>> fileList = new ArrayList<Map<String, String>>(); //用印文件选择
            JSONObject jsonObject = new JSONObject();


            Map<String, Object> sealmap = SendSignetDao.getesealInfo(requestid, detailtable, formTable);
            if (sealmap == null || sealmap.size() <= 0) {
                bb.writeLog("用印登记单");
                return;
            }
            String id = Util.null2String(sealmap.get("id"));
            String yz = Util.null2String(sealmap.get("yz"));
            String zwpdf = Util.null2String(sealmap.get("zwpdf"));
            String fjid = Util.null2String(sealmap.get("fjid"));
            String ngr = Util.null2String(sealmap.get("ngr"));
            String szbm = Util.null2String(sealmap.get("szbm"));
            String szjg = Util.null2String(sealmap.get("szjg"));
            String lxfs = Util.null2String(sealmap.get("lxfs"));
            String jjcd = Util.null2String(sealmap.get("jjcd"));
            String ngrq = Util.null2String(sealmap.get("ngrq"));
            String fs = Util.null2String(sealmap.get("fs"));
            String filedistinct = SendSignetDao.filedistinct(zwpdf, fjid);
            if ("".equals(ngr) || "".equals(yz) || "".equals(filedistinct)) {
                return;
            }

            User usr = new User(Util.getIntValue(ngr));
            Map<String, String> mapstr = new HashMap<>();
            JSONObject sealjson = new JSONObject();
            sealjson.put("yz", yz);
            sealjson.put("yywj", filedistinct);
            sealjson.put("ngr", ngr);
       //     sealjson.put("gllc", Util.null2String(requestid));
            sealjson.put("szbm", szbm);
            sealjson.put("szjg", szjg);
            sealjson.put("lxfs", lxfs);
            sealjson.put("fs", fs);
            sealjson.put("jjcd", jjcd);
            sealjson.put("ngsj", ngrq);
            sealjson.put("lylx", JAVATools.getmainunnumber(Util.null2String(requestid)));
           // sealjson.put("fj", filedistinct);
            sealjson.put("requestid",  Util.null2String(requestid));
            sealjson.put("requestname", requestname);
            bb.writeLog("公文类数据调用拥有登记单参数：" + sealjson.toJSONString());
            String url=new PropBean().getPropName("htoa.esealworkflow");
            String response=new HTUtil().httpPostRaw(url,sealjson.toJSONString(),null,"utf-8");
            if(StringUtils.isBlank(response)){
                return;
            }
            try {
                JSONObject json = JSONObject.parseObject(response);
                String status=Util.null2String(json.get("status"));
                if(!"200".equals(status)){
                    return ;
                }
                String datavalue=Util.null2String(json.get("data"));
                if(StringUtils.isBlank(datavalue)){
                    return ;
                }
                JSONObject jsonvalue = JSONObject.parseObject(datavalue);
                String htoaid=Util.null2String(jsonvalue.get("id"));
                if(StringUtils.isBlank(htoaid) ){
                    return ;
                }
                Map<String,Object> updatemap=new HashMap<>();
                updatemap.put("id",id);
                updatemap.put("tablename",formTable);
                updatemap.put("htoagllc",htoaid);

                SendSignetDao.updatewfstatus(updatemap);
            }catch (Exception e){

            }
        }
    }


}
