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

import com.engine.core.impl.Service;
import com.engine.htxc.jzxcshb.cmd.JzxcshbFreeNodeCmd;
import com.engine.htxc.jzxcshb.service.JzxcshbService;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-22 14:00
 * @Description:竞争性磋商汇报相关业务方法
 * @ModifyLog:
 **/
public class JzxcshbServiceImpl extends Service implements JzxcshbService {
    @Override
    public Map<String, Object> createFreeNode(User user, Map<String, Object> params) {
        return commandExecutor.execute(new JzxcshbFreeNodeCmd(user,params));
    }

    @Override
    public Map<String, Object> judgeLdsp(Map<String, Object> param) {
        BaseBean bb = new BaseBean();
        String requestid = (String)param.get("requestid");
        JAVATools.writelog("招标汇报requestid"+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 = 'jzxcshb')) and unnumber = 'jzxcshb_ldsp'";
        JAVATools.writelog("招标汇报workSql"+workSql);
        rs.execute(workSql);
        if(rs.next()){
            String nodeid = rs.getString("id");
            String sql = "select * from  workflow_requestlog where requestid = "+requestid+" and nodeid = "+nodeid;
            bb.writeLog("招标汇报sql"+sql);
            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;
    }

    @Override
    public String getNewRequestLog(Map<String, Object> param) {
        String requestId = (String)param.get("requestId");
        String number = (String)param.get("number");
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("getNewRequestLog---requestId"+requestId);
        bb.writeLog("getNewRequestLog---number"+number);
        String nodeSql = "select * from workflow_nodebase where unnumber = "+number+"_ldsp";
        rs.getString(nodeSql);
        rs.next();
        String nodeId = rs.getString("nodeid");
        String sql = "select logtype from (select rownum ,w.* from workflow_requestlog w where requestid = '"+requestId+"' and nodeid = '"+nodeId+"' order by concat(w.operatedate,' ',w.operatetime) desc) where rownum = 1 ";
        rs.execute(sql);
        if(rs.next()){
            return rs.getString("logtype");
        }
        return null;
    }
}
