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

import com.engine.core.impl.Service;
import com.engine.htxc.jzxcsqs.cmd.JzxcsqsFreeNodeCmd;
import com.engine.htxc.jzxcsqs.service.JzxcsqsService;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.hrm.User;

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

public class JzxcsqsServiceImpl extends Service implements JzxcsqsService {
    @Override
    public Map<String, Object> createFreeNode(User user, Map<String, Object> params) {
        return commandExecutor.execute(new JzxcsqsFreeNodeCmd(user,params));
    }

    @Override
    public Map<String, Object> judgeLdsp(Map<String, Object> param) {
        String requestid = (String)param.get("requestid");
        HashMap<String, Object> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        String workSql = "select id from  workflow_nodebase where id in (select nodeid  from workflow_flownode where workflowid   = (select id from workflow_base where unnumber = 'jzxcsqs')) and unnumber = 'jzxcsqs_ldsp'";
        rs.execute(workSql);
        if(rs.next()){
            String nodeid = rs.getString("id");
            String sql = "select * from  workflow_requestlog where requestid = "+requestid+" and nodeid = "+nodeid;
            rs.execute(sql);
            if(rs.next()){
                map.put("data",true);
            }else{
                map.put("data",false);
            }
        }

        return map;
    }
    @Override
    public Map<String, Object> getTzydry(Map<String, Object> param) {

        HashMap<String, Object> result = new HashMap<>();
        String jmid = (String)param.get("jmid");
        ArrayList<String> ids = new ArrayList<>();
        ArrayList<HashMap<String,String>> specialobj = new ArrayList<>();
        RecordSet rs = new RecordSet();
        if(StringUtils.isNotBlank(jmid)){
            String ufSql ="select xmfzr from uf_cgxq where id =" + jmid;
            rs.execute(ufSql);
            if(rs.next()){
                String xmfzr = rs.getString("xmfzr");
                String hrmSql = "select id,lastname from hrmresource where id in ("+xmfzr+")";
                rs.execute(hrmSql);
                while(rs.next()){
                    String id = rs.getString("id");
                    String lastname = rs.getString("lastname");
                    HashMap<String, String> map = new HashMap<>();
                    map.put("id",id);
                    map.put("name",lastname);
                    ids.add(id);
                    specialobj.add(map);
                }
            }
            result.put("value",ids);
            result.put("specialobj",specialobj);
        }
        return result;
    }
}
