package com.yem.ensign.weboffice;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yem.ensign.common.BigDecimalUtils;
import com.yem.ensign.common.NumberParserUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.AppInfo;
import kd.bos.entity.AppMetadataCache;
import kd.bos.entity.param.AppParam;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.parameter.SystemParamServiceHelper;

/**
 * webOffice工具类
 * 
 * @author hdd
 * @date 2020-10-26 下午2:17:03
 */
public class WebOfficeComUtils {

	private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 日期格式
	private static DateFormat formatHms = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 日期格式
	private static List<String> endate = new ArrayList<>();

	/*
	 * 数量A 单价B 金额C 溢短装D 日期中文E 日期英文F
	 */

	public static String objecttransformation(Object obj, String type, int accuracy) {
		String currencystring = "";
		// String type = "";//调用此方法需要改为参数，目前设为变量是为了调用方法不报错
		boolean isinstanceof = false;
		if (obj != null) {
			if (obj instanceof Integer) {
				int value = ((Integer) obj).intValue();
				currencystring = String.valueOf(value);
				if ("H".equals(type)) {
					currencystring = SetAmountInWords(BigDecimalUtils.toBigDecimal(value));
				}
				isinstanceof = true;
			} else if (obj instanceof String) {
				String s = (String) obj;
				currencystring = s;
				isinstanceof = true;
			} else if (obj instanceof Double) {
				double d = ((Double) obj).doubleValue();
				BigDecimal f = new BigDecimal(d);
				if (accuracy > 0) {
					currencystring = (String) toBigDecimal1(f, accuracy).toString();
					isinstanceof = true;
				} else {
					currencystring = (String) toBigDecimal1(f, getsyspr(type)).toString();
					isinstanceof = true;
				}
				if ("H".equals(type)) {
					currencystring = SetAmountInWords(BigDecimalUtils.toBigDecimal(f));
					isinstanceof = true;
				}
			} else if (obj instanceof Float) {
				float f1 = ((Float) obj).floatValue();
				// currencystring = String.valueOf(f);
				// isinstanceof = true;
				BigDecimal f = new BigDecimal(f1);
				if (accuracy > 0) {
					currencystring = (String) toBigDecimal1(f, accuracy).toString();
					isinstanceof = true;
				} else {
					currencystring = (String) toBigDecimal1(f, getsyspr(type)).toString();
					isinstanceof = true;
				}
				if ("H".equals(type)) {
					currencystring = SetAmountInWords(BigDecimalUtils.toBigDecimal(f));
					isinstanceof = true;
				}
			} else if (obj instanceof Long) {
				long l = ((Long) obj).longValue();
				currencystring = String.valueOf(l);
				if ("H".equals(type)) {
					currencystring = SetAmountInWords(BigDecimalUtils.toBigDecimal(l));
				}
				isinstanceof = true;
			} else if (obj instanceof Boolean) {
				boolean b = ((Boolean) obj).booleanValue();
				currencystring = String.valueOf(b);
				isinstanceof = true;
			} else if (obj instanceof Date) {
				Date d = (Date) obj;
				if (null != type && type.equals("E")) {
					currencystring = datetochinadate(d);
				} else if (null != type && type.equals("F")) {
					currencystring = datetochinadatenoday(d);
				} else if (null != type && type.equals("I")) {
					currencystring = datetoEnglishdate(d);
				} else if (null != type && type.equals("J")) {
					currencystring = datetoEnglishdatenoday(d);
				} else {
					currencystring = (String) formatDate(d).toString();
				}
				isinstanceof = true;
			} else if (obj instanceof BigDecimal) {
				BigDecimal f = (BigDecimal) obj;
				if (accuracy > 0) {
					currencystring = (String) toBigDecimal1(f, accuracy).toString();
					if (type.equals("C")) {
						switch (accuracy) {
						case 1:
							DecimalFormat df1 = new DecimalFormat("###,##0.0");
							currencystring = df1.format(toBigDecimal1(f, accuracy));
							break;
						case 2:
							DecimalFormat df2 = new DecimalFormat("###,##0.00");
							currencystring = df2.format(toBigDecimal1(f, accuracy));
							break;
						case 3:
							DecimalFormat df3 = new DecimalFormat("###,##0.000");
							currencystring = df3.format(toBigDecimal1(f, accuracy));
							break;
						case 4:
							DecimalFormat df4 = new DecimalFormat("###,##0.0000");
							currencystring = df4.format(toBigDecimal1(f, accuracy));
							break;
						}
					} else if ("H".equals(type)) {
						currencystring = SetAmountInWords(BigDecimalUtils.toBigDecimal(f));
					}
					isinstanceof = true;
				} else {
					currencystring = (String) toBigDecimal1(f, accuracy).toString();
					if (type.equals("C")) {
						switch (accuracy) {
						case 1:
							DecimalFormat df1 = new DecimalFormat("###,##0.0");
							currencystring = df1.format(toBigDecimal1(f, getsyspr(type)));
							break;
						case 2:
							DecimalFormat df2 = new DecimalFormat("###,##0.00");
							currencystring = df2.format(toBigDecimal1(f, getsyspr(type)));
							break;
						case 3:
							DecimalFormat df3 = new DecimalFormat("###,##0.000");
							currencystring = df3.format(toBigDecimal1(f, getsyspr(type)));
							break;
						case 4:
							DecimalFormat df4 = new DecimalFormat("###,##0.0000");
							currencystring = df4.format(toBigDecimal1(f, getsyspr(type)));
							break;
						}
					} else if ("H".equals(type)) {
						currencystring = SetAmountInWords(BigDecimalUtils.toBigDecimal(f));
					}
					isinstanceof = true;
				}
			}
			if (!isinstanceof) {
				currencystring = String.valueOf(obj);
			}
		}
		return currencystring;
	}

