package com.engine.htsc.lhcd.Service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.lhcd.Service.LhcdunifiedService;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

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

/**
 * @author xf
 * @version 1.0.0
 * @ClassName LhcdunifiedServiceimpl.java
 * @createTime 2021年12月29日 10:13:00
 */
public class LhcdunifiedServiceimpl extends Service implements LhcdunifiedService {

    private String logname="联合档案电子化平台接口:";

    @Override
    public void  innertunified(Map<String, String> map) {
        JAVATools.SetCmdwritelog(logname+"开始执行!");
        JSONObject jsonObject=new JSONObject();
        JSONArray  jsonArray=new JSONArray();
       String requestid = Util.null2String(map.get("requestid"));
       if("".equals(requestid)){
           return ;
       }
       String tablename= JAVATools.getMaintable(requestid);
       Map<String,Object> mapvalue=JAVATools.gettablevalue(requestid);
       if(mapvalue==null || mapvalue.size()<=0){
           return ;
       }
        String mainid=Util.null2String(mapvalue.get("id"));
        String cdfl=Util.null2String(mapvalue.get("cdfl"));
        String ngrq=Util.null2String(mapvalue.get("ngrq"));
        String ngr=Util.null2String(mapvalue.get("ngr"));
        String lcbh=Util.null2String(mapvalue.get("lcbh"));
        String szbm=Util.null2String(mapvalue.get("szbm"));
        String cdlc=Util.null2String(mapvalue.get("cdlc"));
        String cdfj=Util.null2String(mapvalue.get("cdfj"));
        String cdxz=Util.null2String(mapvalue.get("cdxz"));
        Map<String,Object> cddatavalue=JAVATools.gettablevalue(cdlc);
        int years=Util.getIntValue(ngrq.substring(0,4));
        Map<String,Object> deptmap=JAVATools.getdeptmap(szbm);
        if(deptmap==null || deptmap.size()<=0){
            return ;
        }
        if("".equals(cdfj)  || !"0".equals(cdxz) ){
            return ;
        }
        String ngrdeptname=Util.null2String(deptmap.get("departmentname"));
        String departmentcode =Util.null2String(deptmap.get("departmentcode"));
        String responsibleParty=getHtTypeString(JAVATools.getMaintable(cdlc),Util.null2String(cddatavalue.get("id")));
        String storageProcessTitle=JAVATools.getrequestname(requestid);//存档标题
        String storageFullName=JAVATools.gethrmidtoworkcode(ngr); //存档拟稿人
        String storageDept=ngrdeptname; //存档拟稿人部门
        String workflowurl="";
        String processUrl=Util.null2String(new PropBean().getPropName("lhcd.workflowurl"));//存档登记流程URL 档案流程访问URL
        processUrl= MessageFormat.format(processUrl,requestid);
        String storageFilesNo=lcbh; //OA流程编号
        String oaProcessId=""; //流程实例编号
        jsonObject.put("years",years);
        jsonObject.put("responsibleParty",responsibleParty);
        jsonObject.put("storageProcessTitle",storageProcessTitle);
        jsonObject.put("storageFullName",storageFullName);
        jsonObject.put("storageDept",storageDept);
        jsonObject.put("processUrl",processUrl);
        jsonObject.put("storageFilesNo",storageFilesNo);
        jsonObject.put("oaProcessId",oaProcessId);
        Arrays.stream(cdfj.split(",")).forEach((docid)->{
            jsonArray.add(getdetail(cdfl,cddatavalue,docid,deptmap));
        });
        jsonObject.put("documentList",jsonArray); // //附件内容集合
        JSONArray jsonreturn=new JSONArray();
        jsonreturn.add(jsonObject);
        JAVATools.SetCmdwritelog(logname+"参数查看：",jsonreturn.toString());
        String url=new PropBean().getPropName("lhcd.sendurl");
        Map<String,String> headermap= new HTUtil().getAppHeader();
        String repstr= new HTUtil().httpPostRaw(url,jsonreturn.toJSONString(),headermap,null);
        JAVATools.SetCmdwritelog(logname+"调用之后的参数：",repstr);

        if("".equals(repstr)){
            return ;
        }
        try{
           JSONObject jonsobj= JSONObject.parseObject(repstr);
           String code=Util.null2String(jonsobj.get("code"));
            if("1".equals(code)){
                JAVATools.SetCmdwritelog(logname,"调用成功："+requestid);
            }else{
                JAVATools.SetCmdwritelog(logname,"调用失败："+requestid);

            }
        } catch (Exception e){
            JAVATools.SetCmdwritelog(logname,"调用失败："+requestid);
            JAVATools.SetCmdwritelog(logname,"调用失败："+e.getMessage());
        }
    }

