package com.engine.htsc.zbzysx.service;

import com.engine.core.impl.Service;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubImpSearchService extends Service {

    public Map<String,Object> getWorkflowInfo(String beginDate, String endDate, String searchType){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            String workflowid = pb.getActiveWorkflowIdByUnNumber("fzjgzysx");
            String tablename = JAVATools.GetUnumbertotable("fzjgzysx");
            String sblx = "";
            if("ksgt".equals(searchType)){
                sblx = pb.getPropName("subimportant-sblx-ksgt");
            }else if("tdrjcg".equals(searchType)){
                sblx = pb.getPropName("subimportant-sblx-tdrjcg");
            }
            List<Map<String,Object>> workflowInfoList = new ArrayList<>();

            String sql = "select t1.requestmark,t1.requestname,t3.loginid,t6.departmentname, " +
                    "(t1.createdate || ' ' || t1.createtime) as createtime,(t1.lastoperatedate || ' ' || t1.lastoperatetime) as endtime, t5.fj " +
                    "from workflow_requestbase t1 " +
                    "left join workflow_base t2 on t1.workflowid = t2.id " +
                    "left join hrmresource t3 on t3.id = t1.creater " +
                    "left join "+tablename+" t5 on t1.requestid = t5.requestid " +
                    "left join hrmdepartment t6 on t5.szbm = t6.id " +
                    "where t1.workflowid = '"+workflowid+"' and t1.currentnodetype = '3' and t5.sblx = '"+sblx+"'";
            if(StringUtils.isNotEmpty(beginDate)){
                sql += "and t1.lastoperatedate >= '"+beginDate+"'";
            }
            if(StringUtils.isNotEmpty(endDate)){
                sql += "and t1.lastoperatedate <= '"+endDate+"'";
            }
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                Map<String,Object> workflowInfoMap = new HashMap<>();
                workflowInfoMap.put("requestmark",Util.null2String(rs.getString("requestmark")));
                workflowInfoMap.put("requestname",Util.null2String(rs.getString("requestname")));
                workflowInfoMap.put("loginid",Util.null2String(rs.getString("loginid")));
                workflowInfoMap.put("departmentname",Util.null2String(rs.getString("departmentname")));
                workflowInfoMap.put("createtime",Util.null2String(rs.getString("createtime")));
                workflowInfoMap.put("endtime",Util.null2String(rs.getString("endtime")));
                String fj = Util.null2String(rs.getString("fj"));
                List<Map<String,String>> fileList = new ArrayList<>();
                if(StringUtils.isNotEmpty(fj)){
                    String sql2 = "select t1.fileid, t1.imagefilename from imagefile t1 left join docimagefile t2 on t2.imagefileid = t1.imagefileid where t2.docid in ("+fj+")";
                    bs.writeLog(sql2);
                    rs2.executeQuery(sql2);
                    while(rs2.next()){
                        Map<String,String> fileMap = new HashMap<>();
                        fileMap.put("fileid",Util.null2String(rs2.getString("fileid")));
                        fileMap.put("filename",Util.null2String(rs2.getString("imagefilename")));
                        fileList.add(fileMap);
                    }
                }
                workflowInfoMap.put("fileList",fileList);
                workflowInfoList.add(workflowInfoMap);
            }
            resultMap.put("api_status",true);
            resultMap.put("result",workflowInfoList);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("api_status",false);
            resultMap.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return resultMap;
    }

}
