commit
560c2b2bae
7 changed files with 708 additions and 0 deletions
@ -0,0 +1,6 @@
|
||||
# open-JSD-10347 |
||||
|
||||
JSD-10347 round5函数模式上实现“4舍6入5凑偶”规则\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系【pioneer】处理。 |
Binary file not shown.
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||
<id>com.fr.plugin.third.party.jsdbadeh</id> |
||||
<name><![CDATA[函数round5_EK]]></name> |
||||
<active>yes</active> |
||||
<version>1.0.2</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2019-01-01</jartime> |
||||
<vendor>fr.open</vendor> |
||||
<description><![CDATA[]]></description> |
||||
<change-notes><![CDATA[ |
||||
[2022-07-18]JSD-10347插件初始化<br/> |
||||
]]></change-notes> |
||||
<lifecycle-monitor class="com.fr.plugin.third.party.jsdbadeh.PluginLifecycleMonitor"/> |
||||
<extra-core> |
||||
<FunctionDefineProvider class="com.fr.plugin.third.party.jsdbadeh.function.Round6Function" name="ROUND6" description="四舍六入五成双"/> |
||||
</extra-core> |
||||
<function-recorder class="com.fr.plugin.third.party.jsdbadeh.PluginLifecycleMonitor"/> |
||||
</plugin> |
@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<parent> |
||||
<groupId>com.fr.plugin</groupId> |
||||
<artifactId>starter</artifactId> |
||||
<version>10.0</version> |
||||
</parent> |
||||
<packaging>jar</packaging> |
||||
<artifactId>plugin-jsd-10347</artifactId> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>com.fanruan.api</groupId> |
||||
<artifactId>finekit</artifactId> |
||||
<version>10.0</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/finekit-10.0.jar</systemPath> |
||||
</dependency> |
||||
</dependencies> |
||||
<properties> |
||||
<!---如果要更改调试插件,改这里的配置就可以了--> |
||||
<plugin-path> |
||||
${project.basedir}/../webroot/WEB-INF/plugins/plugin-com.fr.plugin.third.party.jsd10347-1.0 |
||||
</plugin-path> |
||||
</properties> |
||||
<build> |
||||
<outputDirectory>${plugin-path}/classes</outputDirectory> |
||||
<resources> |
||||
<resource> |
||||
<targetPath>${plugin-path}</targetPath> |
||||
<directory>${project.basedir}</directory> |
||||
<includes> |
||||
<include>plugin.xml</include> |
||||
</includes> |
||||
</resource> |
||||
<resource> |
||||
<targetPath>${plugin-path}</targetPath> |
||||
<directory>${project.basedir}/lib</directory> |
||||
<includes> |
||||
<include>*.jar</include> |
||||
<include>*.dll</include> |
||||
</includes> |
||||
</resource> |
||||
</resources> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.apache.maven.plugins</groupId> |
||||
<artifactId>maven-compiler-plugin</artifactId> |
||||
<configuration> |
||||
<source>8</source> |
||||
<target>8</target> |
||||
</configuration> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
</project> |
@ -0,0 +1,25 @@
|
||||
package com.fr.plugin.third.party.jsdbadeh; |
||||
|
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.plugin.context.PluginContext; |
||||
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.stable.fun.Authorize; |
||||
|
||||
|
||||
/** |
||||
* 配置信息初始化 |
||||
*/ |
||||
@EnableMetrics |
||||
@Authorize(callSignKey = "com.fr.plugin.third.party.jsdbadeh") |
||||
public class PluginLifecycleMonitor extends AbstractPluginLifecycleMonitor { |
||||
@Override |
||||
@Focus(id = "com.fr.plugin.third.party.jsdbadeh", text = "plugin-jsdbadeh", source = Original.PLUGIN) |
||||
public void afterRun(PluginContext pluginContext) { |
||||
} |
||||
|
||||
@Override |
||||
public void beforeStop(PluginContext pluginContext) { |
||||
} |
||||
} |
@ -0,0 +1,411 @@
|
||||
package com.fr.plugin.third.party.jsdbadeh; |
||||
|
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.decision.authority.AuthorityContext; |
||||
import com.fr.decision.authority.data.Authority; |
||||
import com.fr.decision.authority.data.User; |
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.decision.webservice.bean.user.UserUpdateBean; |
||||
import com.fr.decision.webservice.impl.user.UserPageQueryAuthorityParam; |
||||
import com.fr.decision.webservice.utils.ControllerFactory; |
||||
import com.fr.decision.webservice.utils.controller.UserController; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.query.data.DataList; |
||||
import com.fr.stable.query.restriction.Restriction; |
||||
import com.fr.third.org.apache.http.HttpEntity; |
||||
import com.fr.third.org.apache.http.HttpStatus; |
||||
import com.fr.third.org.apache.http.client.config.RequestConfig; |
||||
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; |
||||
import com.fr.third.org.apache.http.client.methods.HttpGet; |
||||
import com.fr.third.org.apache.http.client.methods.HttpPost; |
||||
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier; |
||||
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||
import com.fr.third.org.apache.http.entity.StringEntity; |
||||
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
||||
import com.fr.third.org.apache.http.impl.client.HttpClients; |
||||
import com.fr.third.org.apache.http.ssl.SSLContextBuilder; |
||||
import com.fr.third.org.apache.http.ssl.TrustStrategy; |
||||
import com.fr.third.org.apache.http.util.EntityUtils; |
||||
import com.fr.third.springframework.web.util.UriUtils; |
||||
|
||||
import javax.net.ssl.HostnameVerifier; |
||||
import javax.net.ssl.SSLContext; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.X509Certificate; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.UUID; |
||||
|
||||
public class Utils { |
||||
public static String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"; |
||||
public static RequestConfig REQUEST_CONFIG = RequestConfig.custom() |
||||
.setConnectionRequestTimeout(30000) |
||||
.setSocketTimeout(30000) // 服务端相应超时
|
||||
.setConnectTimeout(30000) // 建立socket链接超时时间
|
||||
.build(); |
||||
|
||||
public static CloseableHttpClient createSslHttpClient() { |
||||
try { |
||||
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { |
||||
|
||||
@Override |
||||
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
||||
return true; |
||||
} |
||||
}).build(); |
||||
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; |
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); |
||||
return HttpClients.custom() |
||||
.setSSLSocketFactory(sslsf) |
||||
.build(); |
||||
} catch (Exception e) { |
||||
LogKit.error(e.getMessage(), e); |
||||
} |
||||
return HttpClients.createDefault(); |
||||
} |
||||
|
||||
public static CloseableHttpClient createDefaultHttpClient() { |
||||
CloseableHttpClient httpClient = HttpClients.custom() |
||||
.build(); |
||||
return httpClient; |
||||
} |
||||
|
||||
public static synchronized CloseableHttpClient createHttpClient(String url) { |
||||
CloseableHttpClient httpClient = null; |
||||
if (StringKit.isEmpty(url)) { |
||||
httpClient = createDefaultHttpClient(); |
||||
return httpClient; |
||||
} |
||||
|
||||
if (url.startsWith("https://")) { |
||||
httpClient = createSslHttpClient(); |
||||
return httpClient; |
||||
} |
||||
httpClient = createDefaultHttpClient(); |
||||
return httpClient; |
||||
} |
||||
|
||||
public static synchronized String createHttpGetContent(CloseableHttpClient httpClient, String url, String basicAuth) throws IOException { |
||||
if ((httpClient == null) || (StringKit.isEmpty(url))) { |
||||
return ""; |
||||
} |
||||
|
||||
HttpGet httpGet = new HttpGet(url); |
||||
httpGet.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT); |
||||
if (StringKit.isNotEmpty(basicAuth)) { |
||||
httpGet.addHeader("Authorization", basicAuth); |
||||
} |
||||
|
||||
httpGet.setConfig(Utils.REQUEST_CONFIG); |
||||
CloseableHttpResponse response = httpClient.execute(httpGet); |
||||
int statusCode = response.getStatusLine().getStatusCode(); |
||||
if (statusCode != HttpStatus.SC_OK) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http status:" + statusCode); |
||||
return ""; |
||||
} |
||||
|
||||
HttpEntity httpEntity = response.getEntity(); |
||||
if (httpEntity == null) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http响应内容为空"); |
||||
return ""; |
||||
} |
||||
String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); |
||||
response.close(); |
||||
if (StringKit.isEmpty(responseContent)) { |
||||
LogKit.info("http请求出错,http响应内容为空1"); |
||||
return ""; |
||||
} |
||||
return responseContent; |
||||
} |
||||
|
||||
public static synchronized String createHttpPostContent(CloseableHttpClient httpClient, String url, String bodyContent, String basicAuth, String contentType) throws IOException { |
||||
if ((httpClient == null) || (StringKit.isEmpty(url))) { |
||||
return ""; |
||||
} |
||||
|
||||
HttpPost httpPost = new HttpPost(url); |
||||
httpPost.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT); |
||||
httpPost.setConfig(Utils.REQUEST_CONFIG); |
||||
if (StringKit.isNotEmpty(basicAuth)) { |
||||
httpPost.addHeader("Authorization", basicAuth); |
||||
} |
||||
if (StringKit.isNotEmpty(contentType)) { |
||||
httpPost.addHeader("Content-Type", contentType); |
||||
} |
||||
StringEntity bodyEntity = new StringEntity(bodyContent, "UTF-8"); |
||||
httpPost.setEntity(bodyEntity); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost); |
||||
int statusCode = response.getStatusLine().getStatusCode(); |
||||
if (statusCode != HttpStatus.SC_OK) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http status:" + statusCode); |
||||
return ""; |
||||
} |
||||
|
||||
HttpEntity httpEntity = response.getEntity(); |
||||
if (httpEntity == null) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http响应内容为空"); |
||||
return ""; |
||||
} |
||||
String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); |
||||
response.close(); |
||||
if (StringKit.isEmpty(responseContent)) { |
||||
LogKit.info("http请求出错,http响应内容为空1"); |
||||
return ""; |
||||
} |
||||
return responseContent; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取完整请求链接 |
||||
* |
||||
* @param req 请求 |
||||
* @return |
||||
*/ |
||||
public static String getFullRequestUrl(HttpServletRequest req) { |
||||
if (req == null) { |
||||
return ""; |
||||
} |
||||
String url = req.getRequestURL().toString(); |
||||
String queryUrl = req.getQueryString(); |
||||
if ((queryUrl == null) || "null".equalsIgnoreCase(queryUrl)) { |
||||
queryUrl = ""; |
||||
} else { |
||||
queryUrl = "?" + queryUrl; |
||||
} |
||||
String fullUrl = url + queryUrl; |
||||
return fullUrl; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 重定向 |
||||
* |
||||
* @param res |
||||
* @param url |
||||
*/ |
||||
public static void sendRedirect(HttpServletResponse res, String url) { |
||||
if ((res == null) || (StringKit.isEmpty(url))) { |
||||
return; |
||||
} |
||||
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); |
||||
res.setHeader("Location", url); |
||||
} |
||||
|
||||
|
||||
public static synchronized String getUuid() { |
||||
String uuid = UUID.randomUUID().toString().replace("-", ""); |
||||
return uuid; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取用户信息 |
||||
* |
||||
* @param userCount 用户数,-1为不限制用户数 |
||||
* @param isAdminContained 是否包含管理员用户 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static List<UserBean> getAllUsers(int userCount, boolean isAdminContained) throws Exception { |
||||
List<UserBean> userBeans = new ArrayList<>(); |
||||
if (userCount == 0) { |
||||
return userBeans; |
||||
} |
||||
int limitCount = userCount; |
||||
if (userCount <= -1) { |
||||
limitCount = Integer.MAX_VALUE; |
||||
} |
||||
List<String> adminUserIds = UserService.getInstance().getAdminUserIdList(); |
||||
if ((adminUserIds == null) || (adminUserIds.size() <= 0)) { |
||||
return userBeans; |
||||
} |
||||
String adminUserId = adminUserIds.get(0); |
||||
|
||||
UserController userController = ControllerFactory.getInstance().getUserController(adminUserId); |
||||
|
||||
DataList dataList = userController.findPageUsersPerfectMatch(adminUserId, new UserPageQueryAuthorityParam(1, limitCount, "", null), new Restriction[0]); |
||||
if (dataList == null) { |
||||
return userBeans; |
||||
} |
||||
List users = dataList.getList(); |
||||
if ((users == null) || (users.size() <= 0)) { |
||||
return userBeans; |
||||
} |
||||
User user; |
||||
UserBean userBean; |
||||
String userId; |
||||
for (int i = 0, max = users.size() - 1; i <= max; i++) { |
||||
user = (User) users.get(i); |
||||
userId = user.getId(); |
||||
if ((!isAdminContained) && (adminUserIds.contains(userId))) { |
||||
continue; |
||||
} |
||||
userBean = new UserBean(user.getEmail(), user.isEnable(), user.getMobile(), user.getRealName(), user.getUserName(), user.getId()); |
||||
userBean.setCreationType(user.getCreationType().toInteger()); |
||||
userBeans.add(userBean); |
||||
} |
||||
return userBeans; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取不包含管理员的用户信息 |
||||
* |
||||
* @param userCount |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static List<UserBean> getAllUsersWithoutAdmin(int userCount) throws Exception { |
||||
return getAllUsers(userCount, false); |
||||
} |
||||
|
||||
public static List<String> getAllUserNames(int userCount, boolean isAdminContained) throws Exception { |
||||
List<String> userNames = new ArrayList<>(); |
||||
List<UserBean> userBeans = getAllUsers(userCount, isAdminContained); |
||||
UserBean userBean; |
||||
for (int i = 0, max = userBeans.size() - 1; i <= max; i++) { |
||||
userBean = userBeans.get(i); |
||||
userNames.add(userBean.getUsername()); |
||||
} |
||||
return userNames; |
||||
} |
||||
|
||||
public static List<String> getAllUserNamesWithoutAdmin(int userCount) throws Exception { |
||||
return getAllUserNames(userCount, false); |
||||
} |
||||
|
||||
/** |
||||
* 通过用户名删除用户,管理员用户无法删除 |
||||
* |
||||
* @param username 用户名 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static int deleteUsersByUsername(String username) throws Exception { |
||||
if (StringUtils.isEmpty(username)) { |
||||
return 0; |
||||
} |
||||
User user = UserService.getInstance().getUserByUserName(username); |
||||
if (user == null) { |
||||
return 0; |
||||
} |
||||
String userId = user.getId(); |
||||
List<String> adminUserIds = UserService.getInstance().getAdminUserIdList(); |
||||
if ((adminUserIds != null) && (adminUserIds.size() >= 1) && (adminUserIds.contains(userId))) { |
||||
return 0; |
||||
} |
||||
UserUpdateBean userUpdateBean = new UserUpdateBean(); |
||||
userUpdateBean.setRemoveUserIds(new String[]{userId}); |
||||
return UserService.getInstance().deleteUsers(userUpdateBean); |
||||
} |
||||
|
||||
public static int deleteUsersByUsernames(List<String> usernames) throws Exception { |
||||
if ((usernames == null) || (usernames.size() <= 0)) { |
||||
return 0; |
||||
} |
||||
String username; |
||||
int totalCount = 0, count; |
||||
for (int i = 0, max = usernames.size() - 1; i <= max; i++) { |
||||
username = usernames.get(i); |
||||
count = deleteUsersByUsername(username); |
||||
totalCount = totalCount + count; |
||||
} |
||||
return totalCount; |
||||
} |
||||
|
||||
public static void editRealNameByUsername(String username, String realName) throws Exception { |
||||
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(realName)) { |
||||
return; |
||||
} |
||||
User user = UserService.getInstance().getUserByUserName(username); |
||||
if (user == null) { |
||||
return; |
||||
} |
||||
UserBean userBean = new UserBean(user.getEmail(), user.isEnable(), user.getMobile(), realName, user.getUserName(), user.getId()); |
||||
UserService.getInstance().editAccount(username, userBean); |
||||
} |
||||
|
||||
/** |
||||
* 根据链接获取报表路径 |
||||
* |
||||
* @param url |
||||
* @return |
||||
*/ |
||||
public static String getReportPathByUrl(String url) { |
||||
if (StringKit.isEmpty(url)) { |
||||
return ""; |
||||
} |
||||
String reportPath = ""; |
||||
try { |
||||
reportPath = getReportPathWithViewletValueByUrl(url); |
||||
if (StringKit.isEmpty(reportPath)) { |
||||
reportPath = getReportPathWithEntryIdByUrl(url); |
||||
} |
||||
} catch (Exception e) { |
||||
LogKit.error("根据链接获取报表路径出错," + e.getMessage(), e); |
||||
return ""; |
||||
} |
||||
return reportPath; |
||||
} |
||||
|
||||
private static String getReportPathWithViewletValueByUrl(String url) throws UnsupportedEncodingException { |
||||
if (StringKit.isEmpty(url)) { |
||||
return ""; |
||||
} |
||||
int index = url.indexOf("viewlet="); |
||||
if (index < 0) { |
||||
return ""; |
||||
} |
||||
int index1 = url.indexOf("&", index); |
||||
String tempValue = ""; |
||||
if (index1 >= 0) { |
||||
tempValue = url.substring(index + "viewlet=".length(), index1); |
||||
} else { |
||||
tempValue = url.substring(index + "viewlet=".length()); |
||||
} |
||||
if (StringKit.isEmpty(tempValue)) { |
||||
return ""; |
||||
} |
||||
String reportName = UriUtils.decode(UriUtils.decode(tempValue, "UTF-8"), "UTF-8"); |
||||
return reportName; |
||||
} |
||||
|
||||
private static String getReportPathWithEntryIdByUrl(String url) throws Exception { |
||||
|
||||
if (StringKit.isEmpty(url)) { |
||||
return ""; |
||||
} |
||||
String tempStr = "decision/v10/entry/access/"; |
||||
int index = url.indexOf(tempStr); |
||||
if (index <= 0) { |
||||
return ""; |
||||
} |
||||
String tempUrl = url.substring(index + tempStr.length()); |
||||
String entryId = ""; |
||||
index = tempUrl.indexOf("?"); |
||||
if (index >= 0) { |
||||
entryId = tempUrl.substring(0, index); |
||||
} else { |
||||
entryId = tempUrl; |
||||
} |
||||
Authority entry = AuthorityContext.getInstance().getAuthorityController().getById(entryId); |
||||
if (entry == null) { |
||||
return ""; |
||||
} |
||||
String reportPath = entry.getPath(); |
||||
return reportPath; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,189 @@
|
||||
package com.fr.plugin.third.party.jsdbadeh.function; |
||||
|
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.function.ROUND5; |
||||
import com.fr.general.FArray; |
||||
import com.fr.general.GeneralUtils; |
||||
import com.fr.plugin.context.PluginContexts; |
||||
import com.fr.script.AbstractFunction; |
||||
import com.fr.stable.ArrayUtils; |
||||
import com.fr.stable.Primitive; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.math.BigInteger; |
||||
|
||||
|
||||
public class Round6Function extends AbstractFunction { |
||||
public Object run(Object[] args) { |
||||
if (!PluginContexts.currentContext().isAvailable()) { |
||||
LogKit.error("函数ROUND6插件许可证过期"); |
||||
return Primitive.ERROR_VALUE; |
||||
} |
||||
|
||||
int len = ArrayUtils.getLength(args); |
||||
if (len <= 1) { |
||||
LogKit.error("函数ROUND6参数个数必须两个以上"); |
||||
//return Primitive.ERROR_VALUE;
|
||||
return 0; |
||||
} |
||||
|
||||
Object obj0 = args[0]; |
||||
if (isNotNumber(obj0)) { |
||||
LogKit.error("函数ROUND6第一个参数必须为数字"); |
||||
//return Primitive.ERROR_VALUE;
|
||||
return 0; |
||||
} |
||||
|
||||
Object obj1 = args[1]; |
||||
if ((obj1 == null) || (!(obj1 instanceof Integer))) { |
||||
LogKit.error("函数ROUND6第二个参数必须为整数"); |
||||
//return Primitive.ERROR_VALUE;
|
||||
return 0; |
||||
} |
||||
|
||||
String obj0Value = String.valueOf(obj0); |
||||
String obj1Value = String.valueOf(obj1); |
||||
|
||||
if (StringKit.isEmpty(obj0Value) || StringKit.isEmpty(obj1Value)) { |
||||
//return Primitive.ERROR_VALUE;
|
||||
return 0; |
||||
} |
||||
|
||||
BigDecimal b = new BigDecimal(obj0Value); |
||||
int scale = Integer.valueOf(obj1Value); |
||||
|
||||
BigDecimal value = null; |
||||
if (scale >= 0) { |
||||
value = getValue(b, scale); |
||||
return value.doubleValue(); |
||||
} |
||||
value = getValue1(b, scale); |
||||
return value.doubleValue(); |
||||
} |
||||
|
||||
private boolean isNotNumber(Object obj) { |
||||
return !isNumber(obj); |
||||
} |
||||
|
||||
private boolean isNumber(Object obj) { |
||||
if (obj == null) { |
||||
return false; |
||||
} |
||||
|
||||
if (obj instanceof Integer) { |
||||
return true; |
||||
} |
||||
|
||||
if (obj instanceof BigDecimal) { |
||||
return true; |
||||
} |
||||
|
||||
if (obj instanceof Long) { |
||||
return true; |
||||
} |
||||
|
||||
if (obj instanceof BigInteger) { |
||||
return true; |
||||
} |
||||
|
||||
if (obj instanceof Byte) { |
||||
return true; |
||||
} |
||||
|
||||
if (obj instanceof Double) { |
||||
return true; |
||||
} |
||||
|
||||
if (obj instanceof Float) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
private BigDecimal getValue(BigDecimal b, int scale) { |
||||
BigDecimal value = b.setScale(scale + 1, BigDecimal.ROUND_DOWN); |
||||
String valuePlainString = value.toPlainString(); |
||||
char endChar = valuePlainString.charAt(valuePlainString.length() - 1); |
||||
if ('5' != endChar) { |
||||
return b.setScale(scale, BigDecimal.ROUND_HALF_UP); |
||||
} |
||||
|
||||
BigDecimal value1 = b.setScale(scale + 2, BigDecimal.ROUND_DOWN); |
||||
valuePlainString = value1.toPlainString(); |
||||
endChar = valuePlainString.charAt(valuePlainString.length() - 1); |
||||
if ('0' != endChar) { |
||||
return b.setScale(scale, BigDecimal.ROUND_HALF_UP); |
||||
} |
||||
|
||||
char previousChar = valuePlainString.charAt(valuePlainString.length() - 3); |
||||
if ('.' == previousChar) { |
||||
previousChar = valuePlainString.charAt(valuePlainString.length() - 4); |
||||
} |
||||
if (isOddDigit(previousChar)) { |
||||
return b.setScale(scale, BigDecimal.ROUND_HALF_UP); |
||||
} |
||||
return b.setScale(scale, BigDecimal.ROUND_DOWN); |
||||
} |
||||
|
||||
private BigDecimal getValue1(BigDecimal b, int scale) { |
||||
int currentScale = 0 - scale; |
||||
int maxScale = getIntegerLength(b) - 1; |
||||
if (maxScale < 0) { |
||||
maxScale = 0; |
||||
} |
||||
if (currentScale > maxScale) { |
||||
currentScale = maxScale; |
||||
} |
||||
if (currentScale == 0) { |
||||
return getValue(b, 0); |
||||
} |
||||
|
||||
BigDecimal tenValue = new BigDecimal("10").pow(currentScale); |
||||
BigDecimal value = b.divide(tenValue); |
||||
value = getValue(value, 0); |
||||
value = value.multiply(tenValue); |
||||
return value; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 判断是否是奇数数字字符 |
||||
* |
||||
* @param value |
||||
* @return |
||||
*/ |
||||
private boolean isOddDigit(char value) { |
||||
char[] digits = {'1', '3', '5', '7', '9'}; |
||||
for (int i = 0, max = digits.length - 1; i <= max; i++) { |
||||
if (digits[i] == value) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 获取整数部分的数字长度 |
||||
* |
||||
* @param value |
||||
* @return |
||||
*/ |
||||
private int getIntegerLength(BigDecimal value) { |
||||
if (value == null) { |
||||
return 0; |
||||
} |
||||
String valuePlainString = value.toPlainString(); |
||||
String tempStr = valuePlainString; |
||||
if (valuePlainString.startsWith("-")) { |
||||
tempStr = valuePlainString.substring(1); |
||||
} |
||||
int index = tempStr.indexOf("."); |
||||
if (index >= 0) { |
||||
tempStr = tempStr.substring(0, index); |
||||
} |
||||
int len = tempStr.length(); |
||||
return len; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue