package com.engine.kq.cmd.upgrade;

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.List;
import java.util.Map;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.attendance.manager.HrmAttProcSetManager;
import weaver.systeminfo.SystemEnv;

/**
 * 历史考勤流程数据升级 准备
 * @author pzy
 *
 */
public class GetUpgrade10Cmd extends AbstractCommonCommand<Map<String, Object>>{

	public GetUpgrade10Cmd(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>();
    String type = Util.null2String((String)params.get("type"));
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
    int signSetCntInt = 0;
    Map<String,Integer> signMap = new HashMap<>();
    List<Object> itemList = new ArrayList<Object>();
    KQUpgradeTabEnum kqUpgradeTabEnum = KQUpgradeTabEnum.Upgrade10;
    try {
//      if(type.equalsIgnoreCase(kqUpgradeTabEnum.getTabtype())){
//        String attSql = "select * from hrm_att_proc_set";
//        rs.executeQuery(attSql);
//        while (rs.next()){
//          String flowtype = rs.getString("field006");
//          String flowsql = getFlowSql(flowtype);
//          if(flowsql.length() > 0){
//            rs1.executeQuery(flowsql);
//            int tmpSignCnt = rs1.getCounts();
//            signSetCntInt += tmpSignCnt;
//            if(signMap.containsKey(flowtype)){
//              int tmpCnt = signMap.get(flowtype);
//              signMap.put(flowtype, (tmpCnt+tmpSignCnt));
//            }else{
//              signMap.put(flowtype, (tmpSignCnt));
//            }
//          }
//        }
//      }
      HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
      SearchConditionItem searchConditionItem = null;
      HrmFieldBean hrmFieldBean = null;

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("startdate");
      hrmFieldBean.setFieldlabel("740");
      hrmFieldBean.setFieldhtmltype("3");
      hrmFieldBean.setType("2");
      hrmFieldBean.setIsFormField(true);
      hrmFieldBean.setViewAttr(3);
      hrmFieldBean.setRules("required|string");
      searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
      itemList.add(searchConditionItem);

      hrmFieldBean = new HrmFieldBean();
      hrmFieldBean.setFieldname("enddate");
      hrmFieldBean.setFieldlabel("740");
      hrmFieldBean.setFieldhtmltype("3");
      hrmFieldBean.setType("2");
      hrmFieldBean.setIsFormField(true);
      hrmFieldBean.setViewAttr(3);
      hrmFieldBean.setRules("required|string");
      searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
      itemList.add(searchConditionItem);
    } catch (Exception e) {
      writeLog(e);
    }
//    retmap.put("count", UpgradeUtil.formatMsg(kqUpgradeTabEnum, user.getLanguage(), signSetCntInt+"") );
//    retmap.put("title", SystemEnv.getHtmlLabelName(kqUpgradeTabEnum.getTablabel(), user.getLanguage())+SystemEnv.getHtmlLabelName(17530, user.getLanguage()));
    retmap.put("itemList", itemList);
    retmap.put("status", "1");
    retmap.put("title", SystemEnv.getHtmlLabelName(17530,user.getLanguage()));

    return retmap;
	}

  private String getFlowSql(String flowtype) {
	  int type = Util.getIntValue(flowtype);
	  Map<String, String> paramMap = new HashMap<String, String>();
	  String fromDate = "";
	  String toDate = "";
    String _fromdate = "fromDate", _fromtime = "fromTime", _todate = "toDate", _totime = "toTime";
    switch (flowtype) {
      case "0"://请假
        break;
      case "3"://加班
        _fromdate = "fromdate";
        _fromtime = "fromtime";
        _todate = "tilldate";
        _totime = "tilltime";
        paramMap.put("oType", " and t.otype in ('0', '1')");
        break;
    }
//    String paramDate = " and (t." + _fromdate + " between '" + fromDate + "' and '" + toDate + "' or t." + _todate + " between '" + fromDate + "' and '" + toDate + "' or '" + fromDate + "' between t." + _fromdate + " and t." + _todate + " or '" + toDate + "' between t." + _fromdate + " and t." + _todate + ")";
//    if(StringUtil.isNotNull(fromDate) && StringUtil.isNotNull(toDate)) paramMap.put("fromDate", paramDate);
//    else if(StringUtil.isNotNull(fromDate)) paramMap.put("fromDate", " and (t." + _fromdate + " >= '" + fromDate + "' or '" + fromDate + "' between t." + _fromdate + " and t." + _todate + ")");
//    else if(StringUtil.isNotNull(toDate)) paramMap.put("toDate", " and (t." + _todate + " <= '" + toDate + "' or '" + toDate + "' between t." + _fromdate + " and t." + _todate + ")");
    paramMap.put("status", " and c.status in (0,1,2,3)");
    String sql = new HrmAttProcSetManager().getSQLByField006(type, paramMap, true, true);
    return sql;
  }

}
