package com.engine.kq.cmd.leavetypes;

import com.api.browser.bean.SearchConditionItem;
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 com.engine.kq.enums.KqSplitFlowTypeEnum;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

public class GetTimeSelectionFormCmd extends AbstractCommonCommand<Map<String, Object>> {

  public GetTimeSelectionFormCmd(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> retmap = new HashMap<String, Object>();
    try{
      //半天设置的类型，是请假0，还是出差1，公出2，加班3
      int selectiontype = Util.getIntValue(Util.null2String(params.get("selectiontype")),-1);
      //请假可以细分到每一种请假类型，leaveruleid为请假类型的id
      int leaveruleid = Util.getIntValue(Util.null2String(params.get("leaveruleid")),-1);
      //最小请假单位：2-按半天请假、4-按整天请假
      //目前只做2半天的情况
      int minimumUnit = Util.getIntValue((String) params.get("minimumUnit"), 2);
      boolean isEdit = false;
      String half_on = "";
      String half_off = "";
      String whole = "";
      if(selectiontype == KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
        if(selectiontype < 0 || leaveruleid == -1){
          retmap.put("status", "-1");
          writeLog("SaveTimeSelectionForm:参数有问题:selectiontype:"+selectiontype+":leaveruleid:"+leaveruleid);
          retmap.put("message", SystemEnv.getHtmlLabelName(514241, user.getLanguage()));
          return retmap;
        }
      }else{
        if(selectiontype < 0){
          retmap.put("status", "-1");
          writeLog("SaveTimeSelectionForm:参数有问题:selectiontype:"+selectiontype+":leaveruleid:"+leaveruleid);
          retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
          return retmap;
        }
      }
      RecordSet rs = new RecordSet();
      String editSql = "";
      if(selectiontype == KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
        editSql = "select * from kq_timeselection where selectiontype="+ selectiontype+" and leaveruleid="+leaveruleid+" and minimumUnit="+minimumUnit;
      }else {
        editSql = "select * from kq_timeselection where selectiontype="+ selectiontype+" and minimumUnit="+minimumUnit;
      }
      rs.executeQuery(editSql);
      if(rs.next()){
        isEdit = true;
        half_on  = Util.null2String(rs.getString("half_on"));
        half_off  = Util.null2String(rs.getString("half_off"));
        whole  = Util.null2String(rs.getString("whole"));
      }

      List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();
      Map<String, Object> groupItem = new HashMap<String, Object>();
      List<Object> itemList = new ArrayList<Object>();
      HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
      SearchConditionItem searchConditionItem = null;
      HrmFieldBean hrmFieldBean = null;
      if(minimumUnit == 2){
        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("half_on");//上午
        hrmFieldBean.setFieldlabel("16689");
        hrmFieldBean.setFieldhtmltype("1");
        hrmFieldBean.setType("1");
        hrmFieldBean.setFieldvalue(isEdit?half_on:"");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setMultilang(true);
        hrmFieldBean.setViewAttr(2);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("half_off");//下午
        hrmFieldBean.setFieldlabel("16690");
        hrmFieldBean.setFieldhtmltype("1");
        hrmFieldBean.setType("1");
        hrmFieldBean.setFieldvalue(isEdit?half_off:"");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setMultilang(true);
        hrmFieldBean.setViewAttr(2);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        itemList.add(searchConditionItem);
      }else if(minimumUnit == 4){
        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("whole");//整天
        hrmFieldBean.setFieldlabel("390728");
        hrmFieldBean.setFieldhtmltype("1");
        hrmFieldBean.setType("1");
        hrmFieldBean.setFieldvalue(isEdit?whole:"");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setMultilang(true);
        hrmFieldBean.setViewAttr(2);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        itemList.add(searchConditionItem);
      }

      groupItem.put("items", itemList);
      groupList.add(groupItem);
      retmap.put("status", "1");
      retmap.put("condition", groupList);

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