    private  JSONObject getdetail(String httype,Map<String,Object> cddatavalue,String docid,Map<String,Object> deptmap){

        JSONObject jsonObject=new JSONObject();
        Map<String,Object> filemap=JAVATools.getfilemap(docid);
        int filesType=0;
        String contractMoney="";
        String documentOwningDebt=Util.null2String(deptmap.get("departmentcode"));
        String documentOwningDeptName=Util.null2String(deptmap.get("departmentname"));
        String fileid=Util.null2String(filemap.get("fileid"));
        String documentTitle=Util.null2String(filemap.get("filename"));
        int secretGrade=1;
        int storageSum=1;//份数
        int currencyType=1;
         if("0".equals(httype)){
            filesType=1;
            //获取流程主表的数据库
             contractMoney=Util.null2String(cddatavalue.get("htje"));
             String mjvalue=Util.null2String(cddatavalue.get("mj"));
             secretGrade=getmjtype(mjvalue);
        }else if("1".equals(httype)){
            filesType=2;
        }
        jsonObject.put("secretGrade",secretGrade);
        jsonObject.put("storageSum",storageSum);
        jsonObject.put("currencyType",currencyType);
        jsonObject.put("filesType",filesType);
        jsonObject.put("contractMoney",contractMoney);
        jsonObject.put("documentTitle",documentTitle);
        jsonObject.put("documentNo","");
        if(!"".equals(fileid)){
            fileid=new PropBean().getPropName("lhcd.filedownload")+fileid;
        }

        jsonObject.put("attachmentUrl",fileid);

        jsonObject.put("documentOwningDebt",documentOwningDebt);
        jsonObject.put("documentOwningDeptName",documentOwningDeptName);
        return jsonObject;
    }

    private int getmjtype(String type){
        ////密级  1=一般 2=秘密 3=机密 4=绝密
        int value=-1;
        switch (type){
            case "0":
                value=1;
                break;
            case "1":
                value=2;
                break;
            case "2":
                value=3;
                break;
            case "3":
                value=4;
                break;
        }
     return value;
    }

    private String getHtTypeString(String tablename,String id){
        String sql=" select * from "+tablename+"_dt12   where 1=1 ";
        Map<String,String> map=new HashMap<>();
        map.put("mainid",id);
        List<String> liststr=new ArrayList<>();
        List<Map<String,Object>> listdata= JAVATools.GetTableToListMap(sql,map);
        listdata.forEach((Map<String,Object> value)->{
            String itemvalue=getHtType(value);
            if(!"".equals(itemvalue)){
                liststr.add(itemvalue);
            }
        });
        if(liststr.size()<=0){
            return "";
        }
        return liststr.stream().distinct().collect(Collectors.joining(":"));
    }

    private  String getHtType(Map<String,Object> map){
        String retvalue="";
        String htfvalue=Util.null2String(map.get("htf"));
        String htfmcvalue=Util.null2String(map.get("htfmc"));
        String strvalue="甲,乙,丙,丁,戊,己,庚,辛,壬,癸";
        List<String> list=Arrays.stream(strvalue.split(",")).map((item)->{
            return "合同"+item+"方";
        }).collect(Collectors.toList());
         int indexcount=list.indexOf(htfvalue);
         if(indexcount>=0) {
             retvalue=(indexcount+1)+","+htfmcvalue;
         }

         return retvalue;
    }
}
