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 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;
import weaver.workflow.workflow.WorkflowRequestComInfo;

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

	public DoUpgrade10Cmd(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 {
      String startdate = Util.null2String(params.get("startdate"));
      String enddate = Util.null2String(params.get("enddate"));
      if(startdate.length() == 0 || enddate.length() ==0 ){
        retmap.put("status", "1");
        retmap.put("items", items);
        return retmap;
      }
		  KQOneStopUp kqOneStopUp = new KQOneStopUp();
      kqOneStopUp.clearFlowData(startdate, enddate);
		  kqOneStopUp.upFlowData(startdate, enddate);

      WorkflowRequestComInfo workflowRequestComInfo = new WorkflowRequestComInfo();

      Map<String,Object> splitmap = new HashMap<String,Object>();
      List<Object> splitList = new ArrayList<>();
      List<List> params = new ArrayList<>();
      String batchSql = " insert into hrm_to_flow_split(splitid,flowtype) values(?,?) ";

      String getsplit_leaveSql = "select distinct requestid,fromdatedb,fromtimedb,todatedb,totimedb,durationdb from kq_flow_split_leave where fromdate >='"+startdate+"' and todate <='"+enddate+"' ";
      rs.executeQuery(getsplit_leaveSql);
      while (rs.next()){
        String typename = SystemEnv.getHtmlLabelName(83393, user.getLanguage());
        String requestid = rs.getString("requestid");
        String fromdatedb = rs.getString("fromdatedb");
        String fromtimedb = rs.getString("fromtimedb");
        String todatedb = rs.getString("todatedb");
        String totimedb = rs.getString("totimedb");
        String durationdb = rs.getString("durationdb");
        String desc = typename+" "+workflowRequestComInfo.getRequestName(requestid)+" "+fromdatedb+" "+fromtimedb+" "+todatedb+" "+totimedb+" "+durationdb;
        splitList.add(desc);
        List<Object> beanParams =  new ArrayList<Object>();
        beanParams.add(requestid);
        beanParams.add("leave");
        params.add(beanParams);
      }

      String getsplit_evectionSql = "select distinct requestid,fromdatedb,fromtimedb,todatedb,totimedb,durationdb from kq_flow_split_evection where fromdate >='"+startdate+"' and todate <='"+enddate+"' ";
      rs.executeQuery(getsplit_evectionSql);
      while (rs.next()){
        String typename = SystemEnv.getHtmlLabelName(83394, user.getLanguage());
        String requestid = rs.getString("requestid");
        String fromdatedb = rs.getString("fromdatedb");
        String fromtimedb = rs.getString("fromtimedb");
        String todatedb = rs.getString("todatedb");
        String totimedb = rs.getString("totimedb");
        String durationdb = rs.getString("durationdb");
        String desc = typename+" "+workflowRequestComInfo.getRequestName(requestid)+" "+fromdatedb+" "+fromtimedb+" "+todatedb+" "+totimedb+" "+durationdb;
        splitList.add(desc);
        List<Object> beanParams =  new ArrayList<Object>();
        beanParams.add(requestid);
        beanParams.add("evection");
        params.add(beanParams);
      }

      String getsplit_outSql = "select distinct requestid,fromdatedb,fromtimedb,todatedb,totimedb,durationdb from kq_flow_split_out where fromdate >='"+startdate+"' and todate <='"+enddate+"' ";
      rs.executeQuery(getsplit_outSql);
      while (rs.next()){
        String typename = SystemEnv.getHtmlLabelName(83395, user.getLanguage());
        String requestid = rs.getString("requestid");
        String fromdatedb = rs.getString("fromdatedb");
        String fromtimedb = rs.getString("fromtimedb");
        String todatedb = rs.getString("todatedb");
        String totimedb = rs.getString("totimedb");
        String durationdb = rs.getString("durationdb");
        String desc = typename+" "+workflowRequestComInfo.getRequestName(requestid)+" "+fromdatedb+" "+fromtimedb+" "+todatedb+" "+totimedb+" "+durationdb;
        splitList.add(desc);
        List<Object> beanParams =  new ArrayList<Object>();
        beanParams.add(requestid);
        beanParams.add("out");
        params.add(beanParams);
      }

      String getsplit_overtimeSql = "select distinct requestid,fromdatedb,fromtimedb,todatedb,totimedb,durationdb from kq_flow_split_overtime where fromdate >='"+startdate+"' and todate <='"+enddate+"' ";
      rs.executeQuery(getsplit_overtimeSql);
      while (rs.next()){
        String typename = SystemEnv.getHtmlLabelName(83396, user.getLanguage());
        String requestid = rs.getString("requestid");
        String fromdatedb = rs.getString("fromdatedb");
        String fromtimedb = rs.getString("fromtimedb");
        String todatedb = rs.getString("todatedb");
        String totimedb = rs.getString("totimedb");
        String durationdb = rs.getString("durationdb");
        String desc = typename+" "+workflowRequestComInfo.getRequestName(requestid)+" "+fromdatedb+" "+fromtimedb+" "+todatedb+" "+totimedb+" "+durationdb;
        splitList.add(desc);
        List<Object> beanParams =  new ArrayList<Object>();
        beanParams.add(requestid);
        beanParams.add("overtime");
        params.add(beanParams);
      }

      String getsplit_otherSql = "select distinct requestid,fromdatedb,fromtimedb,todatedb,totimedb,durationdb from kq_flow_split_other where fromdate >='"+startdate+"' and todate <='"+enddate+"' ";
      rs.executeQuery(getsplit_otherSql);
      while (rs.next()){
        String typename = SystemEnv.getHtmlLabelName(127655, user.getLanguage());
        String requestid = rs.getString("requestid");
        String fromdatedb = rs.getString("fromdatedb");
        String fromtimedb = rs.getString("fromtimedb");
        String todatedb = rs.getString("todatedb");
        String totimedb = rs.getString("totimedb");
        String durationdb = rs.getString("durationdb");
        String desc = typename+" "+workflowRequestComInfo.getRequestName(requestid)+" "+fromdatedb+" "+fromtimedb+" "+todatedb+" "+totimedb+" "+durationdb;
        splitList.add(desc);
        List<Object> beanParams =  new ArrayList<Object>();
        beanParams.add(requestid);
        beanParams.add("other");
        params.add(beanParams);
      }
      splitmap.put("label", SystemEnv.getHtmlLabelName(505747, user.getLanguage()));
      splitmap.put("status", "1");
      splitmap.put("results", splitList);
      items.add(splitmap);

      boolean isOk = rs1.executeBatchSql(batchSql, params);


      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;
	}

}
