Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.fastchar.extjs.action.ExtEntityAction Maven / Gradle / Ivy
Go to download
FastChar-ExtJs is a Java Web framework that uses extjs libraries.Quickly build a background management system
package com.fastchar.extjs.action;
import com.fastchar.core.*;
import com.fastchar.database.FastPage;
import com.fastchar.database.FastType;
import com.fastchar.database.info.FastColumnInfo;
import com.fastchar.database.info.FastSqlInfo;
import com.fastchar.extjs.FastExtConfig;
import com.fastchar.extjs.annotation.AFastLog;
import com.fastchar.extjs.annotation.AFastSession;
import com.fastchar.extjs.core.FastExtEntity;
import com.fastchar.extjs.core.FastExtEnumHelper;
import com.fastchar.extjs.core.FastExtLayerHelper;
import com.fastchar.extjs.core.FastExtSameHelper;
import com.fastchar.extjs.core.database.FastExtColumnInfo;
import com.fastchar.extjs.core.database.FastExtData;
import com.fastchar.extjs.core.database.FastExtTableInfo;
import com.fastchar.extjs.core.database.FastSqlTool;
import com.fastchar.extjs.core.enums.FastEnumInfo;
import com.fastchar.extjs.core.heads.FastHeadExtInfo;
import com.fastchar.extjs.echarts.FastEChartsBean;
import com.fastchar.extjs.entity.ExtManagerEntity;
import com.fastchar.extjs.entity.ExtSystemConfigEntity;
import com.fastchar.extjs.info.ExtExcelModelInfo;
import com.fastchar.extjs.interfaces.IFastImportDataListener;
import com.fastchar.extjs.utils.POIHelper;
import com.fastchar.extjs.utils.POIUtils;
import com.fastchar.utils.*;
import com.google.gson.reflect.TypeToken;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import java.util.*;
@SuppressWarnings({"ResultOfMethodCallIgnored", "SuspiciousMethodCalls"})
@AFastSession
public class ExtEntityAction extends FastAction {
@Override
protected String getRoute() {
return "/entity";
}
/**
* 获取实体的列表数据
* 参数:
* page 页数 {int}
* limit 每页大小 {int}
* entityCode 实体编号 {String}
* where 列表sql的where判断语句 ,例如:where['t.userId'] = 1
* indexSort 指定排序列 json数组,例如:[{"property":"serviceNickName","direction":"DESC"}]
* power 是否需要权限层级筛选,默认:true
*/
public FastPage> list() {
String entityCode = getParam("entityCode", true);
int page = getParamToInt("page", 1);
int pageSize = getParamToInt("limit", 10);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return null;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return null;
}
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
Map where = getParamToMap("where");
entity.putAll(where);
Map indexSort = getParamToMap("indexSort");
String sort = getParam("sort");
if (FastStringUtils.isNotEmpty(sort)) {
List> sortList = FastChar.getJson().fromJson(sort,
new TypeToken>>() {
}.getType());
for (Map map : sortList) {
if (map.get("property").startsWith("@")) {
continue;
}
entity.put(FastNumberUtils.formatToInt(indexSort.get(map.get("property"))) + map.get("property") + ":sort", map.get("direction"));
}
}
// && !getParamToBoolean("fromTree", false) 未说明作用,暂时取消
if (getParamToBoolean("power", Boolean.TRUE)) {
entity.pullLayer(managerEntity);
}
FastPage> fastPage = entity.showList(page, pageSize);
if (fastPage.getSqlInfo() != null) {
managerEntity.put("Sql" + entityCode + "_" + entity.getBoolean("^fromRecycle"), fastPage.getSqlInfo().getSql());
}
return fastPage;
}
/**
* 添加实体数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* data 以data为前缀的参数,提交实体数据,例如:data.topicError=无,data.topicState=1,data.topicTitle=我的话题
* method 执行实体的方法名,默认为:save方法
*/
@AFastLog(value = "页面【${menu}】添加了一条数据!", type = "添加数据")
public void save() {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
FastEntity> entity = getParamToEntity("data", extEntityClass);
if (FastStringUtils.isEmpty(menu)) {
menu = entity.getTableDetails();
}
setRequestAttr("menu", menu);
List> saveEntities = new ArrayList<>();
pluckArrayValue(entity, saveEntities);
List saveResult = new ArrayList<>();
for (FastEntity> saveEntity : saveEntities) {
if (saveEntity.getTable() != null) {
if (saveEntity.getTable() instanceof FastExtTableInfo) {
FastExtTableInfo extTableInfo = saveEntity.getTable();
if (extTableInfo.isBindSessionLayer()) {
saveEntity.put(FastExtEntity.EXTRA_PARENT_LAYER_CODE, managerEntity.getLayerValue());
}
}
}
saveEntity.put("fromWeb", true);
saveEntity.put("session", managerEntity);
saveEntity.clearEmpty();
String[] checks = getParamToArray("check");
String method = getParam("method", "save");
if (checks.length == 0) {
if (saveEntity.save()) {
saveResult.add(new FastHandler().setCode(0).setError("添加成功!"));
continue;
}
} else {
if (method.equalsIgnoreCase("save")) {
if (saveEntity.save(checks)) {
saveResult.add(new FastHandler().setCode(0).setError("添加成功!"));
continue;
} else if (FastStringUtils.isEmpty(saveEntity.getError())) {
saveResult.add(new FastHandler().setCode(-1).setError("添加失败!数据或已存在!"));
continue;
}
} else if (method.equalsIgnoreCase("push")) {
FastHandler handler = new FastHandler();
if (saveEntity.push(handler, checks)) {
if (handler.getCode() == 0) {
saveResult.add(new FastHandler().setCode(0).setError("添加成功!"));
continue;
}
saveResult.add(new FastHandler().setCode(0).setError("更新成功!"));
continue;
}
}
}
saveResult.add(new FastHandler().setCode(-1).setError("添加失败!" + saveEntity.getError()));
}
if (saveResult.size() == 1) {
responseJson(saveResult.get(0).getCode(), saveResult.get(0).getError());
} else {
StringBuilder stringBuilder = new StringBuilder();
int errorCount = 0;
for (int i = 0; i < saveResult.size(); i++) {
FastHandler handler = saveResult.get(i);
if (handler.getCode() != 0) {
errorCount++;
}
stringBuilder.append("数据").append(i + 1).append(":").append(handler.getError()).append(" ");
}
if (errorCount == saveResult.size()) {
responseJson(-1, stringBuilder.toString());
} else {
responseJson(0, stringBuilder.toString());
}
}
}
private void pluckArrayValue(FastEntity> entity, List> entities) {
Collection> columns = entity.getTable().getColumns();
boolean hasArray = false;
for (FastColumnInfo> column : columns) {
Object values = entity.get(column.getName());
if (values == null || FastStringUtils.isEmpty(values.toString())) {
continue;
}
if (entity.getBoolean(column.getName() + "@JsonArray")) {
entity.remove(column.getName());
entity.remove(column.getName() + "@JsonArray");
values = FastChar.getJson().fromJson(values.toString(), List.class).toArray();
}
if (values.getClass().isArray()) {
hasArray = true;
for (int i = 0; i < Array.getLength(values); i++) {
Object singleValue = Array.get(values, i);
if (singleValue == null || FastStringUtils.isEmpty(singleValue.toString())) {
continue;
}
FastEntity> newEntity = FastChar.getOverrides().newInstance(entity.getClass());
newEntity.setAll(entity);
newEntity.set(column.getName(), singleValue);
pluckArrayValue(newEntity, entities);
}
}
}
if (!hasArray) {
entities.add(entity);
}
}
/**
* 删除实体数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* where 用作sql的where判断语句 ,例如:where['t.userId'] = 1
* data 以data为前缀的参数,提交实体数据,例如:data.topicError=无,data.topicState=1,data.topicTitle=我的话题
*/
@AFastLog(value = "页面【${menu}】进行删除数据!", type = "删除数据")
public void delete() throws Exception {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
boolean all = getParamToBoolean("all", false);
if (all) {
FastExtEntity> extEntity = FastClassUtils.newInstance(extEntityClass);
if (extEntity != null) {
if (FastStringUtils.isEmpty(menu)) {
menu = extEntity.getTableDetails();
}
setRequestAttr("menu", menu);
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
Map where = getParamToMap("where");
extEntity.putAll(where);
extEntity.pullLayer(managerEntity);
boolean fromRecycle = extEntity.getBoolean("^fromRecycle");
String showListSql = managerEntity.getString("Sql" + entityCode + "_" + fromRecycle);
if (FastStringUtils.isEmpty(showListSql)) {
responseJson(-1, "清除失败!请稍后重试!");
}
Collection> primaries = extEntity.getPrimaries();
String keyName = primaries.iterator().next().getName();
for (FastColumnInfo> primary : primaries) {
if (primary.isAutoincrement()) {
keyName = primary.getName();
}
}
int fromIndex = showListSql.indexOf("from");
int whereIndex = showListSql.lastIndexOf("where");
String tableName = extEntity.getTableName();
if (fromRecycle) {
tableName = tableName + "_recycle";
}
String sqlStr = "select t." + keyName + " " + showListSql.substring(fromIndex, whereIndex);
FastSqlInfo sqlInfo = FastSqlTool.appendWhere(extEntity.getFastData().getFastSql(), sqlStr, extEntity);
String deleteSql = "delete from " + tableName + " where " +
keyName + " in ( select * from ( " + sqlInfo.getSql() + ") as tmp ) ";
int update = FastChar.getDB().update(deleteSql, sqlInfo.toParams());
if (update > 0) {
responseJson(0, "删除成功!共删除了" + update + "条数据!");
}
}
} else {
int count = 0;
List extends FastExtEntity>> entity = getParamToEntityList("data", extEntityClass);
for (FastEntity> fastEntity : entity) {
if (FastStringUtils.isEmpty(menu)) {
menu = fastEntity.getTableDetails();
}
setRequestAttr("menu", menu);
fastEntity.put("fromWeb", true);
if (!fastEntity.delete()) {
if (count > 0) {
responseJson(-1, "已成功删除" + count + "条数据后发生错误!" + fastEntity.getError());
}
responseJson(-1, fastEntity.getError());
}
count++;
}
responseJson(0, "删除成功!共删除了" + count + "条数据!", entity);
}
responseJson(-1, "删除失败!");
}
/**
* 修改实体数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* data 以data为前缀的参数,提交实体数据,例如:data.topicError=无,data.topicState=1,data.topicTitle=我的话题
*/
@AFastLog(value = "页面【${menu}】进行修改数据!", type = "修改数据")
public void update() throws Exception {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
setRequestAttr("menu", menu);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
List extends FastExtEntity>> entity = getParamToEntityList("data", extEntityClass);
for (FastEntity> fastEntity : entity) {
if (FastStringUtils.isEmpty(menu)) {
menu = fastEntity.getTableDetails();
}
setRequestAttr("menu", menu);
fastEntity.put("fromWeb", true);
if (!fastEntity.update()) {
if (fastEntity.getModified().size() > 0) {
responseJson(-1, fastEntity.getError());
}
}
}
responseJson(0, "修改成功!", entity);
}
/**
* 批量更新数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* where 用作sql的where判断语句 ,例如:where['t.userId'] = 1
*/
@AFastLog(value = "页面【${menu}】进行批量修改数据!", type = "批量修改数据")
public void batchUpdate() throws Exception {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
setRequestAttr("menu", menu);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastClassUtils.newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
Map where = getParamToMap("where");
entity.putAll(where);
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.pullLayer(managerEntity);
String field = getParam("field", true);
String fieldValue = getParam("fieldValue", true);
String showListSql = managerEntity.getString("Sql" + entityCode + "_" + entity.getBoolean("^fromRecycle"));
if (FastStringUtils.isNotEmpty(showListSql)) {
int fromIndex = showListSql.indexOf("from");
int whereIndex = showListSql.lastIndexOf("where");
Collection> primaries = entity.getPrimaries();
if (primaries.size() > 0) {
FastColumnInfo> fastColumnInfo = primaries.iterator().next();
String inSqlStr = " select " + fastColumnInfo.getName() + " " + showListSql.substring(fromIndex, whereIndex);
FastSqlInfo sqlInfo = entity.toSelectSql(inSqlStr);
String sqlStr = " update " + entity.getTableName() + " set " + field + " = '" + fieldValue + "' " +
" where " + fastColumnInfo.getName() + " in ( select * from ( " + sqlInfo.getSql() + " ) as temp ) ";
int updateCount = FastChar.getDB().update(sqlStr, sqlInfo.toParams());
responseJson(0, "更新成功!共更新" + updateCount + "条数据!");
}
}
responseJson(-1, "操作失败!");
}
/**
* 复制实体数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* data 以data为前缀的参数,提交实体数据,例如:data.topicError=无,data.topicState=1,data.topicTitle=我的话题
*/
@AFastLog(value = "页面【${menu}】进行复制数据!", type = "复制数据")
public void copy() {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
setRequestAttr("menu", menu);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
List extends FastExtEntity>> entity = getParamToEntityList("data", extEntityClass);
for (FastEntity> fastEntity : entity) {
if (FastStringUtils.isEmpty(menu)) {
menu = fastEntity.getTableDetails();
}
setRequestAttr("menu", menu);
fastEntity.put("fromWeb", true);
FastEntity> copySave = fastEntity.copySave();
if (copySave == null) {
responseJson(-1, "复制失败!" + fastEntity.getError());
}
}
responseJson(0, "数据复制成功!");
}
/**
* 清除某个属性为空的所有数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* where 用作sql的where判断语句 ,例如:where['t.userId'] = 1
*/
@AFastLog(value = "页面【${menu}】进行清理数据!", type = "清理数据")
public void clear() throws Exception {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
setRequestAttr("menu", menu);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastClassUtils.newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
if (FastStringUtils.isEmpty(menu)) {
menu = entity.getTableDetails();
}
setRequestAttr("menu", menu);
if (entity.getPrimaries().size() == 0) {
responseJson(-1, "清除失败!此列表不允许此功能!");
}
Map where = getParamToMap("where");
entity.putAll(where);
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.pullLayer(managerEntity);
String showListSql = managerEntity.getString("Sql" + entityCode + "_" + entity.getBoolean("^fromRecycle"));
if (FastStringUtils.isEmpty(showListSql)) {
responseJson(-1, "清除失败!请稍后重试!");
}
int fromIndex = showListSql.indexOf("from");
int whereIndex = showListSql.lastIndexOf("where");
Collection> primaries = entity.getPrimaries();
String keyName = primaries.iterator().next().getName();
String sqlStr = "select t." + keyName + " " + showListSql.substring(fromIndex, whereIndex);
String field = getParam("field", true);
field = FastSqlTool.formatAlias(field, "t");
FastSqlInfo fastSqlInfo = entity.toSelectSql(sqlStr);
sqlStr = fastSqlInfo.getSql() + " and " + field + " is null ";
String deleteSql = "delete from " + entity.getTableName() + " where " +
keyName + " in ( select * from ( " + sqlStr + ") as tmp ) ";
int update = FastChar.getDB().update(deleteSql, fastSqlInfo.toParams());
responseJson(0, "清理成功!共清理" + update + "条数据!");
}
/**
* 清除某个属性重复的数据,保留一条
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* where 用作sql的where判断语句 ,例如:where['t.userId'] = 1
* type 0:只保留最早的一条 1:只保留最新的一条
*/
@AFastLog(value = "页面【${menu}】进行清理重复数据!", type = "清理重复数据")
public void repeat() throws Exception {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
setRequestAttr("menu", menu);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastClassUtils.newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
if (FastStringUtils.isEmpty(menu)) {
menu = entity.getTableDetails();
}
setRequestAttr("menu", menu);
if (entity.getPrimaries().size() == 0) {
responseJson(-1, "清除失败!此列表不允许此功能!");
}
Map where = getParamToMap("where");
entity.putAll(where);
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.pullLayer(managerEntity);
String showListSql = managerEntity.getString("Sql" + entityCode + "_" + entity.getBoolean("^fromRecycle"));
if (FastStringUtils.isEmpty(showListSql)) {
responseJson(-1, "清除失败!请稍后重试!");
}
int fromIndex = showListSql.indexOf("from");
int whereIndex = showListSql.lastIndexOf("where");
Collection> primaries = entity.getPrimaries();
String keyName = primaries.iterator().next().getName();
int type = getParamToInt("type");
String selectItem = "min(" + keyName + ")";
String sqlStr = "select {{selectItem}} " + showListSql.substring(fromIndex, whereIndex);
if (type == 1) {
selectItem = "max(" + keyName + ")";
sqlStr = "select {{selectItem}} " + showListSql.substring(fromIndex, whereIndex);
}
String field = getParam("field", true);
field = FastSqlTool.formatAlias(field, "t");
FastSqlInfo fastSqlInfo = entity.toSelectSql(sqlStr);
sqlStr = fastSqlInfo.getSql();
Object[] params = fastSqlInfo.toParams();
List paramsList = new ArrayList<>(Arrays.asList(params));
if (paramsList.size() == 0) {
String deleteSql = "delete from " + entity.getTableName() + " where " +
keyName + " not in ( select * from ( " + sqlStr.replace("{{selectItem}}", selectItem) + " group by " + field + " ) as tmp ) ";
int update = FastChar.getDB().update(deleteSql, paramsList.toArray());
responseJson(0, "清理成功!共清理" + update + "条重复数据!");
} else {
String deleteSql = "delete from " + entity.getTableName() + " where " +
keyName + " not in ( select * from ( " + sqlStr.replace("{{selectItem}}", selectItem) + " group by " + field + " ) as tmp )" +
" and " +
keyName + " in (select * from (" + sqlStr.replace("{{selectItem}}", keyName) + ") as tmp2 ) ";
paramsList.addAll(Arrays.asList(FastArrayUtils.clone(params)));
int update = FastChar.getDB().update(deleteSql, paramsList.toArray());
responseJson(0, "清理成功!共清理" + update + "条重复数据!");
}
}
/**
* 从回收站中还原实体数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* data 以data为前缀的参数,提交实体数据,例如:data.topicError=无,data.topicState=1,data.topicTitle=我的话题
*/
@AFastLog(value = "页面【${menu}】进行还原数据!", type = "数据还原")
public void reback() {
String entityCode = getParam("entityCode", true);
String menu = getParam("menu");
setRequestAttr("menu", menu);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
List extends FastExtEntity>> entity = getParamToEntityList("data", extEntityClass);
for (FastEntity> fastEntity : entity) {
if (FastStringUtils.isEmpty(menu)) {
menu = fastEntity.getTableDetails();
}
setRequestAttr("menu", menu);
FastExtData> fastData = (FastExtData>) fastEntity.getFastData();
if (fastData.copyFromRecycle()) {
fastData.deleteRecycle();
}
}
responseJson(0, "还原成功!");
}
/**
* 计算实体指定属性的值
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* type 计算类型,为sql的计算函数,例如:sum、avg
* field 需要计算的字段名
*/
public void compute() throws Exception {
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastClassUtils.newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
Map where = getParamToMap("where");
entity.putAll(where);
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.pullLayer(managerEntity);
String type = getParam("type", true);
String field = getParam("field", true);
field = FastSqlTool.formatAlias(field, "t");
String sqlStr;
String showListSql = managerEntity.getString("Sql" + entityCode + "_" + entity.getBoolean("^fromRecycle"));
if (FastStringUtils.isNotEmpty(showListSql)) {
int fromIndex = showListSql.indexOf("from");
int whereIndex = showListSql.lastIndexOf("where");
sqlStr = "select " + type + "(" + field + ") as result " + showListSql.substring(fromIndex, whereIndex);
} else {
sqlStr = "select " + type + "(" + field + ") as result from " + entity.getTableName();
}
FastSqlInfo sqlInfo = entity.toSelectSql(sqlStr);
FastEntity> fastEntity = FastChar.getDB().selectFirst(sqlInfo.getSql(), sqlInfo.toParams());
if (fastEntity != null) {
responseJson(0, "计算成功!", fastEntity.getFloat("result", 4));
} else {
responseJson(-1, "计算失败!");
}
}
/**
* 导出实体数据excel
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* where 列表sql的where判断语句 ,例如:where['t.userId'] = 1
* indexSort 指定排序列 json数组,例如:[{"property":"serviceNickName","direction":"DESC"}]
* column 需要导出的列 json数组
*/
@AFastLog(value = "导出了【${entityDetails}】数据!", type = "数据导出")
public void export() throws Exception {
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
setRequestAttr("entityDetails", entity.getTableDetails());
Map where = getParamToMap("where");
entity.putAll(where);
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.pullLayer(managerEntity);
Map index = getParamToMap("indexSort");
String sort = getParam("sort");
if (FastStringUtils.isNotEmpty(sort)) {
List> sortList = FastChar.getJson().fromJson(sort,
new TypeToken>>() {
}.getType());
for (Map map : sortList) {
if (map.get("property").startsWith("@")) {
continue;
}
entity.put(FastNumberUtils.formatToInt(index.get(map.get("property"))) + map.get("property") + ":sort", map.get("direction"));
}
}
List> columns = getParamToMapList("column");
if (getParamToBoolean("exportIndex", false)) {
Map indexColumn = new HashMap<>();
indexColumn.put("text", "序号");
indexColumn.put("dataIndex", "__index");
columns.add(0, indexColumn);
}
String title = getParam("title", true);
FastPage> fastPage = entity.showList(-1, -1);
List> list = fastPage.getList();
if (list.size() == 0) {
responseJson(-1, "暂无数据!");
}
//创建一个新的Excel
HSSFWorkbook workBook = new HSSFWorkbook();
POIHelper poiHelper = new POIHelper(workBook);
//创建sheet页
HSSFSheet sheet = workBook.createSheet();
//sheet页名称
workBook.setSheetName(0, title.replace("/", "_"));
//设置默认的行高
sheet.setDefaultRowHeight((short) (30 * 20));
sheet.setDefaultRowHeightInPoints((short) 30);
HSSFDataFormat dataFormat = workBook.createDataFormat();
HSSFCellStyle titleCellStyle = poiHelper.getCellStyle("title");
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont font = poiHelper.getFont("default");
font.setBold(true);
font.setFontHeightInPoints((short) 14);
titleCellStyle.setFont(font);
int[] rowIndex = new int[]{0};
HSSFRow tableNameRow = sheet.createRow(rowIndex[0]++);
HSSFCell tableNameCell = tableNameRow.createCell(0);
tableNameCell.setCellStyle(titleCellStyle);
tableNameCell.setCellValue(title);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columns.size()));//合并第一行的单元格
createTitleRow(titleCellStyle, rowIndex, sheet, columns);
int titleRow = sheet.getLastRowNum();
Map columnValueMap = new HashMap<>();
HSSFCellStyle defaultCellStyle = poiHelper.getCellStyle("default");
defaultCellStyle.cloneStyleFrom(titleCellStyle);
defaultCellStyle.getFont(workBook).setBold(false);
defaultCellStyle.setDataFormat(dataFormat.getFormat("text"));
for (int i = 0; i < columns.size(); i++) {
if (!columnValueMap.containsKey(i)) {
columnValueMap.put(i, 0);
}
Map column = columns.get(i);
if (column.get("enum") != null && FastStringUtils.isNotEmpty(column.get("enum").toString())) {
POIUtils.setEnumCell(sheet, String.valueOf(column.get("enum")), i, String.valueOf(column.get("text")));
}
sheet.setDefaultColumnStyle(i, defaultCellStyle);
HSSFRow row = sheet.getRow(sheet.getLastRowNum());
if (row == null) {
continue;
}
if (column.containsKey("comment")) {
POIUtils.setCellComment(sheet, row.getCell(i), String.valueOf(column.get("comment")));
}
columnValueMap.put(i, Math.max(columnValueMap.get(i), row.getCell(i).getStringCellValue().getBytes().length));
}
for (int i = 0; i < list.size(); i++) {
FastEntity> data = (FastEntity>) list.get(i);
data.put("__index", i + 1);
HSSFRow row = sheet.createRow(i + rowIndex[0]);
for (int j = 0; j < columns.size(); j++) {
Map column = columns.get(j);
HSSFCell cell = row.createCell(j);
Object entityData = data.get(column.get("dataIndex"));
String type = String.valueOf(column.get("type"));
String value = FastStringUtils.defaultValue(entityData, "");
boolean isCollection = false;
if (entityData instanceof Collection) {
isCollection = true;
Collection> collection = (Collection>) entityData;
if (FastBooleanUtils.formatToBoolean(column.get("files"), false)) {
List items = new ArrayList<>();
for (Object o : collection) {
String url = FastStringUtils.defaultValue(o, "");
items.add(url.split("@")[0]);
}
value = FastStringUtils.join(items, " , ");
} else {
value = FastStringUtils.join(collection, " , ");
}
}
if (column.get("enum") != null && FastStringUtils.isNotEmpty(column.get("enum").toString())) {
if (FastStringUtils.isNotEmpty(value)) {
FastEnumInfo anEnum = FastExtEnumHelper.getEnum(String.valueOf(column.get("enum")), value);
if (anEnum != null) {
value = FastStringUtils.defaultValue(anEnum.getText(), value);
}
}
}
if (FastBooleanUtils.formatToBoolean(column.get("file"), false)
&& FastStringUtils.isNotEmpty(value)) {
if (!value.startsWith("http://") && !value.startsWith("https://")) {
value = getProjectHost() + FastStringUtils.stripStart(value, "/");
}
}
if (type.equalsIgnoreCase("numberfield")) {
HSSFCellStyle columnCellStyle = poiHelper.getCellStyle("number");
columnCellStyle.cloneStyleFrom(defaultCellStyle);
columnCellStyle.setDataFormat(dataFormat.getFormat("0.00"));
cell.setCellStyle(columnCellStyle);
}
if (value.length() < 32767) {
if ((value.startsWith("http://") || value.startsWith("https://")) && !isCollection) {
cell.setCellFormula("HYPERLINK(\"" + value + "\",\"" + value + "\")");
HSSFFont linkFont = poiHelper.getFont("link");
linkFont.setFontHeightInPoints((short) 14);
linkFont.setUnderline((byte) 1);
linkFont.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
HSSFCellStyle columnCellStyle = poiHelper.getCellStyle("link");
columnCellStyle.cloneStyleFrom(defaultCellStyle);
columnCellStyle.setFont(linkFont);
cell.setCellStyle(columnCellStyle);
} else {
cell.setCellValue(new HSSFRichTextString(value));
}
columnValueMap.put(cell.getColumnIndex(), Math.max(columnValueMap.get(cell.getColumnIndex()), value.getBytes().length));
} else {
cell.setCellValue("内容长度过大,无法添加到Excel中");
}
}
}
for (Map.Entry integerIntegerEntry : columnValueMap.entrySet()) {
int width = Math.min((integerIntegerEntry.getValue() + 5) * 256, 256 * 256);
sheet.setColumnWidth(integerIntegerEntry.getKey(), width);
}
ExtExcelModelInfo extExcelModelInfo = new ExtExcelModelInfo();
extExcelModelInfo.setTitleRowNum(titleRow);
extExcelModelInfo.setBeginRowNum(titleRow);
extExcelModelInfo.setColumns(columns);
HSSFSheet extExcelModelInfoSheet = workBook.createSheet(ExtExcelModelInfo.class.getSimpleName());
HSSFCell dataCell = extExcelModelInfoSheet.createRow(0).createCell(0);
dataCell.setCellValue(FastChar.getJson().toJson(extExcelModelInfo));
extExcelModelInfoSheet.protectSheet(FastStringUtils.buildUUID());
workBook.setSheetHidden(workBook.getSheetIndex(extExcelModelInfoSheet), true);
String child = "excel/" + title + "_数据" + ".xls";
File file = new File(FastChar.getConstant().getAttachDirectory(), child);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
workBook.write(fileOutputStream);
fileOutputStream.close();
workBook.close();
responseJson(0, "导出成功,共导出" + list.size() + "条数据!", FastFile.newInstance(file).getUrl());
}
private void createTitleRow(HSSFCellStyle cellStyle, int[] rowIndex, HSSFSheet sheet, List> columns) {
java.util.List title = new ArrayList<>();
for (Map column : columns) {
String text = String.valueOf(column.get("text"));
if (column.containsKey("groupHeaderText") && FastStringUtils.isNotEmpty(column.get("groupHeaderText").toString())) {
text = column.get("groupHeaderText").toString() + "@" + text;
}
title.add(text);
}
POIUtils.createTitleRow(cellStyle, rowIndex, sheet, title);
}
/**
* 生成实体excel导入数据的模板
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* column 列集合 json数组
*/
@AFastLog(value = "生成【${entityDetails}】Excel模板!", type = "Excel模板")
public void module() throws Exception {
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
setRequestAttr("entityDetails", entity.getTableDetails());
List> columns = getParamToMapList("column");
if (columns.size() == 0) {
responseJson(-1, "下载失败!列信息错误!");
}
List> waitRemove = new ArrayList<>();
for (Map column : columns) {
String dataIndex = column.get("dataIndex").toString();
if (entity.isPrimary(dataIndex) && entity.isAutoincrement(dataIndex)) {
waitRemove.add(column);
}
if (entity.isLink(dataIndex)) {
FastExtColumnInfo columnInfo = entity.getColumn(dataIndex);
if (FastType.isNumberType(columnInfo.getLinkInfo().getKeyColumn().getType())) {
column.put("text", column.get("text") + "ID");
column.put("type", "textfield");
}
}
}
columns.removeAll(waitRemove);
Map columnValueMap = new HashMap<>();
String title = getParam("title", true);
HSSFWorkbook workBook = new HSSFWorkbook();
POIHelper poiHelper = new POIHelper(workBook);
HSSFSheet sheet = workBook.createSheet();
//设置默认的行高
sheet.setDefaultRowHeight((short) (30 * 20));
sheet.setDefaultRowHeightInPoints((short) 30);
workBook.setSheetName(0, title);
HSSFCellStyle titleCellStyle = poiHelper.getCellStyle("title");
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont font = workBook.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 14);
titleCellStyle.setFont(font);
int[] rowIndex = new int[]{0};
HSSFRow tableNameRow = sheet.createRow(rowIndex[0]++);
HSSFCell tableNameCell = tableNameRow.createCell(0);
tableNameCell.setCellStyle(titleCellStyle);
tableNameCell.setCellValue(title);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columns.size()));//合并第一行的单元格
createTitleRow(titleCellStyle, rowIndex, sheet, columns);
HSSFDataFormat format = workBook.createDataFormat();
HSSFCellStyle defaultCellStyle = poiHelper.getCellStyle("default");
defaultCellStyle.cloneStyleFrom(titleCellStyle);
defaultCellStyle.setDataFormat(format.getFormat("text"));
HSSFRow titleRow = sheet.getRow(rowIndex[0] - 1);
for (int i = 0; i < columns.size(); i++) {
HSSFCellStyle columnStyle = defaultCellStyle;
Map column = columns.get(i);
String type = String.valueOf(column.get("type"));
HSSFCell cell = titleRow.getCell(i);
if (!columnValueMap.containsKey(i)) {
columnValueMap.put(i, 0);
}
columnValueMap.put(i, cell.getStringCellValue().getBytes().length);
if (column.containsKey("enum") && FastStringUtils.isNotEmpty(String.valueOf(column.get("enum")))) {
POIUtils.setEnumCell(sheet, String.valueOf(column.get("enum")), i, String.valueOf(column.get("text")));
} else if (type.equalsIgnoreCase("numberfield")) {
CellRangeAddressList cellRegions = new CellRangeAddressList(1, Integer.MAX_VALUE, i, i);
DVConstraint constraint = DVConstraint.createNumericConstraint(
DataValidationConstraint.ValidationType.DECIMAL, DataValidationConstraint.OperatorType.BETWEEN, "0", String.valueOf(Integer.MAX_VALUE));
HSSFDataValidation dataValidate = new HSSFDataValidation(cellRegions, constraint);
dataValidate.createErrorBox("输入不合法", "请输入有效的" + column.get("text"));
sheet.addValidationData(dataValidate);
columnStyle = poiHelper.getCellStyle("number");
columnStyle.cloneStyleFrom(defaultCellStyle);
columnStyle.setDataFormat(format.getFormat("0.00"));
}
if (column.containsKey("comment")) {
POIUtils.setCellComment(sheet, cell, String.valueOf(column.get("comment")));
}
sheet.setDefaultColumnStyle(cell.getColumnIndex(), columnStyle);
}
for (Map.Entry integerIntegerEntry : columnValueMap.entrySet()) {
int width = Math.min((integerIntegerEntry.getValue() + 5) * 256, 256 * 256);
sheet.setColumnWidth(integerIntegerEntry.getKey(), width);
}
ExtExcelModelInfo extExcelModelInfo = new ExtExcelModelInfo();
extExcelModelInfo.setTitleRowNum(titleRow.getRowNum());
extExcelModelInfo.setBeginRowNum(sheet.getLastRowNum());
extExcelModelInfo.setColumns(columns);
HSSFSheet extExcelModelInfoSheet = workBook.createSheet(ExtExcelModelInfo.class.getSimpleName());
HSSFCell dataCell = extExcelModelInfoSheet.createRow(0).createCell(0);
dataCell.setCellValue(FastChar.getJson().toJson(extExcelModelInfo));
workBook.setSheetHidden(workBook.getSheetIndex(extExcelModelInfoSheet), true);
String child = "excel/" + title + "_导入数据模板" + ".xls";
File file = new File(FastChar.getConstant().getAttachDirectory(), child);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
workBook.write(fileOutputStream);
fileOutputStream.close();
workBook.close();
responseJson(0, "下载成功!", FastFile.newInstance(file).getUrl());
}
/**
* 导入实体Excel数据
* 参数:
* entityCode 实体编号 {String}
* menu 操作的菜单名称,用作记录日志 {String}
* file excel文件
*/
@AFastLog(value = "导入【${entityDetails}】数据!", type = "数据导入")
public void importData() throws Exception {
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
setRequestAttr("entityDetails", FastChar.getOverrides().newInstance(extEntityClass).getTableDetails());
//公共属性值
Map values = getParamToMap("value");
if (values == null) {
values = new HashMap<>();
}
List> batchSave = new ArrayList<>();
List> batchUpdate = new ArrayList<>();
FastFile> paramFile = getParamFile();
FileInputStream inputStream = new FileInputStream(paramFile.getFile());
Workbook workbook = WorkbookFactory.create(inputStream);
ExtExcelModelInfo extExcelModelInfo = null;
Sheet extExcelModelInfoSheet = workbook.getSheet(ExtExcelModelInfo.class.getSimpleName());
if (extExcelModelInfoSheet != null) {
String configInfo = extExcelModelInfoSheet.getRow(0).getCell(0).getStringCellValue();
extExcelModelInfo = FastChar.getJson().fromJson(configInfo, ExtExcelModelInfo.class);
}
int sheetCount = workbook.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) {
if (workbook.isSheetHidden(i)) {
continue;
}
Sheet sheet = workbook.getSheetAt(i);
String sheetName = sheet.getSheetName();
if (sheetName.equalsIgnoreCase(ExtExcelModelInfo.class.getSimpleName())) {
continue;
}
int titleRowNum = 0;
int beginRowNum = 1;
int rowCount = sheet.getPhysicalNumberOfRows();
List> columns = new ArrayList<>();
//兼容老版本的模板
if (sheetName.contains("@标识码禁止删除@")) {
String onlyCode = sheetName.split("@标识码禁止删除@")[1];
ExtSystemConfigEntity entityExcelModule = ExtSystemConfigEntity.getInstance().selectById(onlyCode);
if (entityExcelModule == null) {
responseJson(-1, "导入失败!数据模板信息不存在,请您重新下载模板!");
continue;
}
columns = FastChar.getJson().fromJson(entityExcelModule.getConfigValue(), new TypeToken>>() {
}.getType());
}
if (extExcelModelInfo != null) {
titleRowNum = extExcelModelInfo.getTitleRowNum();
beginRowNum = extExcelModelInfo.getBeginRowNum() + 1;
columns = extExcelModelInfo.getColumns();
}
Row titleRow = sheet.getRow(titleRowNum);
//标题行
for (int i1 = beginRowNum; i1 < rowCount; i1++) {
Row dataRow = sheet.getRow(i1);
if (dataRow == null) {
continue;
}
if (dataRow.getFirstCellNum() < 0) {
continue;
}
FastEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
setRequestAttr("entityDetails", entity.getTableDetails());
int cellCount = dataRow.getLastCellNum();
for (int i2 = 0; i2 < cellCount; i2++) {
Cell cell = dataRow.getCell(i2);
Object value = POIUtils.getCellValue(workbook, cell);
if (i2 >= columns.size()) {
if (titleRow != null) {
Object cellTitle = POIUtils.getCellValue(workbook, titleRow.getCell(i2));
if (cellTitle != null) {
entity.put(cellTitle.toString(), value);
}
continue;
} else {
break;
}
}
Map column = columns.get(i2);
String type = String.valueOf(column.get("type"));
if (value != null && FastStringUtils.isNotEmpty(String.valueOf(value))) {
if (column.containsKey("enum") && FastStringUtils.isNotEmpty(String.valueOf(column.get("enum")))) {
FastEnumInfo anEnum = FastExtEnumHelper.getEnum(String.valueOf(column.get("enum")), String.valueOf(value));
if (anEnum == null) {
continue;
}
value = anEnum.getId();
} else if (type.equalsIgnoreCase("numberfield")) {
value = FastNumberUtils.formatToNumber(value).floatValue();
}
entity.set(String.valueOf(column.get("dataIndex")), value);
}
}
if (entity.getTable() instanceof FastExtTableInfo) {
FastExtTableInfo extTableInfo = entity.getTable();
if (extTableInfo.isBindSessionLayer()) {
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.put(FastExtEntity.EXTRA_PARENT_LAYER_CODE, managerEntity.getLayerValue());
}
}
if (entity.isEmptyColumn()) {
continue;
}
if (entity.size() > 0) {
entity.putAll(values);
entity.clearEmpty();
int pCount = 0;
Collection> primaries = entity.getPrimaries();
for (FastColumnInfo> primary : primaries) {
if (entity.isNotEmpty(primary.getName())) {
pCount++;
}
}
entity.put("__entityId", FastMD5Utils.MD5To16(FastStringUtils.buildUUID()));
if (pCount == primaries.size() && primaries.size() > 0) {
batchUpdate.add(entity);
} else {
batchSave.add(entity);
}
}
}
}
inputStream.close();
workbook.close();
paramFile.delete();
List> all = new ArrayList<>();
all.addAll(batchSave);
all.addAll(batchUpdate);
IFastImportDataListener iFastImportData = FastChar.getOverrides().newInstance(false, IFastImportDataListener.class);
if (iFastImportData != null) {
FastHandler handler = new FastHandler();
iFastImportData.onBeforeImportData(all, handler);
if (handler.getCode() == -1) {
responseJson(-1, handler.getError());
} else if (handler.getCode() == -2) {
responseJson(0, handler.getError());
}
}
if (batchSave.size() > 0) {
List> removed = new ArrayList<>(batchSave);
removed.removeAll(all);
batchSave.removeAll(removed);
FastChar.getDB().batchSaveEntity(batchSave, 2000);
}
if (batchUpdate.size() > 0) {
List> removed = new ArrayList<>(batchUpdate);
removed.removeAll(all);
batchUpdate.removeAll(removed);
FastChar.getDB().batchUpdateEntity(batchUpdate, 2000);
}
if (iFastImportData != null) {
FastHandler handler = new FastHandler();
iFastImportData.onAfterImportData(all, handler);
if (handler.getCode() == -1) {
responseJson(-1, handler.getError());
} else if (handler.getCode() == -2) {
responseJson(0, handler.getError());
}
}
StringBuilder msg = new StringBuilder();
if (batchSave.size() > 0) {
msg.append("共导入").append(batchSave.size()).append("条数据!");
}
if (batchUpdate.size() > 0) {
msg.append("共更新").append(batchUpdate.size()).append("条数据!");
}
if (msg.length() == 0) {
msg.append("共导入0条数据!");
}
//上传文件强制使用text/html格式返回,避免浏览器弹出json下载,ie
if (!isParamExists(PARAM_ACCPET) && getParam("__browser", "none").equalsIgnoreCase("ie")) {
addParam(PARAM_ACCPET, "text/html");
}
responseJson(0, "导入成功!" + msg);
}
/**
* 下载实体数据
*/
public void downData() throws Exception {
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
setRequestAttr("entityDetails", entity.getTableDetails());
boolean fromTree = getParamToBoolean("fromTree");
Map where = getParamToMap("where");
entity.putAll(where);
if (fromTree) {
String treeParentIdName = getParam("treeParentIdName");
if (FastStringUtils.isNotEmpty(treeParentIdName)) {
entity.remove(treeParentIdName);
}
}
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.pullLayer(managerEntity);
FastPage> fastPage = entity.showList(-1, -1);
List> list = fastPage.getList();
if (list.size() == 0) {
responseJson(-1, "下载失败!暂无下载的数据!");
}
String versionStr = "v1_0";
FastHeadExtInfo version = FastExtConfig.getInstance().getExtInfo("version");
if (version != null) {
versionStr = version.getMapWrap().getString("desc", "v1.0").toLowerCase().replace(".", "_");
}
File dataFile = new File(FastChar.getConstant().getAttachDirectory(), entityCode + "_" + versionStr.toUpperCase() + ".data");
Map data = new HashMap<>();
data.put("entity", extEntityClass.getName());
data.put("data", FastChar.getJson().toJson(list));
byte[] serialize = FastSerializeUtils.serialize(data);
FastFileUtils.writeByteArrayToFile(dataFile, serialize);
responseJson(0, "获取成功!", FastFile.newInstance(dataFile).getUrl());
}
/**
* 上传数据
*/
@SuppressWarnings("unchecked")
public void loadData() throws Exception {
//上传文件强制使用text/html格式返回,避免浏览器弹出json下载,ie
if (!isParamExists(PARAM_ACCPET) && getParam("__browser", "none").equalsIgnoreCase("ie")) {
addParam(PARAM_ACCPET, "text/html");
}
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
Collection> primaries = entity.getPrimaries();
List checkAttr = new ArrayList<>();
for (FastColumnInfo> primary : primaries) {
checkAttr.add(primary.getName());
}
FastFile> paramFile = getParamFile();
byte[] bytes = FastFileUtils.readFileToByteArray(paramFile.getFile());
Map dataMap = (Map) FastSerializeUtils.deserialize(bytes);
if (dataMap != null) {
FastMapWrap fastMapWrap = FastMapWrap.newInstance(dataMap);
if (getParamToBoolean("strict")) {
String entityCacheName = fastMapWrap.getString("entity");
if (!entityCacheName.equals(extEntityClass.getName())) {
responseJson(-1, "上传失败!数据类型不匹配!");
}
}
List> entityList = new ArrayList<>();
String dataJson = fastMapWrap.getString("data");
List> data = FastChar.getJson().fromJson(dataJson, List.class);
for (Object datum : data) {
FastEntity> entityData = FastChar.getOverrides().newInstance(extEntityClass);
entityData.setAll((Map) datum);
entityData.clearEmpty();
if (entityData.isEmptyColumn()) {
continue;
}
for (String key : entityData.allKeys()) {
Object keyValue = entityData.get(key);
if (keyValue instanceof Number) {
continue;
}
if (keyValue instanceof String) {
continue;
}
if (keyValue instanceof byte[]) {
continue;
}
if (keyValue instanceof Date) {
continue;
}
entityData.put(key, FastChar.getJson().toJson(keyValue));
}
entityList.add(entityData);
}
int[] batchSaveResult = FastChar.getDB().batchSaveEntity(entityList, Math.min(entityList.size(), 2000), checkAttr.toArray(new String[]{}));
paramFile.delete();
int count = 0;
for (int i : batchSaveResult) {
if (i > 0) {
count++;
}
}
responseJson(0, "数据上传成功!共" + count + "条数据!");
}
paramFile.delete();
responseJson(-1, "上传失败!数据文件解析失败!");
}
/**
* 更新权限编号
*/
public void updateLayer() {
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtLayerHelper.LayerMap layerMap = entity.getLayerMap();
if (layerMap != null) {
FastExtLayerHelper.updateAllLayerValue(layerMap);
responseJson(0, "更新成功!共更新了" + layerMap.toAllTableNameList().size() + "张关系表格!");
}
responseJson(-1, "更新失败!表格未绑定权限层级!");
}
/**
* 更新相同字段值
*/
public void updateSame() {
String entityCode = getParam("entityCode", true);
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtLayerHelper.LayerMap layerMap = entity.getLayerMap();
if (layerMap != null) {
int sameColumn = FastExtSameHelper.updateSameColumn(layerMap);
responseJson(0, "更新成功!共更新了" + sameColumn + "张关系表格!");
}
responseJson(-1, "更新失败!表格未被其他表格绑定!");
}
/**
* 获取图表echarts的配置json数据
* 参数:
* type 图表类型:0 日图表 1 月图表 2 时段图表
*/
public void echarts() {
String entityCode = getParam("entityCode", true);
String columnDate = getParam("columnDate", true);
int type = getParamToInt("type", 0);
String beginDate = getParam("beginDate", "1912-01-01");
String endDate = getParam("endDate", FastDateUtils.getDateString("yyyy-MM-dd"));
String chartTitle = getParam("chartTitle", "FastChart图表");
String chartType = getParam("chartType", "line");
beginDate = beginDate + " 00:00:00";
endDate = endDate + " 23:59:59";
Class extends FastExtEntity>> extEntityClass = FastExtConfig.getInstance().getExtEntities().getExtEntity(entityCode);
if (extEntityClass == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
FastExtEntity> entity = FastChar.getOverrides().newInstance(extEntityClass);
if (entity == null) {
responseJson(-1, "EntityCode不存在!" + entityCode);
return;
}
setRequestAttr("entityDetails", entity.getTableDetails());
Map where = getParamToMap("where");
entity.putAll(where);
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
entity.pullLayer(managerEntity);
List seriesColumns = new ArrayList<>();
Map selectColumnMap = new LinkedHashMap<>();
List> echarts = getParamToMapList("echarts");
for (Map echart : echarts) {
String column = FastStringUtils.defaultValue(echart.get("property"), "");
String function = FastStringUtils.defaultValue(echart.get("function"), "");
String details = FastStringUtils.defaultValue(echart.get("details"), "");
String nickName = column + FastStringUtils.firstCharToUpper(function);
if (FastStringUtils.isNotEmpty(column) && FastStringUtils.isNotEmpty(function)) {
seriesColumns.add(function + "(" + FastSqlTool.formatAlias(column, "t") + ") as " + nickName);
selectColumnMap.put(nickName, details);
}
}
columnDate = FastSqlTool.formatAlias(columnDate, "t");
String dateSelect = "date_format(" + columnDate + ",'%Y-%m-%d') as reportDate ";
if (type == 1) {//月报表
dateSelect = "date_format(" + columnDate + ",'%Y-%m') as reportDate ";
} else if (type == 2) {//时报表
dateSelect = "date_format(" + columnDate + ",'%H:00') as reportDate ";
} else if (type == 3) {//时分报表
dateSelect = "date_format(" + columnDate + ",'%H:%i') as reportDate ";
} else if (type == 4) {//年报表
dateSelect = "date_format(" + columnDate + ",'%Y') as reportDate ";
}
boolean fromRecycle = entity.getBoolean("^fromRecycle");
String showListSql = managerEntity.getString("Sql" + entityCode + "_" + fromRecycle);
if (FastStringUtils.isEmpty(showListSql)) {
responseJson(-1, "清除失败!请稍后重试!");
}
int fromIndex = showListSql.indexOf("from");
int whereIndex = showListSql.lastIndexOf("where");
String sqlStr = "select " + FastStringUtils.join(seriesColumns, ",") + " , " + dateSelect
+ showListSql.substring(fromIndex, whereIndex);
sqlStr += " where 1=1 and " + columnDate + " >= '" + beginDate + "' " +
" and " + columnDate + " <= '" + endDate + "' " +
" group by reportDate ";
FastSqlInfo sqlInfo = entity.toSelectSql(sqlStr);
try {
List chartBeans = new ArrayList<>();
List extends FastEntity>> list = FastChar.getDB().setDatabase(entity.getDatabase()).select(sqlInfo.getSql(), sqlInfo.toParams());
for (FastEntity> result : list) {
for (Map.Entry stringStringEntry : selectColumnMap.entrySet()) {
FastEChartsBean fastChartBean = new FastEChartsBean();
fastChartBean.setTitle(chartTitle);
fastChartBean.setName(stringStringEntry.getValue());
fastChartBean.setDate(result.getString("reportDate"));
fastChartBean.setValue(result.get(stringStringEntry.getKey()));
if (chartType.equalsIgnoreCase("stack")) {
fastChartBean.setType("bar");
fastChartBean.setStack("true");
} else {
fastChartBean.setType(chartType);
}
chartBeans.add(fastChartBean);
}
}
responseJson(0, "获取成功!", FastEChartsBean.toJsonData(chartTitle, chartBeans));
} catch (Exception e) {
e.printStackTrace();
}
}
}