package com.engine.kq.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.kq.biz.*;
import com.engine.kq.cmd.attendanceButton.ButtonStatusEnum;
import weaver.common.DateUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class UtilKQ extends BaseBean {

	private static DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	public static String  getWeekDay(int day, int language){
		String weekDay="";
		if(day==0){
			weekDay= SystemEnv.getHtmlLabelName(16100, language);
		}else if(day==1){
			weekDay=SystemEnv.getHtmlLabelName(16101, language);
		}else if(day==2){
			weekDay=SystemEnv.getHtmlLabelName(16102, language);
		}else if(day==3){
			weekDay=SystemEnv.getHtmlLabelName(16103, language);
		}else if(day==4){
			weekDay=SystemEnv.getHtmlLabelName(16104, language);
		}else if(day==5){
			weekDay=SystemEnv.getHtmlLabelName(16105, language);
		}else if(day==6){
			weekDay=SystemEnv.getHtmlLabelName(16106, language);
		}
		return weekDay;
	}

	public static String  getWeekDayShort(int day, int language){
		String weekDay="";
		if(day==0){
			weekDay= SystemEnv.getHtmlLabelName(385821, language);
		}else if(day==1){
			weekDay=SystemEnv.getHtmlLabelName(385822, language);
		}else if(day==2){
			weekDay=SystemEnv.getHtmlLabelName(385823, language);
		}else if(day==3){
			weekDay=SystemEnv.getHtmlLabelName(385824, language);
		}else if(day==4){
			weekDay=SystemEnv.getHtmlLabelName(385825, language);
		}else if(day==5){
			weekDay=SystemEnv.getHtmlLabelName(385826, language);
		}else if(day==6){
			weekDay=SystemEnv.getHtmlLabelName(385820, language);
		}
		return weekDay;
	}

	public static String getPrimaryKeyGuid1SqlStr(String dBType){
		String guid1SQlStr = "replace(newid(),'-','')";
		if("oracle".equalsIgnoreCase(dBType)){
			guid1SQlStr = "SYS_GUID()";
		}else if("mysql".equalsIgnoreCase(dBType)){
			guid1SQlStr = "replace(uuid(),'-','')";
		}
		else if("postgresql".equalsIgnoreCase(dBType)){
			guid1SQlStr = "replace(uuid(),'-','')";
		}
		else if("dm".equalsIgnoreCase(dBType)){
			guid1SQlStr = "replace(SYS_GUID(),'0x','')";
		}
		return guid1SQlStr;
	}

	public static String getTimeStampSqlStr(String dBType){
		String timeStampSqlStr = "convert(char(10),getdate(),20)+' '+convert(char(8),getdate(),108)";
		if("oracle".equalsIgnoreCase(dBType)){
			timeStampSqlStr = "to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')";
		}
		else if("postgresql".equalsIgnoreCase(dBType)){
			timeStampSqlStr = "to_char(now(),'yyyy-mm-dd hh24:mi:ss')";
		}
		else if("mysql".equalsIgnoreCase(dBType)){
			timeStampSqlStr = "date_format(NOW(),'%Y-%m-%d %H:%i:%s')";
		}
		return timeStampSqlStr;
	}

	public static String getTimesAcross(String times){
		String result = times;
		String[] hourMins = Util.splitString(times,":");
		if(hourMins.length==2){
			result = 24 + Util.getIntValue(hourMins[0])+":"+hourMins[1];
		}
		return result;
	}

	/**
	 * 计算签到签退状态
	 * @param signType
	 * @param workdatesection
	 * @return
	 */
	public static String getSignStatus(String signType, String strSignDateTime ,String strWorkDateTime) {
		String signStatus = "";
		try{
			strSignDateTime = DateUtil.formatDateTime(strSignDateTime);
			strWorkDateTime = DateUtil.formatDateTime(strWorkDateTime);
			LocalDateTime signDateTime = LocalDateTime.parse(strSignDateTime,fullFormatter);
			LocalDateTime workDateTime = LocalDateTime.parse(strWorkDateTime,fullFormatter);
			//签到的话
			if("1".equalsIgnoreCase(signType)){
				//打卡时间比上班时间晚，迟到了
				if(signDateTime.isAfter(workDateTime)){
					signStatus = ButtonStatusEnum.BELATE.getStatusCode();
				}else{
					signStatus = ButtonStatusEnum.NORMAL.getStatusCode();
				}
			}else if("2".equalsIgnoreCase(signType)){
				//签退的话
				//打卡时间比下班班时间晚，早退了
				if(signDateTime.isBefore(workDateTime)){
					signStatus = ButtonStatusEnum.LEAVEERALY.getStatusCode();
				}else{
					signStatus = ButtonStatusEnum.NORMAL.getStatusCode();
				}
			}
		}catch (Exception e){
			new BaseBean().writeLog("com.engine.kq.util.UtilKQ.getSignStatus"+e);
		}
		return signStatus;
	}


	public static void main(String[] args){
		for(int i=0; i<10; i++){
			UUID uuid = UUID.randomUUID();
			System.err.println(uuid);
		}
	}
}
