package com.engine.kq.cmd.group;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 获取考勤组 排班周期列表
 */
public class GetShiftCycleFormCmd extends AbstractCommonCommand<Map<String, Object>> {

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

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
    Map<String,Object> shiftSchedulce = new HashMap<>();
    List<Map<String,Object>> grouplist = new ArrayList<Map<String,Object>>();
    Map<String,Object> groupitem = null;
    List<Object> itemlist = null;
    RecordSet rs = new RecordSet();
    String sql = "";
    try{
      String cycleserialid = Util.null2String(params.get("id"));
      Map<String, List<HrmFieldBean>> fieldGroups= new LinkedHashMap<String, List<HrmFieldBean>>();
      List<HrmFieldBean> lsField = new ArrayList<HrmFieldBean>();
      HrmFieldBean hrmFieldBean = null;
      List<SearchConditionOption> options = null;
      Map<String, Object> otherparam = null;

      String shiftcyclename = "";//周期名称
      String shiftcycleday = "";//周期天数
      String shiftcycleserialids = "";//考勤班次
      if(cycleserialid.length()>0){
        sql = "select * from kq_group_shiftcycle where id=?";
        rs.executeQuery(sql,cycleserialid);
        if(rs.next()){
          shiftcyclename = Util.null2String(rs.getString("shiftcyclename"));
          shiftcycleday = Util.null2String(rs.getString("shiftcycleday"));
          shiftcycleserialids = Util.null2String(rs.getString("shiftcycleserialids"));
        }
      }

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("shiftcyclename");
      hrmFieldBean.setFieldlabel("388722");
      hrmFieldBean.setFieldhtmltype("1");
      hrmFieldBean.setType("1");
      hrmFieldBean.setViewAttr(3);
      hrmFieldBean.setRules("required|string");
      hrmFieldBean.setFieldvalue(shiftcyclename);
      lsField.add(hrmFieldBean);

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("shiftcycleday");
      hrmFieldBean.setFieldlabel("389201");
      hrmFieldBean.setFieldhtmltype("1");
      hrmFieldBean.setType("2");
      hrmFieldBean.setFieldvalue(shiftcycleday.length()==0?2:shiftcycleday);
      otherparam = new HashMap<String, Object>();
      otherparam.put("min","2");
      otherparam.put("max","31");
      hrmFieldBean.setOtherparam(otherparam);
      hrmFieldBean.setViewAttr(3);
      hrmFieldBean.setRules("required|integer");
      lsField.add(hrmFieldBean);
      fieldGroups.put("389128", lsField);

      HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
      SearchConditionItem searchConditionItem = null;
      Iterator<Entry<String, List<HrmFieldBean>>> iter = fieldGroups.entrySet().iterator();
      while (iter.hasNext()) {
        Entry<String, List<HrmFieldBean>> entry = iter.next();
        String grouplabel = entry.getKey();
        List<HrmFieldBean> fields = entry.getValue();
        groupitem = new HashMap<String, Object>();
        groupitem.put("title", SystemEnv.getHtmlLabelNames(grouplabel, user.getLanguage()));
        groupitem.put("defaultshow", true);

        itemlist = new ArrayList<Object>();
        for (int j = 0; j < fields.size(); j++) {
          hrmFieldBean = fields.get(j);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          itemlist.add(searchConditionItem);
        }
        groupitem.put("items", itemlist);
        grouplist.add(groupitem);
      }
      shiftSchedulce.put("formField", grouplist);
      shiftSchedulce.put("shiftcycleserialids",shiftcycleserialids);
    }catch (Exception e){
      writeLog(e);
    }
    return shiftSchedulce;
  }

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

}
