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

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.htsc.seal.web.SealSync;
import com.engine.htxc.eseal.business.contract.service.ContractEsealService;
import com.engine.htxc.eseal.business.contract.service.impl.ContractEsealServiceImpl;
import com.engine.htxc.eseal.dao.ContractBaseDao;
import com.engine.htxc.eseal.dao.EsealBaseDao;
import com.engine.htxc.eseal.service.EsealService;
import com.engine.htxc.eseal.uinterface.Eseal;
import com.engine.htxc.eseal.uinterface.EsealInit;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName EsealServiceImpl.java
 * @createTime 2022年06月16日 13:22:00
 */
public class EsealServiceImpl extends Service implements EsealService {
    @Override
    public String sendcontracteseal(Map<String, Object> map, User user) {
        String docid = Util.null2String(map.get("docid"));
        String id = Util.null2String(map.get("id"));
        String type = Util.null2String(map.get("type"));
        if(StringUtils.isBlank(docid) || StringUtils.isBlank(id) ||StringUtils.isBlank(type) ){
            return JAVATools.Apiretrun("400","参数有误");
        }
        Eseal eseal= ServiceUtil.getService(ContractEsealServiceImpl.class).sendEseal(map,user);
        eseal.sendseal();
        JSONObject jsonObject= eseal.callback();
        //初始化参数
        return jsonObject.toJSONString();
    }

    @Override
    public Map<String, Object> getsealname(Map<String, Object> map,User user) {
        //wf表示workflow,用于区分流程合
        String wftype="wf";
        String unnumber=Util.null2String(map.get("unnumber"));
        String sealname=  Util.null2String(map.get("sealname"));
        String deptid=  Util.null2String(map.get("deptid"));
        String type=  Util.null2String(map.get("type"));
        Map<String,Object> resultMap = new HashMap<>(3);
        if(StringUtils.isBlank(deptid) ||StringUtils.isBlank(unnumber) || StringUtils.isBlank(sealname)){
            return resultMap;
        }
        Arrays.stream(sealname.split(",")).forEach((value)->{
            Map<String,Object> mapvalue=new HashMap<>(3);
            if(wftype.equals(type)){
                 mapvalue= EsealBaseDao.getsealmap(unnumber,value,deptid);
            }else{
                 mapvalue= EsealBaseDao.getmodelsealmap(unnumber,value,deptid);
            }
            if(mapvalue!=null && mapvalue.size()>0){
                resultMap.put(value,mapvalue);
            }
        });
        return resultMap;
    }

    @Override
    public List<Map<String,Object>> getSealNoToName(Map<String, Object> map, User user) {
        List<Map<String, Object>> list=new ArrayList<>();
        if (map == null || map.size() <= 0) {
            return list;
        }
        String sealno=Util.null2String(map.get("sealno"));

        List<Map<String,Object>> retlist= EsealBaseDao.getSealNoToName(sealno);
        if(retlist.isEmpty()){
            return  retlist;
        }
         list= retlist.stream().map(item-> {
         Map<String,Object> itemmap=new HashMap<>(2);
         itemmap.put("yzmc",item.get("yzmc"));
         itemmap.put("yzbh",item.get("yzbh"));
         return itemmap;
     }).collect(Collectors.toList());
       return list;
    }
}
