package com.ways.app.sales.utils;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ways.auth.model.AuthConstants;
import com.ways.auth.model.MyUserDetails;
import com.ways.framework.utils.LocaleMessageUtil;
import com.ways.waysdata.utils.Convert;
import flexjson.JSONSerializer;
/**
* 公共工具类
* @author yinlue
*
*/
public class AppFrameworkUtil {
/**
* 四舍五入。保留num个小数点后的数字
* @param bh
* @param num
* @return
*/
public static String getNum(double bh, int num) {
String numstr = "";
numstr = new BigDecimal(bh).setScale(num, BigDecimal.ROUND_HALF_UP)+"";
return numstr;
}
/**
* 四舍五入。保留num个小数点后的数字
* @param bh
* @param num
* @return
*/
public static String getNum(String bh, int num) {
if(AppFrameworkUtil.isEmpty(bh) || "-".equals(bh)) return "-";
if("0.0".equals(bh) || "0".equals(bh)) return bh;
String numstr = "";
numstr = new BigDecimal(Double.parseDouble(bh)).setScale(num, BigDecimal.ROUND_HALF_UP)+"";
return numstr;
}
/**
* 字符串空判断
* @param str
* @return
*/
public static boolean isEmpty(String str)
{
boolean flag = true;
if(null != str && 0 != str.trim().length()) flag = false;
return flag;
}
/**
* 添加符号
* @param str
*/
public static String addSign(String str)
{
if(!AppFrameworkUtil.isEmpty(str) && !str.contains("-"))
{
str = "+" + str;
}
return str;
}
/**
* 添加千分位
* @param str
* @return
*/
public static String format(String str)
{
if(!isEmpty(str) && !"-".equals(str))
{
DecimalFormat df = new DecimalFormat("###,###");
try {
str = df.format(Long.parseLong(str));
} catch (Exception e) {
return str;
}
}
return str;
}
/**
* 序列化集合成JSON字符
* @param list
* @return
*/
public static String serializableJSONData(List<?> list)
{
JSONSerializer serializer = new JSONSerializer();
String json="";
json = serializer.exclude("*.class").deepSerialize(list).replaceAll(":\\s*null\\s*", ":\"\"");
return json;
}
public static String serializableJSONData(Object obj)
{
JSONSerializer serializer = new JSONSerializer();
String json="";
json = serializer.exclude("*.class").deepSerialize(obj).replaceAll(":\\s*null\\s*", ":\"\"");
return json;
}
public static String serializableJSONData(Map<String, ?> map)
{
JSONSerializer serializer = new JSONSerializer();
String json="";
json = serializer.exclude("*.class").deepSerialize(map).replaceAll(":\\s*null\\s*", ":\"\"");
return json;
}
/**
* response JSON数据
* @param response
* @param context
*/
public static void renderJSON(HttpServletResponse response,String context){
PrintWriter out = null;
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
out =response.getWriter();
out.write(context);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
out.close();
}
}
/**
* 获取不足一季的补充内容
* @param year
* @param month
* @return String
*/
public static String getQuartersLable(String year,int month){
String monthI18n = "月";
if("en".equals(LocaleMessageUtil.getCurrentLanguage())) monthI18n = "";
int quarter = Convert.getQuarterNumbers(month);
for (int i = 0; i < 4; i++) {
// String label = LocaleMessageUtil.getLocaleMessage(getQuarterKeysMap().get((i+1)+""));
String label = "";
if((i+1) == quarter){
if(i==3 && month % 3 != 0){
//label += "{br}("+year+"’{br}"+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
label += "("+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
}else if(month % 3 != 0){
//label += "("+year+"’"+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
label += "("+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
}
return label;
}
}
return "";
}
/**
* 判断 * @param month
* @return int
*/
public static int getQuarterNumbers(int month){
int nums = 0;
if(month <= 3){
nums = 1;
}else if(month > 3 && month <= 6){
nums = 2;
}else if(month > 6 && month <= 9){
nums = 3;
}else if(month > 9 && month <= 12){
nums = 4;
}
return nums;
}
}