Commit 2deeb9f9 authored by 覃振观's avatar 覃振观 👶

23-12-11: Test 下班前提交代码 JS 保留对象关系。

parent 93c573c3
var rawData={};
$(document).ready(function() {
initializeUI();
initializeToobarContainer();
......@@ -26,76 +25,6 @@ function initializeUI() {
});
}
function loadDataTest() {
console.log("loadData")
var params = new URLSearchParams(document.location.search);
var paramsData = JSON.parse(params.get("data"));
Public.ajax(web_app.name + "/product/loadProductDetail.ajax", {
productId: paramsData.productId
}, function (data) {
rawData = data;
$('#submitForm').formSet(data);
var fronData = {};
fronData.Rows = data.fronColors;
faceGrid("#frontFaceGrid", fronData);
fronData.Rows = data.backColors;
faceGrid("#backFaceGrid", fronData);
inputEventListener();
});
}
// 数据映射配置
var dataMapping = {
"productDetail": [
"factoryName"
, { "productLayout": "productLayout" }
],
"ProductLooked": [
"productLength"
, "productWidth"
]
}
// 构建映射关系。
function mapping(dataMapping, rawData) {
let entitySet = Object.entries(dataMapping);
entitySet.tryForEach((entity, index) => {
let beanName = entity[0];
let propertyNames = entity[1];
let entityData = isExist(rawData, beanName) ? rawData[beanName] : throw {};
propertyNames.tryForEach((item, index) => {
if(typeof item === "string") {
let value = isExist(entityData, item, item) ? entityData[item] : throw {};
dataMapping.item = beanName;
console.log(item);
return;
}
if(item instanceof Object) {
let entry = Object.entries(item);
let property = entry[0];
let value = isExist(entityData, property, property) ? entityData[property] : throw {};
let alias = entry[1];
dataMapping.alias = { beanName: property };
}
})
})
}
Array.prototype.tryForEach = function (callback, thisArg) {
try {
this.forEach(callback, thisArg);
} catch { }
}
function isExist(obj, property) { this.isExist(bj, property, 'rawData') }
function isExist(obj, property, objName) {
if(!obj[property]) {
console.error('No data for name of ' + entity[0] + ' in ' + objName + ' !');
return false;
}
return true;
}
function faceGrid(elId, data) {
var toolbarOptions = UICtrl.getDefaultToolbarOptions({
updateHandler: function(){
......@@ -152,38 +81,161 @@ function initializeToobarContainer(){
$('#toolBar').data('dropup',true).removeClass('job-button-fixed-top').addClass('job-button-fixed-bottom');
}
function runObserver() {
observer.observe(document.querySelector('#panelContainer'), mutationConfig);
function loadDataTest() {
console.log("loadData")
var params = new URLSearchParams(document.location.search);
Public.ajax(web_app.name + "/product/loadProductDetail.ajax", {
data: params.get("data")
}, function (data) {
var showData = mapping(dataMapping, data.rawData);
$('#submitForm').formSet(showData);
var fronData = {};
fronData.Rows = data.fronColors;
faceGrid("#frontFaceGrid", fronData);
fronData.Rows = data.backColors;
faceGrid("#backFaceGrid", fronData);
inputEventListener();
});
}
// 数据映射配置
var dataMapping = {
Product: [
"productId"
, "brandName"
, "productUnit"
, "productCategoryId"
, "productSampleCode"
],
ProductDetail: [
"factoryName"
, { "productLayout": "productLayout" }
],
ProductLooked: [
"productLength"
, "productWidth"
],
// ProductLoss: [
// "customerName"
// ],
}
// 补充方案 监听 Value 属性
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if(mutation.type === 'attributes'&& mutation.attributeName === 'value') {
console.log('属性变化节点 : ' + mutation.target);
console.log('变化属性名 : ' + mutation.attributeName);
console.log('变化属性值 : ' + mutation.target.value);
/**
* 遍历映射配置,构建映射关系
* @param dataMapping 映射配置
* @param rawData 源数据
*/
function mapping(dataMapping, rawData) {
let mapper = {};
delete dataMapping.Mapper;
let entitySet = Object.entries(dataMapping);
let returnData = {};
entitySet.tryForEach((entity, index) => {
let beanName = entity[0];
if(!isExist(rawData, beanName)) { throw {}; }
let entityData = rawData[beanName];
let propertyNames = entity[1];
propertyNames.tryForEach((item, index) => {
if(typeof item === "string") {
if(!isExist(entityData, item, beanName)) { throw {}; }
let value = entityData[item];
mapper[item] = beanName;
if(item === "factoryName") {
console.log('1')
}
})
})
const mutationConfig = { attributes: true, childList: true, subtree: true}
returnData[item] = value;
console.log(item);
return;
} else if(item instanceof Object) {
let entry = Object.entries(item)[0];
let property = entry[0];
if(!isExist(entityData, property, beanName)) { throw {}; }
let value = entityData[property];
let alias = entry[1];
mapper[alias] = {};
mapper[alias][beanName] = property ;
returnData[alias] = value;
} else {
console.error('The configuration of "'+item+'" in "'+beanName+'" is not recognized, and the required,' +
' parameters are ( "propertyName" or "{ propertyName: alias }" ).');
throw {};
}
});
});
dataMapping.Mapper = mapper;
// ---------------- 暂不处理无法执行到此处的情况 ----------------
// 这里写死 映射对象为:‘dataMapping’
window.dataMapping = dataMapping;
// 这里写死 数据集:‘dataSet: { rawData: {}, newData: {} }’
window.dataSet = {};
window.dataSet.rawData = rawData;
window.dataSet.newData = JSON.parse(JSON.stringify(dataSet.rawData));
return returnData;
}
Array.prototype.tryForEach = function (callback, thisArg) {
try {
this.forEach(callback, thisArg);
} catch { }
}
/**
* 检测数据中是否存在对应属性
* @param obj 需要检测数据
* @param property 需要检测的属性
* @param objName 检测的类名
* @returns {boolean}
*/
function isExist(obj, property, objName) {
if(!objName) { objName = 'rawData'; }
// 这里注意,后端返回的类名是否与映射配置一致
if(!obj[property]) {
console.error('No data for name of ' + property + ' in ' + objName + ' !');
return false;
}
return true;
}
// 监听 input 管理页面显示的 Data,提交数据时,只提交修改过的 Data
function inputEventListener() {
const inputElements = document.querySelectorAll('input');
inputElements.forEach(item => {
item.addEventListener('input', (event) => {
let mapper = window.dataMapping.Mapper;
let property = event.target.getAttribute('name');
let beanMapping = mapper[property];
if(!beanMapping) {
console.error("The configuration was not found in the 'dataMapping' : '"+property+"'")
}
let newData = window.dataSet.newData;
newData[beanMapping][property] = event.target.value;
console.log('Input value changed:', event.target.value);
});
});
}
// 用于判断字符串是否更改。
function getHashCode(str) {
getHashCode(getHashCode, true);
function checkUpdata() {
let rawData = window.dataSet.rawData;
let newData = window.dataSet.newData;
window.dataSet.upData = {};
let rawEntry = Object.entries(rawData);
rawEntry.forEach(raw => {
let beanName = raw[0];
let rawHash = getHashCode(JSON.stringify(rawData[beanName]));
let newHash = getHashCode(JSON.stringify(newData[beanName]));
if(!(rawHash === newHash)) {
window.dataSet.upData[beanName] = newData[beanName];
}
})
}
/**
* 用于判断字符串是否更改
* @param str
* @param caseSensitive 是否区分大小写 Default: true
* @returns {number}
*/
function getHashCode (str, caseSensitive) {
if(caseSensitive === undefined) { caseSensitive = true;}
if(!caseSensitive) {
str = str.toLowerCase();
}
......@@ -198,12 +250,28 @@ function getHashCode (str, caseSensitive) {
var hash3 = hash2 ^ (hash2 << 1) ^ (hash2 >> 13);
if (len < 50) {
var hash4 = this.getHashCode(str + str + str, caseSensitive);
return (hash ^ hash2 ^ hash3 ^ hash4) & 0x7FFFFFFF ;
return (hash ^ hash2 ^ hash3 ^ hash4) & 0x7FFFFFFF;
} else {
return (hash ^ hash2 ^ hash3 >> 3 ) & 0x7FFFFFFF;
}
}
function runObserver() {
observer.observe(document.querySelector('#panelContainer'), mutationConfig);
}
// 补充方案 监听 Value 属性
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if(mutation.type === 'attributes'&& mutation.attributeName === 'value') {
console.log('属性变化节点 : ' + mutation.target);
console.log('变化属性名 : ' + mutation.attributeName);
console.log('变化属性值 : ' + mutation.target.value);
}
})
})
const mutationConfig = { attributes: true, childList: true, subtree: true}
// if(!isExist(rawData, entity[0])) {
// throw {};
// }
......
package com.huigou.topsun.product.application;
import com.huigou.topsun.product.domain.Product;
import org.springframework.data.domain.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
......@@ -27,12 +25,12 @@ public interface ProductApplication {
/**
* description 获取产品所有信息
* @param productId 产品ID
* @param product 产品
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author qinzhenguan
* @createDate 2023/11/29 9:33
*/
Map<String, Object> queryDetailAll(int productId);
Map<String, Object> queryDetailAll(Product product);
/**
* description 对比所有对象,并保存更改的数据。
......
......@@ -63,9 +63,9 @@ public class ProductApplicationImpl implements ProductApplication {
* createDate 2023/11/29 13:19
*/
@Override
public Map<String, Object> queryDetailAll(int productId) {
public Map<String, Object> queryDetailAll(Product product) {
Map<String, Object> resultMap = new HashMap<>(200);
String strProductId = Long.toString(productId);
String strProductId = product.getProductId();
ProductDetail productDetail = detailRepository.findByProductId(strProductId);
// EntityManager manager = JPAUtils.getEntityManger();
// manager.getTransaction().begin();
......@@ -87,10 +87,10 @@ public class ProductApplicationImpl implements ProductApplication {
entitys.add(lookedRepository.findByProductId(strProductId));
entitys.add(publishedConfRepository.findByProductId(strProductId));
entitys.add(productDetail);
entitys.add(product);
HashMap<String, Object> listMaps = new HashMap<>(100);
resultMap.putAll(assembleResultOfForm(entitys));
resultMap.put("rawData", assembleResultOfForm(entitys));
listMaps.put("loss", loss);
listMaps.put("material", material);
......@@ -147,13 +147,15 @@ public class ProductApplicationImpl implements ProductApplication {
public Map<String, Object> assembleResultOfForm(Set<Object> entitys) {
Map<String, Object> map = new HashMap<>(1024);
ObjectMapper mapper = new ObjectMapper();
map.put("entitys", entitys);
for(Object item : entitys) {
if(item == null) {
continue;
}
map.putAll(mapper.convertValue(item, new TypeReference<Map<? extends String, ?>>() {}));
map.put(item.getClass().getSimpleName(), mapper.convertValue(item,
new TypeReference<Map<? extends String, ?>>() {}) );
// 这里要获取所有的属性名, 保存对应关系, 属性: 所属实体类;
//map.putAll(mapper.convertValue(item, new TypeReference<Map<? extends String, ?>>() {}));
}
return map;
}
......
package com.huigou.topsun.product.controller;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huigou.cache.DictUtil;
import com.huigou.topsun.product.application.ProductApplication;
import com.huigou.topsun.product.domain.Product;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.client.CommonController;
import com.huigou.util.SDO;
......@@ -46,13 +49,6 @@ public class ProductController extends CommonController {
return toResult(map);
}
public String queryDetailAll() {
SDO sdo = this.getSDO();
String productId = sdo.getString("id");
Map<String, Object> map = productService.queryDetailAll(Integer.parseInt(productId));
return toResult(map);
}
public String forwardProduct() {
// 通过此方法可以查看系统配置的 字典 的数据结构
DictUtil.getDictionaryList("technologyType");
......@@ -77,9 +73,17 @@ public class ProductController extends CommonController {
}
public String loadProductDetail() {
ObjectMapper mapper = new ObjectMapper();
SDO sdo = this.getSDO();
String productId = sdo.getString("productId");
Map<String, Object> resultMap = productService.queryDetailAll(Integer.parseInt(productId));
Product product ;
try {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
product = mapper.readValue(sdo.getString("data") + "", Product.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Map<String, Object> resultMap = productService.queryDetailAll(product);
return toResult(resultMap);
}
......
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