package com.engine.kq.cmd.setupwizard;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import java.util.HashMap;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 判断是否需要做一键初始化，检测如果存在历史一般工作时间等老考勤设置，给出提示让客户选择是否使用考勤设置升级
 */
public class CheckNeedInitCmd extends AbstractCommonCommand<Map<String, Object>> {

    public CheckNeedInitCmd(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>();
        boolean canInit = HrmUserVarify.checkUserRight("KQ:SetupWizard", user);//是否具有初始化的权限
        if (!canInit) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));//没有权限
            return resultMap;
        }
        boolean needInit = true;
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        //3个判断条件，如果检测到含有一般工作时间，或者设置了排班，或者有考勤流程设置，都给出提示
        String hasSchedule = "select * from hrmschedule ";
        rs1.executeQuery(hasSchedule);
        if(rs1.next()){
          needInit = false;
        }else{
          String hasShift = "select * from hrm_schedule_set_detail ";
          rs2.executeQuery(hasShift);
          if(rs2.next()){
            needInit = false;
          }else{
            String hasFlowset = "select * from hrm_att_proc_set ";
            rs3.executeQuery(hasFlowset);
            if(rs3.next()){
              needInit = false;
            }
          }
        }
        if(!needInit){
          resultMap.put("status", "2");
          String link = "<a href='/spa/hrm/staticAttendance4engine/engine.html#/attendance/upgrade' target='_blank'>"+SystemEnv.getHtmlLabelName(504038, user.getLanguage())+"</a>";
          resultMap.put("message", SystemEnv.getHtmlLabelName(516922, user.getLanguage())+"【"+link+"】"+SystemEnv.getHtmlLabelName(516923, user.getLanguage()));
        }else{
          resultMap.put("status", "1");
        }

      return resultMap;
    }
}
