package com.engine.kq.cmd.balanceofleaverp;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQLeaveRulesComInfo;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

import java.util.*;

/**
 * 员工假期余额报表--获取TAB页签
 */
public class GetTabCmd extends AbstractCommonCommand<Map<String, Object>> {

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            int resourceId = Util.getIntValue((String) params.get("resourceId"), -1);//人员ID
            String ruleId = Util.null2String(params.get("ruleId"));//假期规则的ID
            if (resourceId == -1) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(388858, user.getLanguage()));//参数有误
                return resultMap;
            }

            /**
             * 加载TAB页签：
             * 1-只加载启用的假期规则
             */
            Map<String, Object> valueMap = new LinkedHashMap<String, Object>();//假期规则集合
            KQLeaveRulesComInfo kqLeaveRulesComInfo = new KQLeaveRulesComInfo();
            kqLeaveRulesComInfo.setTofirstRow();
            while (kqLeaveRulesComInfo.next()) {
                if (kqLeaveRulesComInfo.getIsEnable().equals("0")) {
                    continue;
                }
                valueMap.put(Util.formatMultiLang(kqLeaveRulesComInfo.getLeaveName(), "" + user.getLanguage()), kqLeaveRulesComInfo.getId());
            }

            /**
             * 拼装前端TAB页签需要的数据
             */
            int index = 0;
            Map<String, Object> tabMap = new HashMap<String, Object>();
            List<Map<String, Object>> tabList = new ArrayList<Map<String, Object>>();
            for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
                tabMap = new HashMap<String, Object>();
                tabMap.put("key", entry.getValue().toString());
                tabMap.put("title", entry.getKey());
                tabList.add(tabMap);

                /**
                 * 直接点击人员姓名查看假期使用记录时，ruleId是为空的，默认为第一个TAB页签
                 */
                if (index == 0 && ruleId.equals("")) {
                    ruleId = entry.getValue().toString();
                }
                index++;
            }
            resultMap.put("tab", tabList);
            resultMap.put("selectedKey", ruleId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
