package com.engine.kq.cmd.upgrade;

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

/**
 * 一般工作时间升级
 * @author pzy
 *
 */
public class DoUpgrade1Cmd extends AbstractCommonCommand<Map<String, Object>>{

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

	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String,Object> retmap = new HashMap<String,Object>();
    List<Object> items = new ArrayList<>();
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
		try {

		  KQOneStopUp kqOneStopUp = new KQOneStopUp();
      kqOneStopUp.clearCommonGroupData();
		  kqOneStopUp.upCommonGroupData();
      String groupids = "";
      String groupidSql = "select kqid from hrm_to_kq_group where groupfrom = 'scheduleset'";
      rs.executeQuery(groupidSql);
      while (rs.next()){
        String tmp_kqid = rs.getString("kqid");
        groupids += ","+tmp_kqid;
      }
      if(groupids.length() > 0){
        kqOneStopUp.upOvertimeRule(groupids, user,"scheduleset");
      }

      Map<String,Object> serialmap = new HashMap<String,Object>();
		  List<Object> serialList = new ArrayList<>();
      Map<String,Object> groupmap = new HashMap<String,Object>();
      List<Object> groupList = new ArrayList<>();

		  String getSerialSql = "select * from kq_shiftmanagement where id in( select kqid from hrm_to_kq_serial where serialfrom ='scheduleset')";
      rs.executeQuery(getSerialSql);
      while (rs.next()){
        serialList.add(rs.getString("serial"));
      }
      serialmap.put("label", SystemEnv.getHtmlLabelName(10000838,weaver.general.Util.getIntValue(user.getLanguage())));
      serialmap.put("status", "1");
      serialmap.put("results", serialList);
      items.add(serialmap);

      String getGroupSql = "select * from kq_group where id in(select kqid from hrm_to_kq_group where groupfrom = 'scheduleset')";
      rs1.executeQuery(getGroupSql);
      while (rs1.next()){
        groupList.add(rs1.getString("groupname"));
      }
      groupmap.put("label", SystemEnv.getHtmlLabelName(10000839,weaver.general.Util.getIntValue(user.getLanguage())));
      groupmap.put("status", "1");
      groupmap.put("results", groupList);
      items.add(groupmap);

			retmap.put("status", "1");
      retmap.put("items", items);
		} catch (Exception e) {
			writeLog(e);
      retmap.put("status", "-1");
      retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
		}
		return retmap;
	}

}
