package com.engine.htsc.zhfw.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.zhfw.service.ComprehensiveArticleService;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;

public class ComprehensiveArticleServiceImpl  extends Service implements ComprehensiveArticleService {

    @Override
    public Map<String, Object> examApproval(String requestid) {
        Map<String,Object> data = new HashMap<>();
        boolean result = false;
        try {
            RecordSet rs = new RecordSet();
            Map<String , String > map = new HashMap<>();
            rs.executeQuery("select id,UNNUMBER from WORKFLOW_NODEBASE  where unnumber in ('zhfw_sxcwg','zhfw_hgzj','zhfw_sxfxg')\n");
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                String unnumber = Util.null2String(rs.getString("unnumber"));
                map.put(unnumber,id);
            }
            //0 同意 1 不同意
            int sxcwg = 1;
            int hgzj = 1;
            int sxfxg = 1;
            rs.executeQuery("select  * from (select REQUESTID,nodeid,logtype,logid,OPERATEDATE,OPERATETIME,row_number() over(partition by nodeid order by logid desc) as rank\n" +
                    "from WORKFLOW_REQUESTLOG where\n" +
                    "NODEID in (select id from WORKFLOW_NODEBASE  where unnumber in('zhfw_sxcwg','zhfw_hgzj','zhfw_sxfxg')\n" +
                    ") and REQUESTID = ?) where rank < 2",requestid);
            while (rs.next()) {
                String nodeid = Util.null2String(rs.getString("nodeid"));
                int logtype = Util.getIntValue(rs.getString("logtype"));
                for(Map.Entry<String, String> entry : map.entrySet()){
                    String mapKey = entry.getKey();
                    String mapValue = entry.getValue();
                    if ("zhfw_sxcwg".equals(mapKey)) {
                        if (nodeid.equals(mapValue)) {
                            sxcwg = logtype;
                            break;
                        }
                    }else if ("zhfw_hgzj".equals(mapKey)) {
                        if (nodeid.equals(mapValue)) {
                            hgzj = logtype;
                            break;
                        }
                    }else if ("zhfw_sxfxg".equals(mapKey)) {
                        if (nodeid.equals(mapValue)) {
                            sxfxg = logtype;
                            break;
                        }
                    }
                }
            }
            data.put("zhfw_sxcwg",sxcwg);
            data.put("zhfw_hgzj",hgzj);
            data.put("zhfw_sxfxg",sxfxg);
            if (sxcwg == 0 && hgzj == 0 && sxfxg == 0){
                result = true;
            }
            data.put("result",result);


        }catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}
