package com.engine.kq.cmd.schedulesignimport;

import com.api.hrm.bean.HrmFieldBean;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogSmallType4Hrm;
import com.engine.common.constant.BizLogType;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.servicefiles.ScheduleXML;
import weaver.systeminfo.SystemEnv;

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

  private SimpleBizLogger logger;

	public SaveImportSetCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
    this.logger = new SimpleBizLogger();
    BizLogContext logContext = new BizLogContext();
    logContext.setDateObject(new Date());
    logContext.setLogType(BizLogType.HRM_ENGINE);
    logContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_HRM_SCHEDULESIGN_IMPORT);
    logContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_HRM_SCHEDULESIGN_IMPORT);
    logContext.setParams(params);
    logger.setUser(user);//当前操作人

		ScheduleXML ScheduleXML = new ScheduleXML();

    String mainSql = " select a.*,"+(ScheduleXML.getPointArrayList().indexOf("KQScheduleSignImport") > -1?"1":"0")+" as isVaild from HrmScheduleSignSet a ";
    logger.setMainSql(mainSql);//主表sql
    logger.setMainPrimarykey("datasourceid");//主日志表唯一key
    logger.setMainTargetNameColumn("tablename");
    logger.before(logContext);
  }

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

  @Override
  public List<BizLogContext> getLogContexts() {
    return logger.getBizLogContexts();
  }

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();
		String sql = "";
		try {
			if (!HrmUserVarify.checkUserRight("HrmResourceEdit:Edit", user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}
			

			ScheduleXML ScheduleXML = new ScheduleXML();
			//保存是否启用
			String isVaild = Util.null2String(params.get("isVaild"));
			ArrayList pointArrayList = ScheduleXML.getPointArrayList();
			ArrayList hstarr = new ArrayList();
			Hashtable dataHST = ScheduleXML.getDataHST();
			int idx = pointArrayList.indexOf("KQScheduleSignImport");
			for(Object pointid : pointArrayList){
				hstarr.add(dataHST.get(pointid));
			}

			if(isVaild.equals("1")){
				//启用
				if(idx==-1){
					Hashtable datadetailHST = new Hashtable();
					datadetailHST.put("construct","com.engine.kq.biz.KQScheduleSignImportJob");
					datadetailHST.put("cronExpr","0 0 1 * * ?");
					ScheduleXML.writeToScheduleXMLAdd("KQScheduleSignImport",datadetailHST);
					pointArrayList.add("KQScheduleSignImport");

					if(pointArrayList.indexOf("HrmScheduleSignImport")!=-1){
						/*ArrayList parapointids = new ArrayList();
						parapointids.add("HrmScheduleSignImport");*/
						ScheduleXML.deleteSchedule("HrmScheduleSignImport");
						pointArrayList.remove("HrmScheduleSignImport");
					}
				}
			}else{
				//停用
				if(idx!=-1){
					/*ArrayList parapointids = new ArrayList();
					parapointids.add("KQScheduleSignImport");*/
					ScheduleXML.deleteSchedule("KQScheduleSignImport");
					pointArrayList.remove("KQScheduleSignImport");
				}
			}
			
			String[] datasourceids = Util.splitString(Util.null2String(params.get("datasourceid")), ",") ; 
			String[] tablenames = Util.splitString(Util.null2String(params.get("tablename")), ",") ;
			String[] loginids = Util.splitString(Util.null2String(params.get("loginid")), ",") ;
			String[] workcodes = Util.splitString(Util.null2String(params.get("workcode")), ",") ;  
			String[] lastnames = Util.splitString(Util.null2String(params.get("lastname")), ",") ; 
			String[] signdates = Util.splitString(Util.null2String(params.get("signdate")), ",") ;  
			String[] signtimes = Util.splitString(Util.null2String(params.get("signtime")), ",") ;  
			String[] clientaddresss = Util.splitString(Util.null2String(params.get("clientaddress")), ",") ;
			String[] longitudes = Util.splitString(Util.null2String(params.get("longitude")), ",") ;
			String[] latitudes = Util.splitString(Util.null2String(params.get("latitude")), ",") ;
			String[] addrs = Util.splitString(Util.null2String(params.get("addr")), ",") ;
			String[] memos = Util.splitString(Util.null2String(params.get("memo")), ",") ;

			rs.executeUpdate("delete from HrmScheduleSignSet");
			for(int i=0;datasourceids!=null&&i<datasourceids.length;i++){
				String datasourceid = Util.null2String(datasourceids[i]); 
				if(datasourceid.length()==0)continue;
				String tablename = Util.null2String(tablenames[i]);
				String loginid = Util.null2String(loginids[i]);
				String workcode = Util.null2String(workcodes[i]);  
				String lastname = Util.null2String(lastnames[i]); 
				String usertype = "1";//Util.null2String(usertypes[i]);  
				String signtype = null;  
				String signdate = Util.null2String(signdates[i]);  
				String signtime = Util.null2String(signtimes[i]);  
				String clientaddress = Util.null2String(clientaddresss[i]);
				String longitude = Util.null2String(longitudes[i]) ;
				String latitude = Util.null2String(latitudes[i]) ;
				String addr = Util.null2String(addrs[i]) ;
				String memo = Util.null2String(memos[i]) ;

				sql = " insert into HrmScheduleSignSet " +
							" (datasourceid, tablename, loginid, workcode, lastname, usertype, signtype, signdate, signtime, clientaddress,longitude,latitude,addr,memo) " +
							" values('"+datasourceid+"','"+tablename+"','"+loginid+"','"+workcode+"','"+lastname+"','"+usertype+"','"+signtype+"','"+signdate+"','"+signtime+"','"+clientaddress+"'," +
							" '"+longitude+"','"+latitude+"','"+addr+"','"+memo+"')";
				rs.executeUpdate(sql);
			}

			retmap.put("status", "1");
			retmap.put("message", SystemEnv.getHtmlLabelName(18758, user.getLanguage()));
		} catch (Exception e) {
			writeLog("保存外部集成考勤失败：" + e);
			retmap.put("status", "-1");
		}
		return retmap;
	}


}
