Commit 74bb1ffa authored by yanzg's avatar yanzg

公式计算处理

parent 723caa19
......@@ -8,6 +8,7 @@ import com.yanzuoguang.db.impl.DbRow;
import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.StringFormatHandle;
import com.yanzuoguang.util.helper.StringHelper;
......@@ -87,6 +88,12 @@ public abstract class BaseDaoSql {
return table;
}
protected void checkTable(){
if(this.table == null){
throw YzgError.getRuntimeException("002");
}
}
/**
* 初始化当前类对应的SQL语句对象
*/
......
......@@ -2,13 +2,11 @@ package com.yanzuoguang.dao.impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -350,4 +348,147 @@ public class TableSqlCache {
public void setNameCache(MemoryCache<SqlData> nameCache) {
this.nameCache = nameCache;
}
/**
* 获取主键名称
*
* @return 获取主键名称
*/
public String getKeyField() {
if (this.table == null) {
throw YzgError.getRuntimeException("002");
}
return this.table.getKeyName();
}
/**
* 设置主键值
*
* @param model 需要设置的实体
* @param key 需要设置的主键值
*/
public void setKeyValue(Object model, String key) {
String keyField = this.getKeyField();
ObjectHelper.set(model, keyField, key);
}
/**
* 获取主键值
*
* @param model 需要获取主键的实体
* @return
*/
public String getKeyValue(Object model) {
String keyField = this.getKeyField();
Object key = ObjectHelper.get(model, keyField);
if (StringHelper.isEmpty(key)) {
return "";
}
String keyString = key.toString();
if (DaoConst.ZERO.equals(keyString)) {
keyString = "";
}
return keyString;
}
/**
* 初始化key
*
* @param model
* @return
*/
public String initKeyValue(Object model) {
// 判断主键是字符串和需要生成主键
boolean isKeyString = this.table.getKeyType() == String.class;
String keyString = this.getKeyValue(model);
// 生成主键
if (StringHelper.isEmpty(keyString) && isKeyString) {
keyString = StringHelper.getNewID();
this.setKeyValue(model, keyString);
}
return keyString;
}
/**
* 根据输入参数来获取主键
*
* @param from 可以为实体或字符串。为实体时必须包含 主键 字段 或者 id 字段。
* @return
*/
public String getInputKey(Object from) {
String key;
if (from != null && from.getClass() == String.class) {
key = StringHelper.toString(from);
} else {
key = this.getKeyValue(from);
}
if (StringHelper.isEmpty(key)) {
key = StringHelper.toString(ObjectHelper.get(from, "id"));
}
if (StringHelper.isEmpty(key)) {
key = "";
}
return key;
}
/**
* 获取请求对象的主键对象
*
* @param from
* @return
*/
public Object getKeyObject(Object from) {
String keyValue = this.getInputKey(from);
return getKeyObject(from, keyValue);
}
/**
* 根据输入参数来获取主键
*
* @param keyValue 根据主键获取对象
* @return
*/
public Object getKeyObject(Object from, String keyValue) {
if (!StringHelper.isEmpty(keyValue)) {
// 去掉其他非主键的属性
from = new HashMap<String, Object>(DaoConst.COLLECTION_INIT_SIZE);
this.setKeyValue(from, keyValue);
}
return from;
}
/**
* 按照主键对集合进行排序
*
* @param collection
* @return
*/
public List getKeySort(Collection collection) {
// 集合数据
List list = new ArrayList();
list.addAll(collection);
// 对集合进行排序
Collections.sort(list, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
String key1 = StringHelper.getFirst(TableSqlCache.this.getKeyValue(o1));
String key2 = StringHelper.getFirst(TableSqlCache.this.getKeyValue(o2));
return key1.compareTo(key2);
}
});
return list;
}
/**
* 获取返回值数组
* @param collection
* @return
*/
public List<String> getCollectionRet(Collection collection) {
List<String> rets = new ArrayList<>();
for (Object item : collection) {
rets.add(this.getKeyValue(item));
}
return rets;
}
}
......@@ -6,6 +6,9 @@ import com.yanzuoguang.db.impl.DbPrintSql;
import com.yanzuoguang.util.helper.JsonHelper;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestDbPrintSql {
@Test
public void test() {
......@@ -44,4 +47,24 @@ public class TestDbPrintSql {
System.out.println(JsonHelper.serialize(cache.getNameCache(), true));
}
@Test
public void testTableStructSort() {
TableSqlCache cache = new TableSqlCache();
TableStruct table = new TableStruct("test", TestTableGroupVo.class);
table.init(cache);
List<TestTableGroupVo> list = new ArrayList<>();
list.add(new TestTableGroupVo("2"));
list.add(new TestTableGroupVo("1"));
list.add(new TestTableGroupVo("3"));
List listTo = cache.getKeySort(list);
String json = JsonHelper.serialize(listTo,true);
System.out.println(json);
}
}
......@@ -23,6 +23,13 @@ public class TestTableGroupVo {
@TableAnnotation(value = "has1", type = DaoConst.FIELD_ADD_GROUP)
private int has1;
public TestTableGroupVo() {
}
public TestTableGroupVo(String id) {
this.id = id;
}
public String getId() {
return id;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment