package com.engine.htsc.cgscfyqr.dao;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CgscfyqrCMD extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public CgscfyqrCMD(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        BaseBean bb = new BaseBean();
        Map<String, Object> serviceBack = new HashMap<>();
        RecordSet rs = new RecordSet();
        String sql = "";
        String wfid = Util.null2String(params.get("wfid"));
        String nyr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String sfm = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        String id = Util.null2String(params.get("id"));
        if ("id".equals(Util.null2String(params.get("type")))) {
            String empowerId = "";
            serviceBack.put("type", "id");
//            需要返回被授权人的id
//            select beagenterid from workflow_agent where workflowid = '46021' and agenterid = '36978' and ENDDATE >= '2021-12-03' and ENDTIME >= '20:05:36'
            sql = "select beagenterid from workflow_agent where workflowid = '" + wfid + "' and " +
                    "agenterid = '" + id + "' and ENDDATE >= '" + nyr + "' and ENDTIME >= '" + sfm + "'";
            bb.writeLog("即将执行获取被授权人的idSQL语句是：" + sql);
            rs.executeQuery(sql);
            if (rs.next()) {
                empowerId = rs.getString("beagenterid");
            }
            serviceBack.put("empowerId", getAllRolesByID("".equals(empowerId) ? id : empowerId));
        } else if ("dept".equals(Util.null2String(params.get("type")))) {
//            需要返回被授权人的dept
            String beagenterid = "";
            String empowerDeptid = "";
            serviceBack.put("type", "dept");
            sql = "select beagenterid " +
                    "from workflow_agent where workflowid = '"+wfid+"' and agenterid = '"+id+"' and" +
                    " ENDDATE >= '"+nyr+"' and ENDTIME >= '"+sfm+"'";
            bb.writeLog("即将执行获取被授权人的beagenterid SQL语句是：" + sql);
            rs.executeQuery(sql);
            if (rs.next()){
                beagenterid = rs.getString("beagenterid");
            }
            serviceBack.put("empowerDeptid",getAllDeptidByID("".equals(beagenterid) ? id : beagenterid));
        }
        return serviceBack;
    }

    private String getAllRolesByID(String id){
        List<String> allRoleId = new ArrayList<>();
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        String sql = "";
        String loginid = "";
        sql = "select loginid from hrmresource where id = '"+id+"'";
        rs.executeQuery(sql);
        if (rs.next()){
            loginid = Util.null2String(rs.getString("loginid"));
        }
        if ("".equals(loginid)){
            return id;
        }else {
            sql = "select id from hrmresource where loginid like '"+loginid+"%'";
            rs.executeQuery(sql);
            while (rs.next()){
                allRoleId.add(Util.null2String(rs.getString("id")));
            }
            return String.join(",",allRoleId);
        }
    }

    private String getAllDeptidByID(String id){
        List<String> allDeptid = new ArrayList<>();
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        String sql = "";
        String loginid = "";
        sql = "select loginid from hrmresource where id = '"+id+"'";
        rs.executeQuery(sql);
        if (rs.next()){
            loginid = Util.null2String(rs.getString("loginid"));
        }
        sql = "select departmentid from hrmresource where loginid like '"+loginid+"%'";
        rs.executeQuery(sql);
        while (rs.next()){
            allDeptid.add(Util.null2String(rs.getString("departmentid")));
        }
        return String.join(",",allDeptid);
    }
}
