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

import com.alibaba.fastjson.JSONObject;
import com.engine.htxc.commonutil.user.UserHt;
import com.engine.htxc.eseal.business.contract.service.ContractEsealService;
import com.engine.htxc.eseal.uinterface.Eseal;
import com.engine.htxc.eseal.uinterface.EsealInit;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractEsealServiceImpl.java
 * @createTime 2022年06月21日 13:56:00
 */
public class ContractEsealServiceImpl extends EsealInit implements ContractEsealService,Eseal {

    @Override
    public Eseal sendEseal(Map<String,Object> map,User user) {
        Map<String,Object> initmap=new HashMap<>(4);
        String type=Util.null2String(map.get("type"));
        String yzid=Util.null2String(map.get("yzid"));
        String tablename=Util.null2String(map.get("tablename"));
        initmap.put("tablename",tablename);
        initmap.put(fileid,Util.null2String(map.get("id")));
        initmap.put("docid",Util.null2String(map.get("docid")));
        if("0".equals(type)){
            initmap.put("detailnum","1");
        }else{
            initmap.put("detailnum","3");
        }
        init(initmap,user);
        int ngrvalue=Util.getIntValue(Util.null2String(this.maintable.get("qcr")),-1);
        if(ngrvalue>0){
            esealConfig.user=new UserHt(this.user.getUID());
        }else{
            esealConfig.user=new UserHt(user.getUID());
        }
        esealConfig.code=Util.null2String(pb.getPropName("AutoSign.code"));
        esealConfig.yzcode=yzid;
        this.autopdf=true;
        this.autopdfname=tablename+"_dt5";
        return this;
    }

    @Override
    public boolean sendseal() {
        JSONObject jsonject=dataload();
        if(jsonject==null || jsonject.size()<=0){
            this.flag=false;
            return false;
        }
        sendpost(jsonject.toJSONString());
        return this.flag;
    }

    @Override
    public JSONObject callback() {
      if(!this.flag)  {
          JSONObject jsonObject=new JSONObject();
          jsonObject.put("status", "500");
          jsonObject.put("error", "送用印失败！");
          return jsonObject;
      }
      String where =" where mainid in ("+id+") and wjmc in ("+ Arrays.stream(docids.split(",")).map(item->"'"+item+"'").collect(Collectors.joining(","))+")";
      callbacktble(this.tablename+"_dt"+this.detailnum,"yyzt",where);
      if(!this.flag)  {
          return retjsonobject;
      }
      retjsonobject.put("msg", "200");
      retjsonobject.put("data", "送e-seal用印成功");
      return retjsonobject;
    }
}
