net.mingsoft.mdiy.biz.impl.ModelDataImpl Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2012-present 铭软科技(mingsoft.net)
* 本软件及相关文档文件(以下简称“软件”)的版权归 铭软科技 所有
* 遵循 铭软科技《服务协议》中的《保密条款》
*/
package net.mingsoft.mdiy.biz.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.dao.IBaseDao;
import net.mingsoft.base.util.SqlInjectionUtil;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mdiy.biz.IModelDataBiz;
import net.mingsoft.mdiy.dao.IModelDao;
import net.mingsoft.mdiy.entity.ModelEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 自定义表单接口实现类
* @author 铭软
* @version
* 版本号:100-000-000
* 创建日期:2012-03-15
* 历史修订:2022-1-21 queryDiyFormData() orderby非法参数
*/
@Service()
public class ModelDataImpl extends BaseBizImpl implements IModelDataBiz {
@Autowired
private net.mingsoft.mdiy.biz.IModelBiz modelBiz;
/**
* 注入自定义表单持久化层
*/
@Autowired
private IModelDao modelDao;
/**
* 获取类别持久化层
* @return diyFormDao 返回类别持久话层
*/
@Override
protected IBaseDao getDao() {
return modelDao;
}
@Override
public boolean saveDiyFormData(String modelId, Map params) {
ModelEntity model = modelBiz.getById(modelId);
return saveDiyFormData(model,params);
}
@Override
public boolean saveDiyFormData(ModelEntity model, Map params) {
if (ObjectUtil.isNotNull(model) ) {
//设置区分大小写的Map,因为代码生成器存在版本问题,有些老数据是大小写没统一
Map fieldMap = new CaseInsensitiveMap<>(model.getFieldMap());
//保存数据的key-value值
HashMap fields = new HashMap<>();
for (String paramKey : params.keySet()) {
//判断是否存在此字段
if (fieldMap.containsKey(paramKey)) {
try {
DateTime dateTime = DateUtil.parseDateTime(params.get(paramKey).toString());
fields.put(fieldMap.get(paramKey).toString(), dateTime);
continue;
} catch (Exception e) {
// 不是时间类型
}
fields.put(fieldMap.get(paramKey).toString(), params.get(paramKey));
}
}
fields.put("CREATE_DATE", new Date());
fields.put("UPDATE_DATE", new Date());
// TODO: 2023/11/15 idType为0是雪花id
if (model.getModelIdType()==0){
Snowflake snowflake = IdUtil.getSnowflake();
fields.put("ID",snowflake.nextId());
}
if (BasicUtil.getManager()!=null){
// 前台提交做容错
fields.put("CREATE_BY", BasicUtil.getManager().getId());
}
modelBiz.insertBySQL(model.getModelTableName(), fields);
return true;
}else {
return false;
}
}
@Override
public boolean updateDiyFormData(String modelId, Map params) {
ModelEntity model = modelBiz.getById(modelId);
return updateDiyFormData(model,params);
}
@Override
public boolean updateDiyFormData(ModelEntity model, Map params) {
if (ObjectUtil.isNotNull(model) ) {
Map fieldMap = model.getFieldMap();
fieldMap = new CaseInsensitiveMap<>(fieldMap);
HashMap fields = new HashMap<>();
//拼接字段
for (String s : params.keySet()) {
//判断是否存在此字段
if (fieldMap.containsKey(s)) {
fields.put(fieldMap.get(s).toString(), params.get(s));
}
}
if (StringUtils.isEmpty(params.get("id").toString())) {
LOG.debug("请求数据不含主键id,无法更新");
return false;
}
fields.put("UPDATE_DATE", new Date());
fields.put("UPDATE_BY", new Date());
Map where = new HashMap<>();
where.put("id",params.get("id").toString());
modelBiz.updateBySQL(model.getModelTableName(), fields,where);
return true;
}else {
return false;
}
}
@Override
public List queryDiyFormData(String modelId, Map params) {
ModelEntity model = modelBiz.getById(modelId);
if (ObjectUtil.isNotNull(model) ) {
Map fieldMap = model.getFieldMap();
HashMap fields = new HashMap<>();
//拼接字段
for (String s : params.keySet()) {
//判断是否存在此字段
if (fieldMap.containsKey(s)) {
fields.put(fieldMap.get(s).toString(), params.get(s));
}
}
List