package com.iamberry.wechat.tools;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ToolsUtils {
	
	public static boolean isNotEmpty(Object obj){
		if(null == obj){
			return false;
		} else if(obj instanceof String){
			if("".equals(((String)obj).trim()) || "null".equals(((String)obj).trim())){
				return false;
			}
		}
		return true;
	}
	
	public static String dateToString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	
	public static String dateToStringFull(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	
	public static Date stringToDateFull(String datestr) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(datestr);
	}
	
	public static Double Double2point(Double d){
		if(d != null){
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(d);
			d = Double.parseDouble(st);
		}else{
			d = 0.00;
		}
		return d;
	}
	//商品满多少减多少优惠
	public static Map<String, Object> getSumtotal100(Integer cardmember,Double tota,Double rate,Double pos,Double sumTotal,Double tota1,Double pos1,String loop){
		Map<String, Object> map = new HashMap<String, Object>(); 
		if("y".equals(loop.trim())){
			if(cardmember != null && cardmember == 101){
				if(sumTotal > 0 && sumTotal < tota){
					sumTotal = sumTotal * rate + pos;
					map.put("flag", "Y");
				}else if(sumTotal >= tota && sumTotal < tota1 ){
					sumTotal = sumTotal * rate;
				}else{
					int n = (int) (sumTotal/tota1);
					sumTotal = (sumTotal - n * pos1) * rate;
				}
			}else{
				if(sumTotal > 0 && sumTotal < tota){
					sumTotal = sumTotal + pos;
					map.put("flag", "Y");
				}else if(sumTotal >= tota && sumTotal < tota1 ){
				}else{
					int n = (int) (sumTotal/tota1);
					sumTotal = sumTotal - n * pos1;
				}
			}
		}else{
			if(cardmember != null && cardmember == 101){
				if(sumTotal > 0 && sumTotal < tota){
					sumTotal = sumTotal * rate + pos;
					map.put("flag", "Y");
				}else if(sumTotal >= tota && sumTotal < tota1 ){
					sumTotal = sumTotal * rate;
				}else{
					sumTotal = (sumTotal - pos1) * rate;
				}
			}else{
				if(sumTotal > 0 && sumTotal < tota){
					sumTotal = sumTotal + pos;
					map.put("flag", "Y");
				}else if(sumTotal >= tota && sumTotal < tota1 ){
				}else{
					sumTotal = sumTotal - pos1;
				}
			}
		}
		map.put("sumTotal", sumTotal);
		return map;
	}
	
	
	//商品总价打七折优惠   显示
	public static Map<String, Object> getSumtotal(Integer cardmember,Double tota,Double rate,Double pos,Double sumTotal){
		/*if(sumTotal > 0 && sumTotal < tota){
			sumTotal = sumTotal * rate + pos;
		}else{
			sumTotal = sumTotal * rate;
		}
		return sumTotal;*/
		Map<String, Object> map = new HashMap<String, Object>(); 
		if(sumTotal > 0 && sumTotal < tota){
			sumTotal = sumTotal * rate + pos;
			map.put("flag", "Y");
		}else{
			sumTotal = sumTotal * rate;
			if(sumTotal < tota){
				sumTotal = sumTotal + pos;
				map.put("flag", "Y");
			}
		}
		map.put("sumTotal", sumTotal);
		return map;
	}
	
	//商品总价打七折优惠   计算
	public static Double getSumtotal1(Integer cardmember,Double tota,Double rate,Double pos,Double sumTotal){
		sumTotal = sumTotal * rate;
		return sumTotal;
	}
	//内部8折优惠   显示页面
	public static Map<String, Object> getSumtotal2(Integer cardmember,Double tota,Double rate,Double pos,Double sumTotal){
		Map<String, Object> map = new HashMap<String, Object>(); 
		if(cardmember != null && cardmember == 0){
			if(sumTotal > 0 && sumTotal < tota){
				sumTotal = sumTotal * rate + pos;
				map.put("flag", "Y");
			}else{
				sumTotal = sumTotal * rate;
				if(sumTotal < tota){
					sumTotal = sumTotal + pos;
					map.put("flag", "Y");
				}
			}
		}else{
			if(sumTotal > 0 && sumTotal < tota){
				sumTotal = sumTotal + pos;
				map.put("flag", "Y");
			}
		}
		map.put("sumTotal", sumTotal);
		return map;
	}
	//内部8折优惠   计算
	public static Double getSumtotal3(Integer cardmember,Double tota,Double rate,Double pos,Double sumTotal){
		//计算会员等级为0的支付价格
		if(cardmember != null && cardmember == 0){
			sumTotal = sumTotal * rate;
		}
		return sumTotal;
	}
}
