package com.engine.htsc.seal.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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

/**
 * @author xf
 * @version 1.0.0
 * @ClassName OtherSealService.java
 * @createTime 2021年11月26日 00:44:00
 */
public class OtherSealService {


    public JSONArray getothertype(Map<String,Object> map){
        try {
            JSONArray jsonObject = new JSONArray();
            //Util.null2String(map.get("tablename"));
            String sysname = Util.null2String(map.get("sysname"));
            String syskey = Util.null2String(map.get("syskey"));

            if ("".equals(sysname) && "".equals(syskey)) {
                return null;
            }
            String tablename = JAVATools.GetUnumbertotable("yydjb");
            if ("".equals(tablename) ) {
                return null;
            }
            syskey= Arrays.stream(syskey.split(",")).map(value-> "\'"+value
                    +"\'").collect(Collectors.joining(","));
            String sql = "  select dt1.*,t.yyjg from "+tablename+"_dt1 dt1 " +
                    " join "+tablename+" t on dt1.mainid=t.id " +
                    "        where dt1.otherkey in ("+syskey+") and  t.wbxtly='"+sysname+"'";
            List<Map<String, String>> list = JAVATools.GetTableToListMap(sql,null);

            list.forEach((Map<String, String> model) -> {
                String wjmc = Util.null2String(model.get("wjmc"));
                String yyfs = Util.null2String(model.get("yyfs"));
                String yyzt = Util.null2String(model.get("yyzt"));
                String sfyy = Util.null2String(model.get("sfyy"));
                String dzyyfs = Util.null2String(model.get("dzyyfs"));
                String dsmdy = Util.null2String(model.get("dsmdy"));
                String wlyyfs = Util.null2String(model.get("wlyyfs"));
                String fwdyfs = Util.null2String(model.get("fwdyfs"));
                String yqxdyr = Util.null2String(model.get("yqxdyr"));
                String otherkey= Util.null2String(model.get("otherkey"));
                String yyjg= Util.null2String(model.get("yyjg"));
                if("".equals(yyzt)){
                    yyzt="0";
                }
                Map<String,Object> paramsmap=new HashMap<>();

                paramsmap.put("yyhwj",Util.null2String(model.get("yyhwj")));
                paramsmap.put("mainid",Util.null2String(model.get("mainid")));
                paramsmap.put("detailid",Util.null2String(model.get("id")));
                paramsmap.put("tablenametwo",tablename+"_2");

                String yqxdyrvalue = JAVATools.getHrmName(yqxdyr);

                if(!"".equals(yyfs)) {
                    if ("1".equals(yyfs)) {
                        //物理用印
                        JSONObject jsonObject1 = new JSONObject();

                        jsonObject1.put("key", otherkey);
                        jsonObject1.put("seal_way",sfyy );
                        jsonObject1.put("seal_type", yyzt);
                        jsonObject1.put("seal_state",yyfs );
                        jsonObject1.put("seal_phprintnum", wlyyfs); //物理用印份数
                        jsonObject1.put("workflow_state", yyjg);//用印结果
                        jsonObject.add(jsonObject1);
                    } else  if ("2".equals(yyfs) ||"0".equals(yyfs) )  {
                        //电子和物理
                        if("0".equals(dzyyfs)){
                            JSONObject jsonObject1 = new JSONObject();
                            jsonObject1.put("key",  otherkey);
                            jsonObject1.put("seal_way",sfyy );
                            jsonObject1.put("seal_type", yyzt);
                            jsonObject1.put("seal_state",yyfs );
                            jsonObject1.put("seal_elstate", dzyyfs);
                            jsonObject1.put("workflow_state", yyjg);//用印结果
                            if("2".equals(yyfs) ){
                                jsonObject1.put("seal_phprintnum", wlyyfs); //物理用印份数
                            }
                            Map<String,Object> filemap=getfilemap(paramsmap);
                            if(filemap!=null && filemap.size()>=0){
                                jsonObject1.put("seal_fileid", Util.null2String(filemap.get("fileid")));
                                jsonObject1.put("seal_filename",  Util.null2String(filemap.get("imagefilename")));
                            }

                            jsonObject.add(jsonObject1);
                        }else{
                            //公文电子用印件
                            JSONObject jsonObject1 = new JSONObject();
                            jsonObject1.put("key", otherkey);
                            jsonObject1.put("seal_elstate", dzyyfs);
                            jsonObject1.put("seal_way", sfyy);
                            jsonObject1.put("seal_type", yyzt);
                            jsonObject1.put("seal_state", yyfs);
                            jsonObject1.put("seal_sysuser", yqxdyrvalue);//有权限打印人
                            jsonObject1.put("seal_singledouble", dsmdy); //单双面打印
                            jsonObject1.put("seal_acprintnum", fwdyfs); //防伪打印份数
                            if("2".equals(yyfs) ){
                                jsonObject1.put("seal_phprintnum", wlyyfs); //物理用印份数
                            }
                            Map<String,Object> filemap=getfilemap(paramsmap);
                            if(filemap!=null && filemap.size()>=0){
                                jsonObject1.put("seal_fileid", Util.null2String(filemap.get("fileid")));
                                jsonObject1.put("seal_filename",  Util.null2String(filemap.get("imagefilename")));
                            }
                            jsonObject1.put("workflow_state", yyjg);//用印结果
                            jsonObject.add(jsonObject1);
                        }
                    }else if("3".equals(yyfs)){
                        //公文电子用印件
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("key", otherkey);
                        jsonObject1.put("seal_way",sfyy );
                        jsonObject1.put("seal_type", yyzt);
                        jsonObject1.put("seal_state", yyfs);
                        jsonObject1.put("seal_sysuser", yqxdyrvalue);//有权限打印人
                        jsonObject1.put("seal_singledouble", dsmdy); //单双面打印
                        jsonObject1.put("seal_acprintnum", fwdyfs); //防伪打印份数
                        jsonObject1.put("workflow_status", yyjg);//用印结果
                        Map<String,Object> filemap=getfilemap(paramsmap);
                        if(filemap!=null && filemap.size()>=0){
                            jsonObject1.put("seal_fileid", Util.null2String(filemap.get("fileid")));
                            jsonObject1.put("seal_filename",  Util.null2String(filemap.get("imagefilename")));
                        }
                        jsonObject.add(jsonObject1);
                    }else if ("4".equals(yyfs)){
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("key", otherkey);
                        jsonObject1.put("seal_way", yyfs);
                        jsonObject1.put("workflow_state", yyjg);//用印结果
                        jsonObject.add(jsonObject1);
                    }
                }
            });
            return jsonObject;
        }catch (Exception exo){
            return null;
        }
    }

