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.interfaces.htsc.comInfo.PropBean;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

import java.util.*;

/**
 * @description 移动报销给予用户部门维度选择需要创建的路程
 * @参数说明 loginId-用户登录ID
 * bxlx-报销类型：0：通用报销     1：差旅报销
 * bmlx-部门类型：0：总部    1：分支机构    2：子公司
 * 由于目前没有子公司的报销类型，所以目前此方法，仅支持总部和分支机构的移动报销；
 * 后期如果适配子公司移动报销，维护uf_lcgl表即可
 */
public class OpenWfByDeptCMD 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 OpenWfByDeptCMD(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<>();
        RecordSet rs = new RecordSet();
        String sql;
        try {
            String loginId = Util.null2String(params.get("loginId"));
            String bxlx = Util.null2String(params.get("bxlx"));
            sql = "select a.DEPTTYPECODE,b.id,c.DEPARTMENTNAME from hrmdepartmentdefined a,hrmresource b,hrmdepartment c " +
                    "where a.DEPTID = b.DEPARTMENTID  and a.DEPTID = c.id and b.loginid like '" + loginId + "%' and b.workcode = '" + loginId + "'";
            logger.info("即将执行的sql是：" + sql);
            rs.executeQuery(sql);
            while (rs.next()) {
                Map<String, String> okWf = new HashMap<>();
                String value = Util.null2String(getWfnumberByDeptType(Util.null2String(rs.getString("DEPTTYPECODE")), bxlx));
                String label = Util.null2String(rs.getString("DEPARTMENTNAME"));
                String userid = Util.null2String(rs.getString("ID"));
                okWf.put("value", userid);
                okWf.put("label", label);
                okWf.put("wfid", new PropBean().getActiveWorkflowIdByUnNumber(value));
                okWf.put("lclx", value);
                if (!"".equals(value) && !"".equals(label) && !"".equals(userid)) {
                    district.add(okWf);
                }
            }
            result.put("isSuccess", "true");
        } catch (Exception e) {
            logger.info("发生错误：" + e.getMessage());
            result.put("isSuccess", "false");
        } finally {
            // 去除相同部门
            district = deduplicationListMap(district);
            result.put("district", district);
        }
        logger.info("返回给前端的值是：" + result);
        return result;
    }

    private String getWfnumberByDeptType(String depttypecode, String bxlx) {
        RecordSet rs = new RecordSet();
        String sql;
        String bmlx = "JGLX01".equals(depttypecode) ? "0" : Arrays.asList(branchDept).contains(depttypecode) ? "1" : "2";
        sql = "select a.UNNUMBER 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()) {
            return Util.null2String(rs.getString("UNNUMBER"));
        } else {
            return "";
        }
    }

    /**
     * 结果去重
     *
     * @param district
     * @return
     */
    private List<Map<String, String>> deduplicationListMap(List<Map<String, String>> district) {
        List<Map<String, String>> deduplicationResult = new ArrayList<>();
        List<String> values = new ArrayList<>();
        for (Map<String, String> stringStringMap : district) {
            String label = Util.null2String(stringStringMap.get("label"));
            if (!values.contains(label)) {
                values.add(label);
                deduplicationResult.add(stringStringMap);
            }
        }
        return deduplicationResult;
    }
}