	/**
	 * 设置英文大写金额
	 *
	 * @param amount 金额
	 */
	public static String SetAmountInWords(BigDecimal amount) {
		String val = NumberParserUtils.parse2(amount.toString());
		return val.toUpperCase();
	}

	public static BigDecimal toBigDecimal(BigDecimal obj) {
		if (obj == null) {
			return BigDecimalUtils.ZERO;
		} else {
			String bigSub = obj.toString();
			if (bigSub.contains(".")) {
				String bigEnd = bigSub.substring(bigSub.lastIndexOf(".") + 1);
				int bigZero = Integer.valueOf(bigEnd).intValue();
				if (bigZero == 0) {
					return obj.setScale(0, BigDecimal.ROUND_HALF_UP);
				} else {
					return obj.setScale(4, BigDecimal.ROUND_HALF_UP);
				}
			} else {
				return obj.setScale(0, BigDecimal.ROUND_HALF_UP);
			}
		}
	}

	/*
	 * 处理小数精度问题
	 */
	public static BigDecimal toBigDecimal1(BigDecimal obj, int accuracy) {
		if (obj == null) {
			return BigDecimalUtils.ZERO;
		} else {
			String bigSub = obj.toString();
			if (bigSub.contains(".")) {
				String bigEnd = bigSub.substring(bigSub.lastIndexOf(".") + 1);
				char[] array = bigEnd.toCharArray();
				boolean isint = true;
				for (char a : array) {
					Integer intbigEnd = Integer.valueOf(a);
					int bigZero = intbigEnd.intValue();
					if (bigZero > 0) {
						isint = false;
						break;
					}
				}
				if (isint) {
					return obj.setScale(accuracy, BigDecimal.ROUND_HALF_UP);
				} else {
					return obj.setScale(accuracy, BigDecimal.ROUND_HALF_UP);
				}
			} else {
				return obj.setScale(accuracy, BigDecimal.ROUND_HALF_UP);
			}
		}
	}

	/*
	 * 获取系统参数
	 */
	public static int getsyspr(String type) {
		int yem_accuracy = 0;
		// 获取应用参数
		// 获取当前登录业务单元id
		long orgId = RequestContext.get().getOrgId();
		// 从缓存中获取应用信息
		AppInfo ptstDemo = AppMetadataCache.getAppInfo("yem_is");
		// 获取应用的主键
		String appId = ptstDemo.getId();
		// 获取应用参数
		Map<String, Object> map = SystemParamServiceHelper.loadAppParameterFromCache(new AppParam(appId, orgId));
//		switch (type) {
//		case "A":// 数量精度
//			if (map != null) {
//				yem_accuracy = (int) map.get("yem_qtyaccuracy");
//			}
//			break;
//		case "B":// 单价精度
//			if (map != null) {
//				yem_accuracy = (int) map.get("yem_priceaccuracy");
//			}
//			break;
//		case "C":// 金额精度
//			if (map != null) {
//				yem_accuracy = (int) map.get("yem_amtaccuracy");
//			}
//			break;
//		case "D":// 溢短装精度
//			if (map != null) {
//				yem_accuracy = (int) map.get("yem_moreorless");
//			}
//			break;
//		default:
//			break;
//		}
//		return yem_accuracy;
		return 4;
	}

	/*
	 * 将日期转换为中文格式(年，月，日)
	 */
	public static String datetochinadate(Date date) {
		String datechinses = "";
		String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(date).toString().split("-");
		Integer year = Integer.parseInt(strNow[0]);
		Integer month = Integer.parseInt(strNow[1]);
		Integer day = Integer.parseInt(strNow[2]);
		return datechinses = year + "年" + month + "月" + day + "日";
	}

	/*
	 * 将日期转换为中文格式(年，月)
	 */
	public static String datetochinadatenoday(Date date) {
		String datechinses = "";
		String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(date).toString().split("-");
		Integer year = Integer.parseInt(strNow[0]);
		Integer month = Integer.parseInt(strNow[1]);
		Integer day = Integer.parseInt(strNow[2]);
		return datechinses = year + "年" + month + "月";
	}

