提交 5cb44fc2 编写于 作者: chenxiang@lab.ibiz5.com's avatar chenxiang@lab.ibiz5.com

NoSQL:MongoDB适配

上级 95c177d3
package {{packageName}}.core.{{entity.module}}.domain;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.data.mongodb.core.mapping.MongoId;
import org.springframework.util.ObjectUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.Assert;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.util.annotation.DEField;
import cn.ibizlab.util.enums.DEPredefinedFieldType;
import cn.ibizlab.util.enums.DEFieldDefaultValueType;
import cn.ibizlab.util.helper.DataObject;
import cn.ibizlab.util.annotation.Audit;
import cn.ibizlab.util.enums.DupCheck;
import cn.ibizlab.util.domain.EntityMP;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Transient;
import org.springframework.data.annotation.Id;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
{{#entity.relEntities}}
import {{packageName}}.core.{{module}}.domain.{{codeName}};
{{/entity.relEntities}}
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
@Document(collection = "{{entity.codeName}}")
@ApiModel(value = "{{entity.name}}", description = "{{entity.logicName}}")
public class {{entity.codeName}} extends EntityMP implements Serializable
{
{{#each entity.fields}}
{{#unless deepStructure}}
/**
* {{logicName}}
*/
{{#if keyDEField}}
@Id
{{else}}
{{/if}}
{{#enableAudit}}
@Audit
{{/enableAudit}}
@DEField({{annotation}})
@JsonProperty("{{jsonName}}")
{{#timeType}}
@JsonFormat(pattern = "{{format}}", locale = "zh", timezone = "GMT+8")
{{/timeType}}
@JSONField(name = "{{jsonName}}"{{#timeType}} , format = "{{format}}"{{/timeType}})
{{#eq type.java "Long"}}
@JsonSerialize(using = ToStringSerializer.class)
{{/eq}}
@ApiModelProperty(value = "{{columnName}}", notes = "{{logicName}}")
private {{type.java}} {{camelCase codeName}};
{{/unless}}
{{/each}}
{{#each entity.references}}
/**
* {{entityLogicName}}
*/
@JsonIgnore
@JSONField(serialize = false)
@ApiModelProperty(value = "{{lowerCase codeName}}", notes = "{{logicName}}")
private {{entityCodeName}} {{camelCase codeName}};
{{/each}}
{{#each entity.nesteds}}
/**
* {{entityLogicName}}
*/
{{#if listCode}}
@JSONField(name = "{{listCode}}")
@JsonProperty("{{listCode}}")
{{else}}
@JsonIgnore
@JSONField(serialize = false)
{{/if}}
{{#if columnName}}
@ApiModelProperty(value = "{{columnName}}", notes = "{{logicName}}")
{{else}}
@ApiModelProperty(value = "{{lowerCase codeName}}", notes = "{{logicName}}")
{{/if}}
protected List<{{entityCodeName}}> {{camelCase codeName}};
{{/each}}
{{#each entity.fields}}
{{#unless deepStructure}}
{{#unless keyDEField}}
{{#unless predefinedType}}
/**
* 设置 [{{logicName}}]
*/
public void set{{pascalCase codeName}}({{type.java}} {{camelCase codeName}}) {
this.{{camelCase codeName}} = {{camelCase codeName}};
this.modify("{{jsonName}}", {{camelCase codeName}});
}
{{/unless}}
{{/unless}}
{{/unless}}
{{/each}}
{{#entity.nesteds}}
{{#columnName}}
/**
* 设置 [{{entityLogicName}}]
*/
public void set{{pascalCase codeName}}(List<{{entityCodeName}}> {{camelCase codeName}}) {
this.{{camelCase codeName}} = {{camelCase codeName}};
this.modify("{{listCode}}", ({{camelCase codeName}}!=null)?{{camelCase codeName}}:(new ArrayList()));
}
{{/columnName}}
{{/entity.nesteds}}
{{#if entity.keyField.type.number}}
@Override
public Serializable getDefaultKey(boolean gen) {
return IdWorker.getId();
}
{{else}}
{{#entity.unionKeyMode}}
{{#unless entity.keyField.phisicalDEField}}
/**
* 设置 [{{logicName}}]
*/
public void set{{pascalCase entity.keyField.codeName}}({{entity.keyField.type.java}} {{camelCase entity.keyField.codeName}}) {
this.{{camelCase entity.keyField.codeName}} = {{camelCase entity.keyField.codeName}};
if(!ObjectUtils.isEmpty({{camelCase entity.keyField.codeName}})) {
String [] args={{camelCase entity.keyField.codeName}}.split("\\|\\|");
{{#each entity.unionKeyFields}}
this.set("{{camelCase codeName}}", args[{{@index}}]);
{{/each}}
}
}
public {{entity.keyField.type.java}} get{{pascalCase entity.keyField.codeName}}() {
if(ObjectUtils.isEmpty(this.{{camelCase entity.keyField.codeName}}))
this.{{camelCase entity.keyField.codeName}} = ({{entity.keyField.type.java}})getDefaultKey(true);
return this.{{camelCase entity.keyField.codeName}};
}
{{/unless}}
@Override
public Serializable getDefaultKey(boolean gen) {
{{#each entity.unionKeyFields}}
Assert.notNull(get{{pascalCase codeName}}(),"未设置{{logicName}}");
{{/each}}
String key = String.format("{{#each entity.unionKeyFields}}{{#unless @first}}||{{/unless}}%s{{/each}}"
{{#each entity.unionKeyFields}},{{#if timeType}}DataObject.datetimeFormat.format(get{{pascalCase codeName}}()){{else}}get{{pascalCase codeName}}(){{/if}}{{/each}});
{{#if entity.keyField.phisicalDEField}}
key = DigestUtils.md5DigestAsHex(key.getBytes());
{{/if}}
return key;
}
{{/entity.unionKeyMode}}
{{/if}}
{{#if entity.hasResetField}}
/**
* 复制当前对象数据到目标对象(粘贴重置)
* @param targetEntity 目标数据对象
* @param bIncEmpty 是否包括空值
* @param <T>
* @return
*/
@Override
public <T> T copyTo(T targetEntity, boolean bIncEmpty) {
{{#each entity.fields}}
{{#if pasteReset}}
this.reset("{{lowerCase name}}");
{{/if}}
{{/each}}
return super.copyTo(targetEntity, bIncEmpty);
}
{{/if}}
}
\ No newline at end of file
package {{packageName}}.core.{{entity.module}}.filter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import cn.ibizlab.util.filter.MongoQueryContext;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
{{#entity.enableES}}
import org.elasticsearch.index.query.QueryBuilders;
{{/entity.enableES}}
import {{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}};
/**
* 关系型数据实体[{{entity.codeName}}] 查询条件对象
*/
@Slf4j
@Getter
@Setter
@NoArgsConstructor
@ApiModel("{{entity.logicName}}")
public class {{entity.codeName}}SearchContext extends MongoQueryContext {
{{#each entity.fields as |field|}}
{{#each field.allPSDEFSearchModes as |formitem|}}
{{#unless formitem.valueFunc}}
{{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL"))}}
@JsonProperty("{{lowerCase formitem.name}}")
@JSONField(name = "{{lowerCase formitem.name}}")
{{else}}
@JsonProperty("{{lowerCase formitem.name}}")
{{#timeType}}
@JsonFormat(pattern = "{{format}}", locale = "zh", timezone = "GMT+8")
{{/timeType}}
@JSONField(name = "{{lowerCase formitem.name}}"{{#timeType}} , format = "{{format}}"{{/timeType}})
{{#eq type.java "Long"}}
@JsonSerialize(using = ToStringSerializer.class)
{{/eq}}
{{/if}}
@ApiModelProperty("{{field.logicName}}{{formitem.valueOP}}")
private {{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL"))}}String{{else}}{{field.type.java}}{{/if}} {{camelCase field.codeName}}{{formitem.valueOP}};
public {{entity.codeName}}SearchContext set{{pascalCase field.codeName}}{{formitem.valueOP}}({{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL"))}}String{{else}}{{field.type.java}}{{/if}} {{camelCase field.codeName}}{{formitem.valueOP}}) {
this.{{camelCase field.codeName}}{{formitem.valueOP}} = {{camelCase field.codeName}}{{formitem.valueOP}};
if(!ObjectUtils.isEmpty(this.{{camelCase field.codeName}}{{formitem.valueOP}})){
{{#eq formitem.valueOP "LIKE"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").regex({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "LEFTLIKE"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").regex({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "RIGHTLIKE"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").regex({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "EQ"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").is({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "NOTEQ"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").ne({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "GT"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").gt({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "GTANDEQ"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").gte({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "LT"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").lt({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "LTANDEQ"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").lte({{camelCase field.codeName}}{{formitem.valueOP}}));
{{/eq}}
{{#eq formitem.valueOP "ISNOTNULL"}}
if(this.{{camelCase field.codeName}}{{formitem.valueOP}}.equals("1")){
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").ne(null));
}
{{/eq}}
{{#eq formitem.valueOP "ISNULL"}}
if(this.{{camelCase field.codeName}}{{formitem.valueOP}}.equals("1")){
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").is(null));
}
{{/eq}}
{{#eq formitem.valueOP "IN"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").in({{camelCase field.codeName}}{{formitem.valueOP}}.split(";|,|;|,")));
{{/eq}}
{{#eq formitem.valueOP "NOTIN"}}
this.getSearchCond().addCriteria(Criteria.where("{{lowerCase field.name}}").nin({{camelCase field.codeName}}{{formitem.valueOP}}.split(";|,|;|,")));
{{/eq}}
}
return this;
}
{{/unless}}
{{/each}}
{{/each}}
/**
* 启用快速搜索
*/
public void setQuery(String query)
{
this.query=query;
if(!StringUtils.isEmpty(query)){
Criteria criteria = new Criteria();
criteria.orOperator(
{{#each entity.quickSearchFields as |field|}}
{{#if @first}}
Criteria.where("{{lowerCase field.name}}").regex(query)
{{else}}
, Criteria.where("{{lowerCase field.name}}").regex(query)
{{/if}}
{{/each}}
);
this.getSearchCond().addCriteria(criteria);
}
}
}
\ No newline at end of file
package {{packageName}}.core.{{entity.module}}.repository;
import {{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}};
import cn.ibizlab.util.repository.IBZMongoRepository;
public interface {{entity.codeName}}Repository extends IBZMongoRepository<{{entity.codeName}}, {{entity.keyField.type.java}}> {
}
\ No newline at end of file
package {{packageName}}.core.{{entity.module}}.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.math.BigInteger;
import lombok.extern.slf4j.Slf4j;
import com.google.common.collect.Lists;
import cn.ibizlab.util.security.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.util.errors.BadRequestAlertException;
{{#system.enableGlobalTransaction}}
import io.seata.spring.annotation.GlobalTransactional;
{{/system.enableGlobalTransaction}}
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import {{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}};
import {{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext;
import {{packageName}}.core.{{entity.module}}.service.{{entity.codeName}}Service;
import {{packageName}}.core.{{entity.module}}.repository.{{entity.codeName}}Repository;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.security.AuthenticationUser;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
{{#entity.relEntities}}
import {{packageName}}.core.{{module}}.domain.{{codeName}};
{{#neq storage "NONE"}}
import {{packageName}}.core.{{module}}.service.{{codeName}}Service;
{{/neq}}
{{/entity.relEntities}}
/**
* 实体[{{entity.logicName}}] 服务对象接口实现
*/
@Slf4j
@Service("{{entity.codeName}}Service")
public class {{entity.codeName}}ServiceBase implements {{entity.codeName}}Service {
@Autowired
{{entity.codeName}}Repository {{camelCase entity.codeName}}Repository;
{{#entity.relEntities}}
{{#neq storage "NONE"}}
@Autowired
@Lazy
protected {{codeName}}Service {{camelCase codeName}}Service;
{{/neq}}
{{/entity.relEntities}}
{{#if entity.indexSubDE}}
@Autowired
@Lazy
protected {{packageName}}.core.{{entity.module}}.mapping.{{entity.codeName}}InheritMapping {{camelCase entity.codeName}}InheritMapping;
{{/if}}
protected int batchSize = 500;
public {{entity.codeName}} get({{entity.codeName}} et) {
Optional<{{entity.codeName}}> rt = {{camelCase entity.codeName}}Repository.findById(et.get{{pascalCase entity.keyField.codeName}}());
Assert.notNull(rt.get(),"数据不存在,{{entity.logicName}}:"+et.get{{pascalCase entity.keyField.codeName}}());
BeanUtils.copyProperties(rt.get(), et);
{{#entity.nesteds}}
{{#if listCode}}
{{#unless columnName}}
//设置 [{{entityLogicName}}]
et.set{{pascalCase codeName}}({{camelCase entityCodeName}}Service.selectBy{{pascalCase fkField.codeName}}(et.get{{pascalCase entity.keyField.codeName}}()));
{{/unless}}
{{/if}}
{{/entity.nesteds}}
return et;
}
public List<{{entity.codeName}}> getByEntities(List<{{entity.codeName}}> entities) {
Iterable<{{entity.codeName}}> rts = {{camelCase entity.codeName}}Repository.findAllById(entities.stream().map(e->e.get{{pascalCase entity.keyField.codeName}}()).collect(Collectors.toList()));
return Lists.newArrayList(rts);
}
{{#entity.hasReferences}}
public void fillParentData({{entity.codeName}} et) {
{{#each entity.references as |reference|}}
{{#gt reference.relFieldCount 1}}
if(!ObjectUtils.isEmpty(et.get{{pascalCase reference.fkField.codeName}}())) {
{{reference.entityCodeName}} {{camelCase reference.entityCodeName}} = et.get{{pascalCase reference.codeName}}();
if(!ObjectUtils.isEmpty({{camelCase reference.entityCodeName}})) {
{{#each reference.fields as |field|}}
{{#neq field.dataType 'PICKUP'}}
et.set{{pascalCase field.codeName}}({{camelCase reference.entityCodeName}}.get{{pascalCase field.refFieldCodeName}}());
{{/neq}}
{{/each}}
}
}
{{/gt}}
{{/each}}
}
{{/entity.hasReferences}}
public {{entity.codeName}} getDraft({{entity.codeName}} et) {
{{#entity.hasReferences}}
fillParentData(et);
{{/entity.hasReferences}}
return et;
}
public Integer checkKey({{entity.codeName}} et) {
{{#entity.hasPhisicalLinkField}}
fillParentData(et);
{{/entity.hasPhisicalLinkField}}
{{#if entity.unionKeyMode}}
if(ObjectUtils.isEmpty(et.get{{pascalCase entity.keyField.codeName}}()))
et.set{{pascalCase entity.keyField.codeName}}(({{entity.keyField.type.java}})et.getDefaultKey(true));
{{/if}}
return ( (!ObjectUtils.isEmpty(et.get{{pascalCase entity.keyField.codeName}}())) && {{camelCase entity.codeName}}Repository.existsById(et.get{{pascalCase entity.keyField.codeName}}()))?1:0;
}
@Override
{{#eq entity.actions.create.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.create.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean create({{entity.codeName}} et) {
{{#entity.hasPhisicalLinkField}}
fillParentData(et);
{{/entity.hasPhisicalLinkField}}
{{#if entity.isIndexSubDE}}
if(ObjectUtils.isEmpty(et.get{{pascalCase entity.keyField.codeName}}()))
et.set{{pascalCase entity.keyField.codeName}}(({{entity.keyField.type.java}})et.getDefaultKey(true));
{{entity.indexRelation.entityCodeName}} {{camelCase entity.indexRelation.entityCodeName}} = {{camelCase entity.codeName}}InheritMapping.to{{pascalCase entity.indexRelation.entityCodeName}}(et);
{{#if entity.indexRelation.indexTypePSDEField}}
{{camelCase entity.indexRelation.entityCodeName}}.set("{{lowerCase entity.indexRelation.indexTypePSDEField.codeName}}","{{entity.indexRelation.typeValue}}");
{{/if}}
{{camelCase entity.indexRelation.entityCodeName}}Service.create({{camelCase entity.indexRelation.entityCodeName}});
{{else}}
{{#if entity.unionKeyMode}}
if(ObjectUtils.isEmpty(et.get{{pascalCase entity.keyField.codeName}}()))
et.set{{pascalCase entity.keyField.codeName}}(({{entity.keyField.type.java}})et.getDefaultKey(true));
{{/if}}
{{/if}}
{{#if entity.isLogicInheritDE}}
if(!update(et, (Wrapper) et.getUpdateWrapper(true).eq("{{lowerCase entity.keyField.name}}", et.get{{pascalCase keyField.codeName}}())))
return false;
{{else}}
{{camelCase entity.codeName}}Repository.insert(et);
{{/if}}
{{#each entity.nesteds}}
{{#if listCode}}
{{#unless columnName}}
{{camelCase entityCodeName}}Service.saveBy{{pascalCase der.codeName}}(et,et.get{{pascalCase codeName}}());
{{/unless}}
{{/if}}
{{/each}}
{{#if entity.hasPSDERsMapping}}
updateParentData(et);
{{/if}}
return true;
}
{{#eq entity.actions.create.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.create.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean createBatch(List<{{entity.codeName}}> list) {
{{#if (or entity.actions.create.psDEActionLogics entity.isIndexSubDE)}}
for ({{entity.codeName}} et : list) {
getSelf().create(et);
}
{{else}}
{{#entity.hasPhisicalLinkField}}
list.forEach(et->fillParentData(et));
{{/entity.hasPhisicalLinkField}}
{{#if entity.unionKeyMode}}
list.forEach(et -> {
if(ObjectUtils.isEmpty(et.get{{pascalCase entity.keyField.codeName}}()))
et.set{{pascalCase entity.keyField.codeName}}(({{entity.keyField.type.java}})et.getDefaultKey(true));
});
{{/if}}
list = {{camelCase entity.codeName}}Repository.insert(list);
{{#if entity.hasPSDERsMapping}}
updateParentDataBatch(list);
{{/if}}
{{/if}}
return true;
}
{{#eq entity.actions.update.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.update.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean update({{entity.codeName}} et) {
{{#entity.hasPhisicalLinkField}}
fillParentData(et);
{{/entity.hasPhisicalLinkField}}
{{#entity.isIndexSubDE}}
{{entity.indexRelation.entityCodeName}} {{camelCase entity.indexRelation.entityCodeName}} = {{camelCase entity.codeName}}InheritMapping.to{{pascalCase entity.indexRelation.entityCodeName}}(et);
{{#if entity.indexRelation.indexTypePSDEField}}
{{camelCase entity.indexRelation.entityCodeName}}.set("{{lowerCase entity.indexRelation.indexTypePSDEField.codeName}}","{{entity.indexRelation.typeValue}}");
{{/if}}
{{camelCase entity.indexRelation.entityCodeName}}Service.save({{camelCase entity.indexRelation.entityCodeName}});
{{/entity.isIndexSubDE}}
{{camelCase entity.codeName}}Repository.save(et);
{{#each entity.nesteds}}
{{#if listCode}}
{{#unless columnName}}
{{camelCase entityCodeName}}Service.saveBy{{pascalCase der.codeName}}(et,et.get{{pascalCase codeName}}());
{{/unless}}
{{/if}}
{{/each}}
{{#if entity.hasPSDERsMapping}}
updateParentData(et);
{{/if}}
return true;
}
{{#eq entity.actions.update.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.update.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean updateBatch(List<{{entity.codeName}}> list) {
{{#if (or entity.actions.update.psDEActionLogics entity.isIndexSubDE (not entity.keyField.phisicalDEField))}}
for ({{entity.codeName}} et : list) {
getSelf().update(et);
}
{{else}}
{{#entity.hasPhisicalLinkField}}
list.forEach(et->fillParentData(et));
{{/entity.hasPhisicalLinkField}}
list = {{camelCase entity.codeName}}Repository.saveAll(list);
{{#if entity.hasPSDERsMapping}}
updateParentDataBatch(list);
{{/if}}
{{/if}}
return true;
}
{{#eq entity.actions.save.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.save.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean save({{entity.codeName}} et) {
if(checkKey(et)>0)
return getSelf().update(et);
else
return getSelf().create(et);
}
{{#eq entity.actions.save.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.save.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean saveBatch(List<{{entity.codeName}}> list) {
List<{{entity.codeName}}> rt=this.getByEntities(list);
Set<Serializable> keys=new HashSet<>();
rt.forEach(et->{
Serializable key = et.get{{pascalCase entity.keyField.codeName}}();
if(!ObjectUtils.isEmpty(key))
keys.add(key);
});
List<{{entity.codeName}}> _create=new ArrayList<>();
List<{{entity.codeName}}> _update=new ArrayList<>();
list.forEach(et-> {
Serializable key = et.get{{pascalCase entity.keyField.codeName}}();
if(keys.contains(key))
_update.add(et);
else
_create.add(et);
});
List rtList=new ArrayList<>();
if(_update.size()>0 && (!getSelf().updateBatch(_update)))
return false;
if(_create.size()>0 && (!getSelf().createBatch(_create)))
return false;
return true;
}
{{#eq entity.actions.remove.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.remove.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean remove({{entity.codeName}} et) {
{{entity.keyField.type.java}} key = et.get{{pascalCase entity.keyField.codeName}}();
{{#if entity.hasPSDERsMapping}}
get(et);
{{/if}}
{{#entity.nesteds}}
{{#if listCode}}
{{#unless columnName}}
{{#eq removeActionType 1}}
{{camelCase entityCodeName}}Service.removeBy{{pascalCase fkField.codeName}}(key);
{{/eq}}
{{#eq removeActionType 2}}
{{camelCase entityCodeName}}Service.resetBy{{pascalCase fkField.codeName}}(key);
{{/eq}}
{{#eq removeActionType 3}}
if(!ObjectUtils.isEmpty({{camelCase entityCodeName}}Service.selectBy{{pascalCase fkField.codeName}}(key)))
throw new BadRequestAlertException("删除数据失败,当前数据存在关系实体[{{camelCase entityCodeName}}]数据,无法删除!","","");
{{/eq}}
{{/unless}}
{{/if}}
{{/entity.nesteds}}
{{#entity.isIndexSubDE}}
{{camelCase entity.indexRelation.entityCodeName}}Service.remove({{camelCase entity.codeName}}InheritMapping.to{{pascalCase entity.indexRelation.entityCodeName}}(et));
{{/entity.isIndexSubDE}}
{{camelCase entity.codeName}}Repository.deleteById(key);
{{#if entity.hasPSDERsMapping}}
updateParentData(et);
{{/if}}
return true ;
}
{{#eq entity.actions.remove.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.remove.transactionMode "DEFAULT"}}@Transactional{{/eq}}
public boolean removeByEntities(List<{{entity.codeName}}> entities) {
{{#if (or entity.actions.update.psDEActionLogics entity.isIndexSubDE entity.hasPSDERsMapping)}}
for ({{entity.codeName}} et : entities) {
if(!getSelf().remove(et))
return false;
}
return true;
{{else}}
{{camelCase entity.codeName}}Repository.deleteAll(entities);
return true;
{{/if}}
}
{{#entity.extActions}}
{{#eq transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq transactionMode "DEFAULT"}}@Transactional{{/eq}}
public {{outParam}} {{camelCase codeName}}({{inParam}} {{inParamName}}) {
{{#if voidReturn}}
return {{inParamName}};
{{else}}
return null;
{{/if}}
}
{{/entity.extActions}}
{{#entity.dataSets}}
public Page<{{entity.codeName}}> search{{pascalCase codeName}}({{entity.codeName}}SearchContext context) {
return {{camelCase entity.codeName}}Repository.query(context);
}
public List<{{entity.codeName}}> list{{pascalCase codeName}}({{entity.codeName}}SearchContext context) {
return {{camelCase entity.codeName}}Repository.query(context).getContent();
}
{{/entity.dataSets}}
{{#entity.references}}
public List<{{entity.codeName}}> selectBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}}) {
return null;
}
public boolean removeBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}}) {
return false;
}
public boolean resetBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}}) {
return false;
}
public boolean saveBy{{pascalCase codeName}}({{entityCodeName}} {{camelCase entityCodeName}},List<{{entity.codeName}}> list) {
if(list==null)
return true;
Set<{{entity.keyField.type.java}}> delIds=new HashSet<{{entity.keyField.type.java}}>();
List<{{entity.codeName}}> _update=new ArrayList<{{entity.codeName}}>();
List<{{entity.codeName}}> _create=new ArrayList<{{entity.codeName}}>();
for({{entity.codeName}} before:selectBy{{pascalCase fkField.codeName}}({{camelCase entityCodeName}}.get{{pascalCase relEntity.keyField.codeName}}())){
delIds.add(before.get{{pascalCase entity.keyField.codeName}}());
}
for({{entity.codeName}} sub:list) {
sub.set{{pascalCase fkField.codeName}}({{camelCase entityCodeName}}.get{{pascalCase relEntity.keyField.codeName}}());
sub.set{{pascalCase codeName}}({{camelCase entityCodeName}});
if(ObjectUtils.isEmpty(sub.get{{pascalCase entity.keyField.codeName}}()))
sub.set{{pascalCase entity.keyField.codeName}}(({{entity.keyField.type.java}})sub.getDefaultKey(true));
if(delIds.contains(sub.get{{pascalCase entity.keyField.codeName}}())) {
delIds.remove(sub.get{{pascalCase entity.keyField.codeName}}());
_update.add(sub);
}
else
_create.add(sub);
}
if(_update.size()>0 && (!getSelf().updateBatch(_update)))
return false;
if(_create.size()>0 && (!getSelf().createBatch(_create)))
return false;
if(delIds.size()>0 && (!getSelf().removeBatch(delIds)))
return false;
return true;
}
{{/entity.references}}
{{#each entity.nesteds}}
{{#if listCode}}
{{#unless columnName}}
public List<{{entityCodeName}}> get{{pascalCase codeName}}({{entity.codeName}} et) {
return null;
}
{{/unless}}
{{/if}}
{{/each}}
}
\ No newline at end of file
package {{packageName}}.core.{{entity.module}}.service;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.math.BigInteger;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cache.annotation.CacheEvict;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.dynamic.datasource.annotation.DS;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.security.SpringContextHolder;
import {{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}};
import {{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext;
{{#entity.relEntities}}
import {{packageName}}.core.{{module}}.domain.{{codeName}};
{{/entity.relEntities}}
{{#dsName}}
@DS("{{entity.dataSource}}")
{{/dsName}}
public interface {{entity.codeName}}Service {
default {{entity.codeName}}Service getSelf() {
return SpringContextHolder.getBean(this.getClass());
}
{{entity.codeName}} get({{entity.codeName}} et);
default {{entity.codeName}} get({{entity.keyField.type.java}} key) {
{{entity.codeName}} et = new {{entity.codeName}}();
et.set{{pascalCase entity.keyField.codeName}}(key);
return get(et);
}
default List<{{entity.codeName}}> getByIds(Collection<{{entity.keyField.type.java}}> ids) {
List<{{entity.codeName}}> entities =new ArrayList();
ids.forEach(key -> {
{{entity.codeName}} et = new {{entity.codeName}}();
et.set{{pascalCase entity.keyField.codeName}}(key);
entities.add(et);
});
return getByEntities(entities);
}
List<{{entity.codeName}}> getByEntities(List<{{entity.codeName}}> entities);
{{entity.codeName}} getDraft({{entity.codeName}} et);
Integer checkKey({{entity.codeName}} et);
boolean create({{entity.codeName}} et);
{{#entity.enableEntityCache}}
@CacheEvict(value = "{{lowerCase entity.codeName}}", allEntries = true)
{{/entity.enableEntityCache}}
boolean createBatch(List<{{entity.codeName}}> list);
boolean update({{entity.codeName}} et);
{{#entity.enableEntityCache}}
@CacheEvict(value = "{{lowerCase entity.codeName}}", allEntries = true)
{{/entity.enableEntityCache}}
boolean updateBatch(List<{{entity.codeName}}> list);
boolean save({{entity.codeName}} et);
{{#entity.enableEntityCache}}
@CacheEvict(value = "{{lowerCase entity.codeName}}", allEntries = true)
{{/entity.enableEntityCache}}
boolean saveBatch(List<{{entity.codeName}}> list);
default boolean remove({{entity.keyField.type.java}} key) {
{{entity.codeName}} et = new {{entity.codeName}}();
et.set{{pascalCase entity.keyField.codeName}}(key);
return remove(et);
}
default boolean remove(List<{{entity.keyField.type.java}}> key) {
return removeBatch(key);
}
{{#entity.enableEntityCache}}
@CacheEvict(value = "{{lowerCase entity.codeName}}", key = "'row:'+#p0.{{camelCase entity.keyField.codeName}}")
{{/entity.enableEntityCache}}
boolean remove({{entity.codeName}} et);
default boolean removeBatch(Collection<{{entity.keyField.type.java}}> ids) {
List<{{entity.codeName}}> entities =new ArrayList();
ids.forEach(key -> {
{{entity.codeName}} et = new {{entity.codeName}}();
et.set{{pascalCase entity.keyField.codeName}}(key);
entities.add(et);
});
return getSelf().removeByEntities(entities);
}
boolean removeByEntities(List<{{entity.codeName}}> entities);
{{#each entity.extActions}}
default {{outParam}} {{camelCase codeName}}({{inParam}} {{inParamName}}) {
{{#if voidReturn}}
return {{inParamName}};
{{else}}
return null;
{{/if}}
}
{{/each}}
{{#entity.dataSets}}
Page<{{entity.codeName}}> search{{pascalCase codeName}}({{entity.codeName}}SearchContext context);
List<{{entity.codeName}}> list{{pascalCase codeName}}({{entity.codeName}}SearchContext context);
{{/entity.dataSets}}
{{#if entity.hasDupCheck}}
default {{entity.codeName}}SearchContext getSearchContext() {
return new {{entity.codeName}}SearchContext();
}
{{/if}}
{{#entity.references}}
List<{{entity.codeName}}> selectBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}});
{{#entity.enableEntityCache}}
@CacheEvict(value = "{{lowerCase entity.codeName}}", allEntries = true)
{{/entity.enableEntityCache}}
boolean removeBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}});
{{#entity.enableEntityCache}}
@CacheEvict(value = "{{lowerCase entity.codeName}}", allEntries = true)
{{/entity.enableEntityCache}}
boolean resetBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}});
{{#entity.enableEntityCache}}
@CacheEvict(value = "{{lowerCase entity.codeName}}", allEntries = true)
{{/entity.enableEntityCache}}
boolean saveBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}},List<{{entity.codeName}}> list);
{{/entity.references}}
{{#each entity.nesteds}}
{{#if listCode}}
{{#unless columnName}}
List<{{entityCodeName}}> get{{pascalCase codeName}}({{entity.codeName}} et);
{{/unless}}
{{/if}}
{{/each}}
}
\ No newline at end of file
package {{packageName}}.util.config;
import cn.ibizlab.util.helper.JSR310DateConverters;
import cn.ibizlab.util.repository.IBZMongoSimpleRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.convert.*;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@Configuration
@EnableMongoRepositories(basePackages = "{{packageName}}.core.*.repository", repositoryBaseClass = IBZMongoSimpleRepository.class)
public class MongoDBConfig {
@Bean
......@@ -22,4 +31,18 @@ public class MongoDBConfig {
return new MongoCustomConversions(converterList);
}
@Bean
public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
try {
mappingConverter.setCustomConversions(customConversions());
} catch (NoSuchBeanDefinitionException ignore) {
}
//移除插入是附带的_class属性
mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
return mappingConverter;
}
}
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册