diff --git a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/dao/BaseRepository.java b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/dao/BaseRepository.java index 129cfe3..e13e761 100644 --- a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/dao/BaseRepository.java +++ b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/dao/BaseRepository.java @@ -72,26 +72,6 @@ public interface BaseRepository extends JpaReposito public int deleteByProperties(String[] propNames, Object[] objValues); /** - * 弱删除 - * @return count - */ - public int deleteWeaklyById(ID id); - public int deleteWeaklyByIds(Long[] ids) ; - public int deleteWeaklyByProperty(String propName, Object propValue); - public int deleteWeaklyByPropertyIn(String propName, Object[] propValues); - public int deleteWeaklyByProperties(String[] propNames, Object[] objValues); - - /** - * 更新有效无效状态 - * @return count - */ - public int updateValidStatusById(ID id,int status); - public int updateValidStatusByIds(Long[] ids,int status) ; - public int updateValidStatusByProperty(String propName, Object propValue,int status); - public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status); - public int updateValidStatusByProperties(String[] propNames, Object[] objValues,int status); - - /** * 通过多单属性进行单个值更新 * @param conditionName * @param conditionValue @@ -235,4 +215,26 @@ public interface BaseRepository extends JpaReposito public List findBySqlObjList(String sql); public List findBySqlObjListByPager(String sql,Pager pager); + + /****************** 部分业务处理 *******************/ + + /** + * 弱删除 + * @return count + */ + public int deleteWeaklyById(ID id,String username); + public int deleteWeaklyByIds(Long[] ids,String username) ; + public int deleteWeaklyByProperty(String propName, Object propValue,String username); + public int deleteWeaklyByPropertyIn(String propName, Object[] propValues,String username); + public int deleteWeaklyByProperties(String[] propNames, Object[] objValues,String username); + + /** + * 更新有效无效状态 + * @return count + */ + public int updateValidStatusById(ID id,int status,String username); + public int updateValidStatusByIds(Long[] ids,int status,String username) ; + public int updateValidStatusByProperty(String propName, Object propValue,int status,String username); + public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status,String username); + public int updateValidStatusByProperties(String[] propNames, Object[] objValues,int status,String username); } diff --git a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseMongoRepositoryImpl.java b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseMongoRepositoryImpl.java index bd7ab88..022ca3f 100644 --- a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseMongoRepositoryImpl.java +++ b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseMongoRepositoryImpl.java @@ -12,6 +12,8 @@ import com.mongodb.client.model.Sorts; import org.apache.commons.lang3.StringUtils; import org.bson.Document; import org.bson.conversions.Bson; +import org.bson.json.JsonMode; +import org.bson.json.JsonWriterSettings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.mapping.context.MappingContext; @@ -20,6 +22,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.repository.query.MongoEntityInformation; import org.springframework.data.mongodb.repository.support.SimpleMongoRepository; +import springfox.documentation.spring.web.json.Json; import javax.persistence.Id; import java.io.Serializable; @@ -47,6 +50,7 @@ public class BaseMongoRepositoryImpl extends SimpleM private final MongoEntityInformation entityInformation; private final MappingContext, MongoPersistentProperty> mongoContext; private final Class entityClass; + private SnowflakeIdMaker snowflakeIdMaker; public BaseMongoRepositoryImpl(MongoEntityInformation metadata, MongoOperations mongoOperations) { super(metadata, mongoOperations); @@ -54,6 +58,7 @@ public class BaseMongoRepositoryImpl extends SimpleM this.mongoOperations = mongoOperations; this.entityClass = this.entityInformation.getJavaType(); mongoContext = mongoOperations.getConverter().getMappingContext(); + snowflakeIdMaker = new SnowflakeIdMaker(); } /** @@ -99,7 +104,7 @@ public class BaseMongoRepositoryImpl extends SimpleM Object val = idField.get(item); if((type == long.class || type == Long.class) && (val == null || Long.parseLong(val.toString()) == 0)){ // long类型主键,以snowflake为主键 - idField.set(item, new SnowflakeIdMaker().nextId()); + idField.set(item, snowflakeIdMaker.nextId()); } else if(type == String.class && (val==null || "".equals(val))){ // String类型主键,以UUID为主键 idField.set(item, UUID.randomUUID().toString().replace("-", "").toLowerCase()); @@ -222,8 +227,10 @@ public class BaseMongoRepositoryImpl extends SimpleM private List packObjectListFromDocument(List dList) { //将获取的document转为对象 List resultList = new ArrayList<>(); + // 设置为宽松模式 + JsonWriterSettings jsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build(); for(Document d : dList){ - resultList.add(JSONObject.parseObject(d.toJson(), entityClass)); + resultList.add(JSONObject.parseObject(d.toJson(jsonWriterSettings), entityClass)); } return resultList; diff --git a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseRepositoryImpl.java b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseRepositoryImpl.java index 0466c15..01e6c9d 100644 --- a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseRepositoryImpl.java +++ b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/jpa/daoimpl/BaseRepositoryImpl.java @@ -2,7 +2,6 @@ package cn.estsh.i3plus.pojo.base.jpa.daoimpl; import cn.estsh.i3plus.pojo.base.common.Pager; import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; -import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil; import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository; import cn.estsh.i3plus.pojo.base.tool.SnowflakeIdMaker; import org.slf4j.Logger; @@ -14,6 +13,7 @@ import javax.persistence.Id; import javax.persistence.Query; import java.io.Serializable; import java.lang.reflect.Field; +import java.text.SimpleDateFormat; import java.util.*; /** @@ -33,11 +33,13 @@ public class BaseRepositoryImpl extends SimpleJpaRep private final EntityManager entityManager; private Class persistentClass; + private SnowflakeIdMaker snowflakeIdMaker; public BaseRepositoryImpl(Class clz, EntityManager em) { super(clz, em); this.entityManager = em; this.persistentClass = clz; + this.snowflakeIdMaker = new SnowflakeIdMaker(); } private void setParameter(Query query, String[] propName, Object[] propValue) { @@ -146,7 +148,7 @@ public class BaseRepositoryImpl extends SimpleJpaRep return query.executeUpdate(); }else{ - throw new IllegalArgumentException("Method deleteByProperties argument is illegal!propName:" + propName + ",propValue:" + propValue); + throw new IllegalArgumentException("删除错误!propName:" + propName + ",propValue:" + propValue); } } @@ -169,112 +171,6 @@ public class BaseRepositoryImpl extends SimpleJpaRep } @Override - public int deleteWeaklyById(ID id) { - return deleteWeaklyByProperty("id", id); - } - - @Override - public int deleteWeaklyByIds(Long[] ids) { - return deleteWeaklyByPropertyIn("id", ids); - } - - @Override - public int deleteWeaklyByProperty(String propName, Object propValue) { - return deleteWeaklyByProperties(new String[] { propName }, new Object[] { propValue }); - } - - /** - * 批量弱删除 - * @return - */ - @Override - public int deleteWeaklyByPropertyIn(String propName, Object[] propValues) { - if(propValues != null && propValues.length > 0){ - String hql = "update " + persistentClass.getName() + " model set model.isValid = :isValid where model."+propName+" in(:"+propName+") "; - Query query = entityManager.createQuery(hql); - query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue()); - query.setParameter(propName, Arrays.asList(propValues)); - - return query.executeUpdate(); - }else{ - throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValues); - } - } - - /** - * 批量弱删除 - * @return - */ - @Override - public int deleteWeaklyByProperties(String[] propName, Object[] propValue) { - if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) { - StringBuffer sb = new StringBuffer("update " + persistentClass.getName() + " model set model.isValid = :isValid where 1=1 "); - appendQL(sb,propName,propValue); - Query query = entityManager.createQuery(sb.toString()); - query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue()); - - setParameter(query,propName,propValue); - - return query.executeUpdate(); - }else{ - throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValue); - } - } - - @Override - public int updateValidStatusById(ID id,int status) { - return updateValidStatusByProperty("id", id,status); - } - - @Override - public int updateValidStatusByIds(Long[] ids,int status) { - return updateValidStatusByPropertyIn("id", ids,status); - } - - @Override - public int updateValidStatusByProperty(String propName, Object propValue,int status) { - return updateValidStatusByProperties(new String[] { propName }, new Object[] { propValue },status); - } - - /** - * 批量弱删除 - * @return - */ - @Override - public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status) { - if(propValues != null && propValues.length > 0){ - String hql = "update " + persistentClass.getName() + " model set model.isValid = :isValid where model."+propName+" in(:"+propName+") "; - Query query = entityManager.createQuery(hql); - query.setParameter("isValid",status); - query.setParameter(propName, Arrays.asList(propValues)); - - return query.executeUpdate(); - }else{ - throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValues); - } - } - - /** - * 批量弱删除 - * @return - */ - @Override - public int updateValidStatusByProperties(String[] propName, Object[] propValue,int status) { - if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) { - StringBuffer sb = new StringBuffer("update " + persistentClass.getName() + " model set model.isValid = :isValid where 1=1 "); - appendQL(sb,propName,propValue); - Query query = entityManager.createQuery(sb.toString()); - query.setParameter("isValid",status); - - setParameter(query,propName,propValue); - - return query.executeUpdate(); - }else{ - throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValue); - } - } - - @Override public int updateByProperties(String conditionName, Object conditionValue, String propertyName, Object propertyValue) { return updateByProperties(new String[] { conditionName }, new Object[] { conditionValue }, new String[] { propertyName }, new Object[] { propertyValue }); } @@ -312,7 +208,7 @@ public class BaseRepositoryImpl extends SimpleJpaRep setParameter(query, conditionName, conditionValue); return query.executeUpdate(); } else { - throw new IllegalArgumentException("Method updateByProperties argument is illegal!conditionName:" + conditionName + throw new IllegalArgumentException("参数值错误!conditionName:" + conditionName + ",conditionValue:" + conditionValue + ",propertyName:" + propertyName + ",propertyValue:" + propertyValue); } } @@ -343,7 +239,7 @@ public class BaseRepositoryImpl extends SimpleJpaRep return query.executeUpdate(); } else { - throw new IllegalArgumentException("Method updateByProperties argument is illegal! propertyName:" + propertyName + ",propertyValue:" + propertyValue); + throw new IllegalArgumentException("参数值错误! propertyName:" + propertyName + ",propertyValue:" + propertyValue); } } @@ -925,7 +821,7 @@ public class BaseRepositoryImpl extends SimpleJpaRep Object val = idField.get(item); if((type == long.class || type == Long.class) && (val == null || Long.parseLong(val.toString()) == 0)){ // long类型主键,以snowflake为主键 - idField.set(item, new SnowflakeIdMaker().nextId()); + idField.set(item, snowflakeIdMaker.nextId()); } else if(type == String.class && (val==null || "".equals(val))){ // String类型主键,以UUID为主键 idField.set(item, UUID.randomUUID().toString().replace("-", "").toLowerCase()); @@ -944,4 +840,140 @@ public class BaseRepositoryImpl extends SimpleJpaRep throw new RuntimeException(e); } } + + /************** 部分业务 *************/ + private String getNowTime(boolean needSecond) { + return needSecond ? getNowTime("yyyy-MM-dd HH:mm:ss") : getNowTime("yyyy-MM-dd HH:mm"); + } + + private String getNowTime(String format) { + String dateTime; + try { + dateTime = (new SimpleDateFormat(format)).format(new Date()); + } catch (Exception e) { + dateTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()); + } + + return dateTime; + } + + @Override + public int deleteWeaklyById(ID id,String username) { + return deleteWeaklyByProperty("id", id,username); + } + + @Override + public int deleteWeaklyByIds(Long[] ids,String username) { + return deleteWeaklyByPropertyIn("id", ids,username); + } + + @Override + public int deleteWeaklyByProperty(String propName, Object propValue,String username) { + return deleteWeaklyByProperties(new String[] { propName }, new Object[] { propValue },username); + } + + /** + * 批量弱删除 + * @return + */ + @Override + public int deleteWeaklyByPropertyIn(String propName, Object[] propValues,String username) { + if(propValues != null && propValues.length > 0){ + String hql = "update " + persistentClass.getName() + + " model set model.isValid = :isValid,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where model." + + propName + " in(:"+propName+") "; + Query query = entityManager.createQuery(hql); + query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue()); + query.setParameter("modifyUser",username); + query.setParameter("modifyDatetime",getNowTime(true)); + query.setParameter(propName, Arrays.asList(propValues)); + + return query.executeUpdate(); + }else{ + throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValues); + } + } + + /** + * 批量弱删除 + * @return + */ + @Override + public int deleteWeaklyByProperties(String[] propName, Object[] propValue,String username) { + if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) { + StringBuffer sb = new StringBuffer("update " + persistentClass.getName() + + " model set model.isValid = :isValid,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where 1=1 "); + appendQL(sb,propName,propValue); + Query query = entityManager.createQuery(sb.toString()); + query.setParameter("modifyUser",username); + query.setParameter("modifyDatetime",getNowTime(true)); + query.setParameter("isValid",CommonEnumUtil.IS_VAILD.INVAILD.getValue()); + + setParameter(query,propName,propValue); + + return query.executeUpdate(); + }else{ + throw new IllegalArgumentException("弱删除失败:"+propName+":" + propValue); + } + } + + @Override + public int updateValidStatusById(ID id,int status,String username) { + return updateValidStatusByProperty("id", id,status,username); + } + + @Override + public int updateValidStatusByIds(Long[] ids,int status,String username) { + return updateValidStatusByPropertyIn("id", ids,status,username); + } + + @Override + public int updateValidStatusByProperty(String propName, Object propValue,int status,String username) { + return updateValidStatusByProperties(new String[] { propName }, new Object[] { propValue },status,username); + } + + /** + * 批量弱删除 + * @return + */ + @Override + public int updateValidStatusByPropertyIn(String propName, Object[] propValues,int status,String username) { + if(propValues != null && propValues.length > 0){ + String hql = "update " + persistentClass.getName() + + " model set model.isValid = :isValid ,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where model." + + propName + " in(:"+propName+") "; + Query query = entityManager.createQuery(hql); + query.setParameter("modifyUser",username); + query.setParameter("modifyDatetime",getNowTime(true)); + query.setParameter("isValid",status); + query.setParameter(propName, Arrays.asList(propValues)); + + return query.executeUpdate(); + }else{ + throw new IllegalArgumentException("修改状态失败:"+propName+":" + propValues); + } + } + + /** + * 批量弱删除 + * @return + */ + @Override + public int updateValidStatusByProperties(String[] propName, Object[] propValue,int status,String username) { + if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) { + StringBuffer sb = new StringBuffer("update " + persistentClass.getName() + + " model set model.isValid = :isValid,model.modifyUser= :modifyUser,model.modifyDatetime=:modifyDatetime where 1=1 "); + appendQL(sb,propName,propValue); + Query query = entityManager.createQuery(sb.toString()); + query.setParameter("modifyUser",username); + query.setParameter("modifyDatetime",getNowTime(true)); + query.setParameter("isValid",status); + + setParameter(query,propName,propValue); + + return query.executeUpdate(); + }else{ + throw new IllegalArgumentException("修改状态失败:"+propName+":" + propValue); + } + } } diff --git a/modules/i3plus-pojo-platform/src/main/java/cn/estsh/i3plus/pojo/platform/bean/SysException.java b/modules/i3plus-pojo-platform/src/main/java/cn/estsh/i3plus/pojo/platform/bean/SysException.java new file mode 100644 index 0000000..5bf5e4d --- /dev/null +++ b/modules/i3plus-pojo-platform/src/main/java/cn/estsh/i3plus/pojo/platform/bean/SysException.java @@ -0,0 +1,45 @@ +package cn.estsh.i3plus.pojo.platform.bean; + +import cn.estsh.i3plus.pojo.base.bean.BaseBean; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiParam; +import lombok.Data; +import org.hibernate.annotations.DynamicInsert; +import org.hibernate.annotations.DynamicUpdate; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +/** + * @Description : 系统异常 + * @Reference : + * @Author : Frin + * @Date : 2018-11-8 12:30:00 + * @Modify : + **/ +@Data +@Entity +@Table(name="SYS_EXCEPTION") +@DynamicInsert +@DynamicUpdate +@Api(value="系统异常表",description = "记录系统出现的异常") +public class SysException extends BaseBean { + + @Column(name="EXC_MODULE_ID") + @ApiParam(value ="系统模块(枚举)", example = "1") + //CommonEnumUtil.SOFT_TYPE + private Integer excModuleId; + + @Column(name="EXC_CLASS_NAME") + @ApiParam(value ="异常类名") + private String excClassName; + + @Column(name="EXC_MESSAGE") + @ApiParam(value ="异常信息") + private String excMessage; + + @Column(name="EXC_STACK") + @ApiParam(value ="堆栈信息") + private String excStack; +} diff --git a/modules/i3plus-pojo-platform/src/main/java/cn/estsh/i3plus/pojo/platform/repositorymongo/SysExceptionRepository.java b/modules/i3plus-pojo-platform/src/main/java/cn/estsh/i3plus/pojo/platform/repositorymongo/SysExceptionRepository.java new file mode 100644 index 0000000..05697c4 --- /dev/null +++ b/modules/i3plus-pojo-platform/src/main/java/cn/estsh/i3plus/pojo/platform/repositorymongo/SysExceptionRepository.java @@ -0,0 +1,14 @@ +package cn.estsh.i3plus.pojo.platform.repositorymongo; + +import cn.estsh.i3plus.pojo.base.jpa.dao.BaseMongoRepository; +import cn.estsh.i3plus.pojo.platform.bean.SysException; + +/** + * @Description : 异常记录表(使用Mongodb) + * @Reference : + * @Author : frin + * @Date : 2018-11-8 12:03:00 + * @Modify : + **/ +public interface SysExceptionRepository extends BaseMongoRepository { +}