package org.smartframework.common;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Administrator
 *
 */
public class ComTool {
	/**
	 * 设置精确度
	 * 
	 * @param object
	 * @param scale
	 * @return
	 */
	@SuppressWarnings("unused")
	public static String accuracy(Object object, int scale) {
		DecimalFormat df = new DecimalFormat("0.00");
		if (object instanceof String) {
			if (object == null) {
				return "0.00";
			}else {
				return df.format(Double.valueOf((String)object));
			}
		}else if(object instanceof Integer){
			return object+".00";
		}
		return df.format(object);
	}

	/**
	 * 除法的四舍五入
	 * 
	 * @param num
	 * @param total
	 * @param scale
	 * @return
	 */
	public static String accuracy(double num, double total, int scale) {
		DecimalFormat df = new DecimalFormat("0.00");
		if(total!=0){
			double accuracy_num = num / total * 100;
			return df.format(accuracy_num) + "%";
		}else{
			return "0.00"+"%";
		}
	}
	
	/**
	 * 分页的公共方法
	 */
	public static Map<String, Object> commPage(List<Map<String, Object>> data, Integer page, Integer pageSize) {
		BigDecimal pages=new BigDecimal(0);
		if(pageSize!=0){
			pages = new BigDecimal(Long.toString(data.size())).divide(new BigDecimal(Long.toString(pageSize)), 0,
				RoundingMode.UP);
		}
		
		BigDecimal currentPage = new BigDecimal(Long.toString(page));
		boolean endOfPage = false;
		if (pages.compareTo(currentPage) <= 0) {
			currentPage=pages;
			endOfPage = true;
		} else {
			endOfPage = false;
		}
		if(currentPage.compareTo(new BigDecimal(0))==0){
			currentPage=new BigDecimal(1);
		}
			
		List<Map<String, Object>> data1 = new ArrayList<>();
		if (data.size() > 0 && data.size() <= pageSize) {
			data1 = data;
		} else if (data.size() > pageSize && data.size() <= currentPage.intValue() * pageSize) {
			for (int i = (currentPage.intValue() - 1) * pageSize; i < data.size(); i++) {
				data1.add(data.get(i));
			}
		} else if (data.size() > currentPage.intValue() * pageSize) {
			for (int i = (currentPage.intValue() - 1) * pageSize; i < currentPage.intValue() * pageSize; i++) {
				data1.add(data.get(i));
			}
		}
		Map<String, Object> map1 = new HashMap<>();
		map1.put("pages", pages);
		map1.put("currentPage", currentPage);
		map1.put("pageSize", pageSize);
		map1.put("endOfPage", endOfPage);
		map1.put("data", data1);
		return map1;
	}
	
	public int formatString(String a){
		if(a.length()==0){
			return 0;
		}
		for (int i=1;i<a.length();i++) {
			String b=a.substring(i-1,i);
			for(int j=0;i<=10;i++){
				if(!b.equals(j+"")){
					return 0;
				}
			}
		}
		return Integer.valueOf(a);
	}
	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		 ComTool c=new ComTool();
		List<Map<String,Object>> list=new ArrayList<>();
		 for (int i=1;i<=21;i++) {
			 Map<String,Object> data=new HashMap<>();
			data.put(i+"", i);
			list.add(data);
		}
		 c.commPage(list, 3, 10);
	}
}