	/*
	 * 将日期转换为英文格式（年，月，日）
	 */
	public static String datetoEnglishdate(Date date) {
		String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(date).toString().split("-");
		String dateen = "";
		endate.add("January");
		endate.add("February");
		endate.add("March");
		endate.add("April");
		endate.add("May");
		endate.add("June");
		endate.add("July");
		endate.add("August");
		endate.add("September");
		endate.add("October");
		endate.add("November");
		endate.add("December");
		Integer year = Integer.parseInt(strNow[0]);
		Integer month = Integer.parseInt(strNow[1]);
		Integer day = Integer.parseInt(strNow[2]);

		switch (month) {
		case 1:
			String January = endate.get(0);
			dateen = day + " " + January + " " + "," + year;
			break;
		case 2:
			String February = endate.get(1);
			dateen = day + " " + February + " " + "," + year;
			break;
		case 3:
			String March = endate.get(2);
			dateen = day + " " + March + " " + "," + year;
			break;
		case 4:
			String April = endate.get(3);
			dateen = day + " " + April + " " + "," + year;
			break;
		case 5:
			String May = endate.get(4);
			dateen = day + " " + May + " " + "," + year;
			break;
		case 6:
			String June = endate.get(5);
			dateen = day + " " + June + " " + "," + year;
			break;
		case 7:
			String July = endate.get(6);
			dateen = day + " " + July + " " + "," + year;
			break;
		case 8:
			String August = endate.get(7);
			dateen = day + " " + August + " " + "," + year;
			break;
		case 9:
			String September = endate.get(8);
			dateen = day + " " + September + " " + "," + year;
			break;
		case 10:
			String October = endate.get(9);
			dateen = day + " " + October + " " + "," + year;
			break;
		case 11:
			String November = endate.get(10);
			dateen = day + " " + November + " " + "," + year;
			break;
		case 12:
			String December = endate.get(11);
			dateen = day + " " + December + " " + "," + year;
			break;
		default:
			break;
		}
		return dateen;
	}

	/*
	 * 将日期转换为英文格式（年，月）
	 */
	public static String datetoEnglishdatenoday(Date date) {
		String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(date).toString().split("-");
		String dateen = "";
		endate.add("January");
		endate.add("February");
		endate.add("March");
		endate.add("April");
		endate.add("May");
		endate.add("June");
		endate.add("July");
		endate.add("August");
		endate.add("September");
		endate.add("October");
		endate.add("November");
		endate.add("December");
		Integer year = Integer.parseInt(strNow[0]);
		Integer month = Integer.parseInt(strNow[1]);
		Integer day = Integer.parseInt(strNow[2]);
		switch (month) {
		case 1:
			String January = endate.get(0);
			dateen = January + "," + year;
			break;
		case 2:
			String February = endate.get(1);
			dateen = February + "," + year;
			break;
		case 3:
			String March = endate.get(2);
			dateen = March + "," + year;
			break;
		case 4:
			String April = endate.get(3);
			dateen = April + "," + year;
			break;
		case 5:
			String May = endate.get(4);
			dateen = May + "," + year;
			break;
		case 6:
			String June = endate.get(5);
			dateen = June + "," + year;
			break;
		case 7:
			String July = endate.get(6);
			dateen = July + "," + year;
			break;
		case 8:
			String August = endate.get(7);
			dateen = August + "," + year;
			break;
		case 9:
			String September = endate.get(8);
			dateen = September + "," + year;
			break;
		case 10:
			String October = endate.get(9);
			dateen = October + "," + year;
			break;
		case 11:
			String November = endate.get(10);
			dateen = November + "," + year;
			break;
		case 12:
			String December = endate.get(11);
			dateen = December + "," + year;
			break;
		default:
			break;
		}
		return dateen;
	}

	/**
	 * 时间转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		String datestr = "";
		if (date != null) {
			datestr = format.format(date);
		}
		return datestr;
	}

	/**
	 * 时间转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateHms(Date date) {
		String datestr = "";
		if (date != null) {
			datestr = formatHms.format(date);
		}
		return datestr;
	}

	/**
	 * 字符串转时间
	 * 
	 * @param datestr
	 * @return
	 */
	public static Date parseDate(String datestr) {
		Date date = null;
		if (StringUtils.isNotBlank(datestr) && !"null".equals(datestr)) {
			try {
				date = format.parse(datestr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

	/**
	 * 根据物料ID获取物料分类
	 * 
	 * @param materialId
	 * @return
	 */
	public static long getMaterialGroupId(long materialId) {
		long yem_materialgroupid = 0L;
		QFilter qFilter1 = new QFilter("material", "=", materialId);
		DynamicObject[] groups = BusinessDataServiceHelper.load("bd_materialgroupdetail", "standard,group,material",
				new QFilter[] { qFilter1 });
		for (int i = 0; i < groups.length; i++) {
			DynamicObject standard = (DynamicObject) groups[i].get("standard");
			DynamicObject group = (DynamicObject) groups[i].get("group");
			if (standard.getString("number").equals("JBFLBZ")) {
				yem_materialgroupid = group.getLong("id");
			}
		}
		return yem_materialgroupid;
	}
}
