Files
jshERP/src/main/java/com/jsh/erp/utils/OrderUtils.java
2018-12-19 23:54:53 +08:00

70 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.jsh.erp.utils;
/**
* @author jishenghua qq752718920 2018-10-7 15:26:27
*/
public class OrderUtils {
/**
* 将指定字段排序
*
* @param orders 格式 属性名,排序方式 例如( name,asc或ip,desc)
* @return 排序字符串 例如name asc 或 ip desc
*/
public static String getOrderString(String orders) {
if (StringUtil.isNotEmpty(orders)) {
String[] splits = orders.split(Constants.SPLIT);
if (splits.length == 2) {
String column = ColumnPropertyUtil.propertyToColumn(splits[0]);
if (column.equals("audit_status")) {
// TODO: 2015/12/24 这么处理不好,得相伴办法调整
return "IF(`audit_status`=3,-1,`audit_status`) " + splits[1];
} else if (column.equals("create_time") || column.equals("modify_time")) {
// TODO: 2015/12/24 这么处理不好,得相伴办法调整
return column + " " + splits[1];
} else {
return "convert(" + column + " using gbk) " + splits[1];
}
}
}
return "";
}
public static String getJoinTablesOrderString(String orders, String tableName) {
if (StringUtil.isNotEmpty(orders)) {
String[] splits = orders.split(Constants.SPLIT);
if (splits.length == 2) {
return "convert(" + tableName + "." + ColumnPropertyUtil.propertyToColumn(splits[0]) + " using gbk) " + splits[1];
}
}
return "";
}
/**
* 将指定字段排序
* inet_atonmysql将IP 转成 long类别函数
*
* @param orders 格式 属性名,排序方式 例如( name,asc或ip,desc)
* @param ipPropertyName 如果需要按IP属性排序需要将属性名传入可不传
* @return 排序字符串 例如name asc 或 ip desc
*/
public static String getOrderString(String orders, String... ipPropertyName) {
if (StringUtil.isNotEmpty(orders)) {
String[] splits = orders.split(Constants.SPLIT);
if (splits.length == 2) {
String column = ColumnPropertyUtil.propertyToColumn(splits[0]);
if (ipPropertyName != null && ipPropertyName.length > 0) {
for (String ip : ipPropertyName) {
if (ip.equals(column)) {
return "inet_aton(" + column + ") " + splits[1];
}
}
}
return column + " " + splits[1];
}
}
return "";
}
}