package com.yifu.cloud.plus.v1.yifu.social.util;

import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.CheckedException;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 公用方法提取
 *
 * @author fxj
 * @date 2019-09-29
 **/
@Slf4j
public class ServiceUtil {
	private ServiceUtil() {
		throw new IllegalStateException("ServiceUtil class");
	}

	/**
	 * @Description: 返回字符串对应的int值
	 * @Date: 2022/7/18 17:29
	 **/
	public static int convertStringToInt(String num1) {
		if (Common.isEmpty(num1) || !Common.isNumber(num1)) {
			return -1;
		}
		return Integer.parseInt(num1);
	}

	/**
	 * 按派单日期生成生成月份
	 * @Author fxj
	 * @Date 2020-03-25
	 * @param createDate
	 * @return
	 **/
	public static String  initCreateMonth(LocalDateTime createDate, String payMonth){
		if (null == createDate || Common.isEmpty(payMonth) || payMonth.length() != 6){
			return DateUtil.getYearAndMonth(LocalDateTime.now(), 1);
		}
		if (createDate.getYear() < Integer.parseInt(payMonth.substring(0,4))){
			return  payMonth;
		}
		if (createDate.getYear() > Integer.parseInt(payMonth.substring(0,4))){
			return DateUtil.getYearAndMonth(createDate, 1);
		}
		if (createDate.getYear() == Integer.parseInt(payMonth.substring(0,4))){
			if (createDate.getMonthValue() < Integer.parseInt(payMonth.substring(4,6))){
				return  payMonth;
			}
			if (createDate.getMonthValue() >= Integer.parseInt(payMonth.substring(4,6))){
				return DateUtil.getYearAndMonth(createDate, 1);
			}
		}
		return  null;
	}

	/**
	 * 抛个异常先
	 * @Author fxj
	 * @Date 2020-09-09
	 * @param errorInfo
	 * @return
	 **/
	public static void runTimeExceptionDiy(String errorInfo) {
		throw new CheckedException(errorInfo);
	}

	/**
	 * 参数2的是否大于等于参数1的
	 * @Author fxj
	 * @Date 20200403
	 * @param socialStartDate
	 * @param socialPayMonth
	 * @param reduceDate
	 * @return
	 **/
	public static boolean checkMothForPaymentImport(Date socialStartDate, String socialPayMonth, Date reduceDate) {
		if(socialStartDate != null) {
			if (null == reduceDate){
				if(Integer.parseInt(socialPayMonth) >= Integer.parseInt(DateUtil.dateToString(socialStartDate))){
					return true;
				}
			}else {
				if(Integer.parseInt(socialPayMonth) >= Integer.parseInt(DateUtil.dateToString(socialStartDate))
						&& Integer.parseInt(socialPayMonth) < Integer.parseInt(DateUtil.dateToString(reduceDate))){
					return true;
				}
			}
		}
		return false;
	}

	public static BigDecimal ifNull(BigDecimal old, BigDecimal last){
		if (Common.isEmpty(old)){
			return last;
		}
		return old;
	}
	public static Date ifNull(Date old,Date last){
		if (Common.isEmpty(old)){
			return last;
		}
		return old;
	}
	public static String ifNullToEmpty(String str){
		if (Common.isEmpty(str)){
			return CommonConstants.EMPTY_STRING;
		}
		return str;
	}

	public static String replaceSeparator(String string,String str1, String... strn){
		if (null != str1) {
			string = string.replace(str1,CommonConstants.EMPTY_STRING);
		}
		if (null != strn) {
			for (String str : strn) {
				string = string.replace(str,CommonConstants.EMPTY_STRING);
			}
		}
		return string;
	}
}