    private String selectitem(String value,String tablename,String fieldname  ){
        String sql=" select rank() over (order by id )-1 id,selectname from workflow_selectitem t where t.fieldid in ( " +
                "  select id from workflow_billfield t where t.billid in （ select id from workflow_bill where tablename='"+tablename+"' ） and rownum=1 and t.fieldname='"+fieldname+"' " +
                " )";
        List<Map<String,Object> > list= JAVATools.GetTableToListMap(sql,null);
        if(list==null || list.size()<=0){
            return "";
        }
        List<String> liststr=new ArrayList<>();
        List<String> valuearray= Arrays.stream(value.split(",")).collect(Collectors.toList());
        list.forEach((Map<String,Object> model)->{
            if(valuearray.contains(Util.null2String(model.get("id"))) ){
                liststr.add(Util.null2String(model.get("selectname")));
            }
        });
        if(liststr==null && liststr.size()<=0){
            return "";
        }else{
            return  liststr.stream().collect(Collectors.joining(","));
        }
    }

    private Map<String,Object> getfilemap(Map<String,Object> paramsmap){
        Map<String,Object> map=new HashMap<>();
        String yyhwj=Util.null2String(paramsmap.get("yyhwj"));
        String detailid=Util.null2String(paramsmap.get("detailid"));
        String mainid=Util.null2String(paramsmap.get("mainid"));
        String tablenametwo=Util.null2String(paramsmap.get("tablename")+"_2");
        if("".equals(yyhwj)){
            return map;
        }
        if(yyhwj.split(",").length>=2){
            //
            String value= gettwoid(mainid,detailid,tablenametwo);
            if("".equals(value)){
                return getfile(value);
            }else{
                return map;
            }
        }else{
            //直接取
            return getfile(yyhwj);
        }

    }
    private String gettwoid(String mainid,String detailid,String tablename){

        if("".equals(detailid) && "".equals(mainid)){
            return "";
        }
        String sql=" select id from "+tablename+" where  dzyylx=1";
        Map<String,String> map=new HashMap<>();
        map.put("dtoneid",detailid);
        map.put("mainid",mainid);
        Map<String,Object> returnmap=JAVATools.GetTableToMap(sql,map);
        if(returnmap==null || returnmap.size()<=0){
            return "";
        }
        return Util.null2String(returnmap.get("id"));
    }

    private Map<String,Object>  getfile(String yyhwj){
        if("".equals(yyhwj)){
            return new HashMap<>();
        }
        yyhwj=JAVATools.getpreventsql(yyhwj);
        String sql="select d.imagefilename,t.fileid from imagefile t join docimagefile d on t.imagefileid=d.imagefileid " +
                "where d.docid ='"+yyhwj+"' and rownum=1 " +
                "order by  t.imagefileid desc ";
        return JAVATools.GetTableToMap(sql,null);
    }
}
