package com.engine.htsc.ydbx.dao;

import com.alibaba.fastjson.JSONObject;
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.Util;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

import java.util.*;

public class GetGoalCanCreateCMD extends AbstractCommonCommand<Map<String, Object>> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 属于分支机构的部门类型
     */
    private static String[] branchDept = {"JGLX02", "JGLX03", "JGLX04", "JGLX05", "JGLX06"};

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        logger.info("接收到的参数是：" + JSONObject.toJSONString(params));
        Map<String, Object> result = new HashMap<>();
        List<Map<String, String>> district = new ArrayList<>();
        String depttypecode = "";
        Map<String,String> connetDeptAndId = new HashMap<>();
        RecordSet rs = new RecordSet();
        String sql;
        try {
            String loginId = Util.null2String(params.get("loginId"));
            // 0：通用报销     1：差旅报销
            String bxlx = Util.null2String(params.get("bxlx"));
            // 获取部门类型
            sql = "select a.DEPTTYPECODE,b.id from hrmdepartmentdefined a,hrmresource b where a.DEPTID = b.DEPARTMENTID and b.loginid like '" + loginId + "%'";
            rs.executeQuery(sql);
            logger.info("即将执行的sql是：" + sql);
            List<String> bmlxs = new ArrayList<>();
            while (rs.next()) {
                depttypecode = Util.null2String(rs.getString("DEPTTYPECODE"));
                String userid = Util.null2String(rs.getString("id"));
                if (!"".equals(depttypecode)) {
                    String bmlx = "JGLX01".equals(depttypecode) ? "0" : Arrays.asList(branchDept).contains(depttypecode) ? "1" : "2";
                    logger.info("部门类型是：" + bmlx);
                    if (!bmlxs.contains(bmlx)) {
                        bmlxs.add(bmlx);
                        connetDeptAndId.put(bmlx,userid);
                    }
                }
            }
            if (bmlxs.size() != 0) {
                for (String bmlx : bmlxs) {
                    sql = "select a.UNNUMBER,b.WORKFLOWNAME from uf_lcgl a,workflow_base b where a.BXLX = ? and a.BMLX = ? and a.UNNUMBER = b.UNNUMBER and b.ISVALID = 1";
                    rs.executeQuery(sql, bxlx, bmlx);
                    if (rs.next()) {
                        Map<String, String> okWf = new HashMap<>();
                        okWf.put("value", Util.null2String(rs.getString("UNNUMBER")));
                        okWf.put("label", Util.null2String(rs.getString("WORKFLOWNAME")));
                        okWf.put("userid",Util.null2String(connetDeptAndId.get(bmlx)));
                        district.add(okWf);
                    }
                }
            }
            result.put("isSuccess", "true");
        } catch (Exception e) {
            logger.info("发生错误：" + e.getMessage());
            result.put("isSuccess", "false");
        } finally {
            result.put("district", district);
        }
        logger.info("返回给前端的值是：" + result);
        return result;
    }
}
