All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.invms.x.services.basis.ManageService Maven / Gradle / Ivy

The newest version!
package com.invms.x.services.basis;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.invms.x.builders.ArgsBuilder;
import com.invms.x.components.GenerateHandler;
import com.invms.x.entities.IFieldEntity;
import com.invms.x.filters.EntityFilter;
import com.invms.x.filters.FieldFilter;
import com.invms.x.models.EntityModel;
import com.invms.x.services.BaseService;
import com.invms.x.utils.ComUtils;
import com.invms.x.utils.XUtils;

@SuppressWarnings("rawtypes")
@Service
public class ManageService extends BaseService {
	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private ResourceLoader resourceLoader;

	@Autowired
	private GenerateHandler generateHandler;

	@Autowired
	private FieldService fieldService;

	public String buildJs(EntityModel entityModel) throws Exception {
		String entityCode = entityModel.getCode();
		String text = "(function(window, undefined) {\r\n";
		text += "\tvar managePanel = x.managePanel, options = {\r\n";
		text += "\t\tcode : '" + entityCode + "'\r\n";
		text += "\t};\r\n";
		text += "\tmanagePanel.render(options);\r\n";
		text += "})(window);";
		return text;
	}

	public String buildHtml(EntityModel entityModel, boolean entityScript) throws Exception {
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, String[].class);
		String entityCode = entityModel.getCode();
		List fieldModels = entityModel.getFieldModels();
		String webRoot;
		if (entityScript) {
			webRoot = "{webRoot}/dist/js/entity/entity";
		} else {
			webRoot = "{webRoot}/dist/js/" + entityModel.getCodePath() + "/" + entityModel.getCodeLast();
		}
		String text = "
\r\n"; if (entityModel.isHierarchic()) { text += "\t
\r\n"; text += "\t\t
    \r\n"; text += "\t
    \r\n"; } text += "\t
    \r\n"; text += "\t\t
    \r\n"; text += "\t\t\t详情\r\n"; text += "\t\t\t新增\r\n"; text += "\t\t\t修改\r\n"; text += "\t\t\t删除\r\n"; text += "\t\t\t清除\r\n"; text += "\t\t\t\r\n"; text += "\t\t\t导出\r\n"; text += "\t\t\t设置查询列\r\n"; text += "\t\t\t设置列表列\r\n"; text += "\t\t\t保存排序列\r\n"; text += "\t\t\t
    \r\n"; text += "\t\t\t\t清空\r\n"; text += "\t\t\t\t搜索\r\n"; text += "\t\t\t
    \r\n"; text += "\t\t
    \r\n"; text += "\t\t
    \r\n"; for (IFieldEntity item : fieldModels) { if (item.isQueryEnable()) { String itemCode = item.getCode(); String itemName = item.getName(); String itemColumn = item.getColumn(); String dataOptions = "prompt:'" + itemName + "'"; if (!ComUtils.empty(item.getQueryOptions())) { dataOptions += "," + item.getQueryOptions(); } text += "\t\t\t
    \r\n"; switch (item.getDataType()) { case FieldFilter.DATA_TYPE_BOOLEAN: text += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_SHORT: case FieldFilter.DATA_TYPE_INTEGER: case FieldFilter.DATA_TYPE_LONG: text += "\t\t\t\t\r\n"; text += "\t\t\t\t-\r\n"; text += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_FLOAT: case FieldFilter.DATA_TYPE_DOUBLE: text += "\t\t\t\t\r\n"; text += "\t\t\t\t-\r\n"; text += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_DATE: text += "\t\t\t\t\r\n"; text += "\t\t\t\t-\r\n"; text += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_TIME: text += "\t\t\t\t\r\n"; text += "\t\t\t\t-\r\n"; text += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_DATETIME: text += "\t\t\t\t\r\n"; text += "\t\t\t\t-\r\n"; text += "\t\t\t\t\r\n"; break; default: if (!ComUtils.empty(item.getOptionValues())) { List optionValues = objectMapper.readValue(item.getOptionValues(), javaType); text += "\t\t\t\t\r\n"; } else { text += "\t\t\t\t\r\n"; } break; } text += "\t\t\t
    \r\n"; } } text += "\t\t
    \r\n"; if (ComUtils.empty(entityModel.getSortOptions())) { text += "\t\t\r\n"; } else { text += "\t\t
    \r\n"; } text += "\t\t\t\r\n"; text += "\t\t\t\t\r\n"; for (IFieldEntity item : fieldModels) { if (item.isListEnable()) { String itemCode = item.getCode(); String itemName = item.getName(); String itemColumn = item.getColumn(); String dataOptions = "field:'" + itemColumn + "',title:'" + itemName + "'"; dataOptions += ",hidden:" + (item.isListVisible() ? "false" : "true"); dataOptions += ",sortable:" + (item.isSortEnable() ? "true" : "false"); if (!ComUtils.empty(item.getListOptions())) { dataOptions += "," + item.getListOptions(); } if (!ComUtils.empty(item.getSortOptions())) { dataOptions += "," + item.getSortOptions(); } text += "\t\t\t\t\t\r\n"; } } text += "\t\t\t\t\r\n"; text += "\t\t\t\r\n"; text += "\t\t
    \r\n"; text += "\t\t
    \r\n"; text += "\t
    \r\n"; text += "
    \r\n"; text += ""; return text; } public String buildDetailHtml(EntityModel entityModel) throws Exception { List fieldModels = entityModel.getFieldModels(); String apiRoot = "{apiRoot}/entity/" + entityModel.getCodePath(); String text = "
    \r\n"; text += "\t
    \r\n"; text += "\t\t返回\r\n"; text += "\t\t重置\r\n"; text += "\t
    \r\n"; text += "\t
    \r\n"; List itemIndexes = new LinkedList(); Map textCategories = new HashMap(); for (IFieldEntity item : fieldModels) { if (item.isDetailEnable()) { String itemCode = item.getCode(); String itemName = item.getName(); String itemColumn = item.getColumn(); String itemCategory = item.getCategory(); String dataOptions = "readonly:true,editable:false"; if (!ComUtils.empty(item.getDetailOptions())) { dataOptions += "," + item.getDetailOptions(); } String subText = textCategories.get(itemCategory); if (subText == null) { if (ComUtils.empty(item.getForeignCode())) { subText = "\t\t
    \r\n"; } else { subText = "\t\t
    \r\n"; } itemIndexes.add(itemCategory); } if (ComUtils.equal(item.getDataType(), FieldFilter.DATA_TYPE_FOREIGN)) { subText += "\t\t\t\r\n"; if (!ComUtils.empty(item.getForeignOptions())) { dataOptions += ',' + item.getForeignOptions(); } if (!dataOptions.contains("url:")) { dataOptions += ",url:'" + apiRoot + "/simple'"; } String foreignName = itemColumn.substring(0, itemColumn.length() - 2) + "Name"; subText += "\t\t\t
      \r\n"; } else { subText += "\t\t\t
      \r\n"; switch (item.getDataType()) { case FieldFilter.DATA_TYPE_BOOLEAN: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_MULTISTRING: subText += "\t\t\t\t\r\n"; break; default: subText += "\t\t\t\t\r\n"; break; } subText += "\t\t\t
      \r\n"; } textCategories.put(itemCategory, subText); } } for (String item : itemIndexes) { text += textCategories.get(item); text += "\t\t
      \r\n"; } text += "\t\r\n"; text += "
      "; return text; } public String buildInsertHtml(EntityModel entityModel) throws Exception { JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, String[].class); List fieldModels = entityModel.getFieldModels(); String apiRoot = "{apiRoot}/entity/" + entityModel.getCodePath(); String text = "
      \r\n"; text += "\t
      \r\n"; text += "\t\t返回\r\n"; text += "\t\t重置\r\n"; text += "\t\t
      \r\n"; text += "\t\t\t新增\r\n"; text += "\t\t
      \r\n"; text += "\t
      \r\n"; text += "\t
      \r\n"; List itemIndexes = new LinkedList(); Map textCategories = new HashMap(); for (IFieldEntity item : fieldModels) { if (item.isInsertEnable()) { String itemCode = item.getCode(); String itemName = item.getName(); String itemColumn = item.getColumn(); String itemCategory = item.getCategory(); String dataOptions = "prompt:'" + itemName + "'"; if (item.isRequired()) { dataOptions += ",required:true"; } if (item.isReadOnly()) { dataOptions += ",readonly:true,editable:false"; } if (!ComUtils.empty(item.getInsertOptions())) { dataOptions += "," + item.getInsertOptions(); } String subText = textCategories.get(itemCategory); if (subText == null) { if (ComUtils.empty(item.getForeignCode())) { subText = "\t\t
      \r\n"; } else { subText = "\t\t
      \r\n"; } itemIndexes.add(itemCategory); } if (ComUtils.equal(item.getDataType(), FieldFilter.DATA_TYPE_FOREIGN)) { subText += "\t\t\t\r\n"; if (!ComUtils.empty(item.getForeignOptions())) { dataOptions += ',' + item.getForeignOptions(); } if (!dataOptions.contains("url:")) { dataOptions += ",url:'" + apiRoot + "/simple'"; } String foreignName = itemColumn.substring(0, itemColumn.length() - 2) + "Name"; subText += "\t\t\t
        \r\n"; } else { subText += "\t\t\t
        \r\n"; switch (item.getDataType()) { case FieldFilter.DATA_TYPE_BOOLEAN: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_SHORT: case FieldFilter.DATA_TYPE_INTEGER: case FieldFilter.DATA_TYPE_LONG: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_FLOAT: case FieldFilter.DATA_TYPE_DOUBLE: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_DATE: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_TIME: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_DATETIME: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_MULTISTRING: subText += "\t\t\t\t\r\n"; break; default: if (!ComUtils.empty(item.getOptionValues())) { List optionValues = objectMapper.readValue(item.getOptionValues(), javaType); subText += "\t\t\t\t\r\n"; } else { subText += "\t\t\t\t\r\n"; } break; } subText += "\t\t\t
        \r\n"; } textCategories.put(itemCategory, subText); } } for (String item : itemIndexes) { text += textCategories.get(item); text += "\t\t
        \r\n"; } text += "\t\r\n"; text += "
        "; return text; } public String buildUpdateHtml(EntityModel entityModel) throws Exception { JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, String[].class); List fieldModels = entityModel.getFieldModels(); String apiRoot = "{apiRoot}/entity/" + entityModel.getCodePath(); String text = "
        \r\n"; text += "\t
        \r\n"; text += "\t\t返回\r\n"; text += "\t\t重置\r\n"; text += "\t\t
        \r\n"; text += "\t\t\t修改\r\n"; text += "\t\t
        \r\n"; text += "\t
        \r\n"; text += "\t
        \r\n"; List itemIndexes = new LinkedList(); Map textCategories = new HashMap(); for (IFieldEntity item : fieldModels) { if (item.isUpdateEnable()) { String itemCode = item.getCode(); String itemName = item.getName(); String itemColumn = item.getColumn(); String itemCategory = item.getCategory(); String dataOptions = "prompt:'" + itemName + "'"; if (item.isRequired()) { dataOptions += ",required:true"; } if (item.isReadOnly()) { dataOptions += ",readonly:true,editable:false"; } if (!ComUtils.empty(item.getUpdateOptions())) { dataOptions += "," + item.getUpdateOptions(); } String subText = textCategories.get(itemCategory); if (subText == null) { if (ComUtils.empty(item.getForeignCode())) { subText = "\t\t
        \r\n"; } else { subText = "\t\t
        \r\n"; } itemIndexes.add(itemCategory); } if (ComUtils.equal(item.getDataType(), FieldFilter.DATA_TYPE_FOREIGN)) { subText += "\t\t\t\r\n"; if (!ComUtils.empty(item.getForeignOptions())) { dataOptions += ',' + item.getForeignOptions(); } if (!dataOptions.contains("url:")) { dataOptions += ",url:'" + apiRoot + "/simple'"; } String foreignName = itemColumn.substring(0, itemColumn.length() - 2) + "Name"; subText += "\t\t\t
          \r\n"; } else { subText += "\t\t\t
          \r\n"; switch (item.getDataType()) { case FieldFilter.DATA_TYPE_BOOLEAN: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_SHORT: case FieldFilter.DATA_TYPE_INTEGER: case FieldFilter.DATA_TYPE_LONG: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_FLOAT: case FieldFilter.DATA_TYPE_DOUBLE: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_DATE: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_TIME: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_DATETIME: subText += "\t\t\t\t\r\n"; break; case FieldFilter.DATA_TYPE_MULTISTRING: subText += "\t\t\t\t\r\n"; break; default: if (!ComUtils.empty(item.getOptionValues())) { List optionValues = objectMapper.readValue(item.getOptionValues(), javaType); subText += "\t\t\t\t\r\n"; } else { subText += "\t\t\t\t\r\n"; } break; } subText += "\t\t\t
          \r\n"; } textCategories.put(itemCategory, subText); } } for (String item : itemIndexes) { text += textCategories.get(item); text += "\t\t
          \r\n"; } text += "\t\r\n"; text += "
          "; return text; } public Object generate(ArgsBuilder argsBuilder) throws Exception { Map> result = new HashMap>(); Map, EntityFilter> classes = XUtils.scanClasses(EntityFilter.class, resourceLoader, null); String root = ComUtils.getResourceRoot(generateHandler); for (Class item : classes.keySet()) { List fieldEntities = fieldService.getEntityFields(item, argsBuilder); if (fieldEntities != null && fieldEntities.size() > 0) { EntityModel entityModel = EntityModel.createModel(item, classes.get(item), fieldEntities); String codePath = entityModel.getCodePath(); String codeLast = entityModel.getCodeLast(); List list = new LinkedList(); list.add(ComUtils.writeFile(Paths.get(root, "js", codePath, codeLast + ".manage.js"), buildJs(entityModel))); list.add(ComUtils.writeFile(Paths.get(root, "htm", codePath, codeLast + ".manage.html"), buildHtml(entityModel, false))); list.add(ComUtils.writeFile(Paths.get(root, "htm", codePath, codeLast + ".manage.detail.html"), buildDetailHtml(entityModel))); list.add(ComUtils.writeFile(Paths.get(root, "htm", codePath, codeLast + ".manage.insert.html"), buildInsertHtml(entityModel))); list.add(ComUtils.writeFile(Paths.get(root, "htm", codePath, codeLast + ".manage.update.html"), buildUpdateHtml(entityModel))); result.put(item.getName(), list); } } return result; } }




          © 2015 - 2025 Weber Informatics LLC | Privacy Policy