package com.engine.kq.cmd.setupwizard;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.constant.*;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.LogUtil;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQInitializationBiz;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 执行初始化
 */
public class PerformInitializationCmd extends AbstractCommonCommand<Map<String, Object>> {

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

        //获取当前日期，当前时间
        Calendar today = Calendar.getInstance();
        //当前日期
        String currentDate = Util.add0(today.get(Calendar.YEAR), 4) + "-" +
                Util.add0(today.get(Calendar.MONTH) + 1, 2) + "-" +
                Util.add0(today.get(Calendar.DAY_OF_MONTH), 2);
        //当前时间
        String currentTime = Util.add0(today.get(Calendar.HOUR_OF_DAY), 2) + ":" +
                Util.add0(today.get(Calendar.MINUTE), 2) + ":" +
                Util.add0(today.get(Calendar.SECOND), 2);

        int index = Util.getIntValue((String) params.get("index"), -1);//索引值
        if (index == 0) {
            BizLogContext bizLogContext = new BizLogContext();
            bizLogContext.setDate(currentDate);
            bizLogContext.setTime(currentTime);
            bizLogContext.setUserid(user.getUID());
            bizLogContext.setUsertype(Util.getIntValue(user.getLogintype(), 1));
            bizLogContext.setTargetId("");
            bizLogContext.setTargetName(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(505591,weaver.general.ThreadVarLanguage.getLang())+"");
            bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_SETUPWIZARD);//所属大类型
            bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_SETUPWIZARD);//所属大类型下的小类型
            bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
            bizLogContext.setClientIp("");
            bizLogContext.setDesc(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(505591,weaver.general.ThreadVarLanguage.getLang())+"");
            bizLogContext.setParams(params);
            bizLogContext.setOperateType(BizLogOperateType.START);
            bizLogContext.setOperateAuditType(BizLogOperateAuditType.INFO);
            bizLogContext.setDeviceType(BizLogDeviceType.PC);
            LogUtil.writeBizLog(bizLogContext);
        }
    }

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        boolean canInit = HrmUserVarify.checkUserRight("KQ:SetupWizard", user);//是否具有初始化的权限
        if (!canInit) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));//没有权限
            return resultMap;
        }
        int index = Util.getIntValue((String) params.get("index"), -1);//索引值
        if (index == 0) {
            resultMap = KQInitializationBiz.initShift(params, user);
        } else if (index == 1) {
            resultMap = KQInitializationBiz.initGroup(params,user);
        } else if (index == 2) {
            resultMap = KQInitializationBiz.initWorkflow(params, user);
        } else if (index == 3) {
            resultMap = KQInitializationBiz.initHolidaySet(params,user);
        } else if (index == 4) {
            resultMap = KQInitializationBiz.initOvertimeRules(params,user);
        } else if (index == 5) {
            resultMap = KQInitializationBiz.initTravelRules(params,user);
        } else if (index == 6) {
            resultMap = KQInitializationBiz.initLeaveRules(params,user);
        } else if (index == 7) {
            resultMap = KQInitializationBiz.initBalanceOfLeave(params,user);
        }
        return resultMap;
    }
}
