yun-zuoyi
lbwgithub 6 years ago
parent d2c3045575
commit 2cf4759c21

@ -215,6 +215,8 @@ public interface BaseRepository <T, ID extends Serializable> extends JpaReposito
List<T> findByProperty(String propertyName, Object value); List<T> findByProperty(String propertyName, Object value);
List<T> findByWasProperty(String[] propertyNames, Object[] values);
List<T> findByProperty(String[] propertyNames, Object[] values); List<T> findByProperty(String[] propertyNames, Object[] values);
List<T> findByProperty(String propertyName, Object value,String orderByStuff); List<T> findByProperty(String propertyName, Object value,String orderByStuff);

@ -3,6 +3,7 @@ package cn.estsh.i3plus.pojo.base.jpa.daoimpl;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean; import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.common.Pager; import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.WmsEnumUtil;
import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository; import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker; import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -37,7 +38,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
private Class<T> persistentClass; private Class<T> persistentClass;
private SnowflakeIdMaker snowflakeIdMaker; private SnowflakeIdMaker snowflakeIdMaker;
public BaseRepositoryImpl(Class<T> clz, EntityManager em,SnowflakeIdMaker snowflakeIdMaker) { public BaseRepositoryImpl(Class<T> clz, EntityManager em, SnowflakeIdMaker snowflakeIdMaker) {
super(clz, em); super(clz, em);
this.entityManager = em; this.entityManager = em;
this.persistentClass = clz; this.persistentClass = clz;
@ -83,7 +84,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
@Override @Override
public T insert(T item) { public T insert(T item) {
return insert(item,true); return insert(item, true);
} }
@Override @Override
@ -94,7 +95,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
@Override @Override
public <S extends T> S save(S entity) { public <S extends T> S save(S entity) {
//复写save方法若id为0或空则新增不然则修改 //复写save方法若id为0或空则新增不然则修改
return (S) innerSave(entity,true); return (S) innerSave(entity, true);
} }
@Override @Override
@ -136,100 +137,101 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
} }
@Override @Override
public void deleteById(ID id){ public void deleteById(ID id) {
deleteByProperty("id", id); deleteByProperty("id", id);
} }
@Override @Override
public int deleteByProperty(String propName, Object propValue) { public int deleteByProperty(String propName, Object propValue) {
return deleteByProperties(new String[] { propName }, new Object[] { propValue }); return deleteByProperties(new String[]{propName}, new Object[]{propValue});
} }
@Override @Override
public int deleteByProperties(String[] propName, Object[] propValue) { public int deleteByProperties(String[] propName, Object[] propValue) {
if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) { if ((propName != null) && (propName.length > 0) && (propValue != null) && (propValue.length > 0) && (propValue.length == propName.length)) {
StringBuffer sb = new StringBuffer("delete from " + persistentClass.getName() + " model where 1=1 "); StringBuffer sb = new StringBuffer("delete from " + persistentClass.getName() + " model where 1=1 ");
appendQL(sb,propName,propValue); appendQL(sb, propName, propValue);
Query query = entityManager.createQuery(sb.toString()); Query query = entityManager.createQuery(sb.toString());
setParameter(query,propName,propValue); setParameter(query, propName, propValue);
return query.executeUpdate(); return query.executeUpdate();
}else{ } else {
throw new IllegalArgumentException("删除错误!propName:" + propName + ",propValue:" + propValue); throw new IllegalArgumentException("删除错误!propName:" + propName + ",propValue:" + propValue);
} }
} }
@Override @Override
public int deleteByIds(ID[] ids) { public int deleteByIds(ID[] ids) {
return deleteByPropertyIn("id", ids); return deleteByPropertyIn("id", ids);
} }
@Override @Override
public int deleteByPropertyIn(String propName, Object[] propValues) { public int deleteByPropertyIn(String propName, Object[] propValues) {
if ((propName != null && propName.length() > 0) && (propValues != null && propValues.length > 0)) { if ((propName != null && propName.length() > 0) && (propValues != null && propValues.length > 0)) {
String hql = "delete from " + persistentClass.getName() + " model where model."+propName+" in(:"+propName+") "; String hql = "delete from " + persistentClass.getName() + " model where model." + propName + " in(:" + propName + ") ";
Query query = entityManager.createQuery(hql); Query query = entityManager.createQuery(hql);
query.setParameter(propName, Arrays.asList(propValues)); query.setParameter(propName, Arrays.asList(propValues));
return query.executeUpdate(); return query.executeUpdate();
}else{ } else {
throw new IllegalArgumentException("删除出错:"+propName+":" + propValues); throw new IllegalArgumentException("删除出错:" + propName + ":" + propValues);
} }
} }
@Override @Override
public int updateByProperties(String conditionName, Object conditionValue, String propertyName, Object propertyValue) { 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 }); return updateByProperties(new String[]{conditionName}, new Object[]{conditionValue}, new String[]{propertyName}, new Object[]{propertyValue});
} }
@Override @Override
public int updateByProperties(String conditionName, Object conditionValue, String[] propertyName, Object[] propertyValue) { public int updateByProperties(String conditionName, Object conditionValue, String[] propertyName, Object[] propertyValue) {
return updateByProperties(new String[] { conditionName }, new Object[] { conditionValue }, propertyName, propertyValue); return updateByProperties(new String[]{conditionName}, new Object[]{conditionValue}, propertyName, propertyValue);
} }
@Override @Override
public int updateByProperties(String[] conditionName, Object[] conditionValue, String propertyName, Object propertyValue) { public int updateByProperties(String[] conditionName, Object[] conditionValue, String propertyName, Object propertyValue) {
return updateByProperties(conditionName, conditionValue, new String[] { propertyName }, new Object[] { propertyValue }); return updateByProperties(conditionName, conditionValue, new String[]{propertyName}, new Object[]{propertyValue});
} }
@Override @Override
public int updateByProperties(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue) { public int updateByProperties(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue) {
return updateByPropertiesMain(conditionName, conditionValue, propertyName, propertyValue,true); return updateByPropertiesMain(conditionName, conditionValue, propertyName, propertyValue, true);
} }
@Override @Override
public int updateByPropertiesWithVal(String conditionName, Object conditionValue, String propertyName, Object propertyValue) { public int updateByPropertiesWithVal(String conditionName, Object conditionValue, String propertyName, Object propertyValue) {
return updateByPropertiesWithVal(new String[] { conditionName }, new Object[] { conditionValue }, new String[] { propertyName }, new Object[] { propertyValue }); return updateByPropertiesWithVal(new String[]{conditionName}, new Object[]{conditionValue}, new String[]{propertyName}, new Object[]{propertyValue});
} }
@Override @Override
public int updateByPropertiesWithVal(String conditionName, Object conditionValue, String[] propertyName, Object[] propertyValue) { public int updateByPropertiesWithVal(String conditionName, Object conditionValue, String[] propertyName, Object[] propertyValue) {
return updateByPropertiesWithVal(new String[] { conditionName }, new Object[] { conditionValue }, propertyName, propertyValue); return updateByPropertiesWithVal(new String[]{conditionName}, new Object[]{conditionValue}, propertyName, propertyValue);
} }
@Override @Override
public int updateByPropertiesWithVal(String[] conditionName, Object[] conditionValue, String propertyName, Object propertyValue) { public int updateByPropertiesWithVal(String[] conditionName, Object[] conditionValue, String propertyName, Object propertyValue) {
return updateByPropertiesWithVal(conditionName, conditionValue, new String[] { propertyName }, new Object[] { propertyValue }); return updateByPropertiesWithVal(conditionName, conditionValue, new String[]{propertyName}, new Object[]{propertyValue});
} }
@Override @Override
public int updateByPropertiesWithVal(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue) { public int updateByPropertiesWithVal(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue) {
return updateByPropertiesMain(conditionName, conditionValue, propertyName, propertyValue,false); return updateByPropertiesMain(conditionName, conditionValue, propertyName, propertyValue, false);
} }
/** /**
* *
* @param conditionName *
* @param conditionName
* @param conditionValue * @param conditionValue
* @param propertyName * @param propertyName
* @param propertyValue * @param propertyValue
* @param valWithSimple * @param valWithSimple
* trueeg price = :price * trueeg price = :price
* falseeg price = price + :price * falseeg price = price + :price
* @return * @return
*/ */
private int updateByPropertiesMain(String[] conditionName, Object[] conditionValue, private int updateByPropertiesMain(String[] conditionName, Object[] conditionValue,
String[] propertyName, Object[] propertyValue,boolean valWithSimple) { String[] propertyName, Object[] propertyValue, boolean valWithSimple) {
if ((propertyName != null) && (propertyName.length > 0) && (propertyValue != null) if ((propertyName != null) && (propertyName.length > 0) && (propertyValue != null)
&& (propertyValue.length > 0) && (propertyName.length == propertyValue.length) && (propertyValue.length > 0) && (propertyName.length == propertyValue.length)
&& (conditionValue != null) && (conditionValue.length > 0)) { && (conditionValue != null) && (conditionValue.length > 0)) {
@ -237,9 +239,9 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
sb.append("update " + persistentClass.getName() + " model set "); sb.append("update " + persistentClass.getName() + " model set ");
for (int i = 0; i < propertyName.length; i++) { for (int i = 0; i < propertyName.length; i++) {
if(valWithSimple) { if (valWithSimple) {
sb.append(propertyName[i] + " = :p_" + propertyName[i] + ","); sb.append(propertyName[i] + " = :p_" + propertyName[i] + ",");
}else{ } else {
sb.append(propertyName[i] + " = " + propertyName[i] + " + :p_" + propertyName[i] + ","); sb.append(propertyName[i] + " = " + propertyName[i] + " + :p_" + propertyName[i] + ",");
} }
} }
@ -261,36 +263,36 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
} }
@Override @Override
public int updateByProperties(String propertyName, Object propertyValue,DdlPackBean packBean) { public int updateByProperties(String propertyName, Object propertyValue, DdlPackBean packBean) {
return updateByProperties(new String[] { propertyName }, new Object[] { propertyValue },packBean); return updateByProperties(new String[]{propertyName}, new Object[]{propertyValue}, packBean);
} }
@Override @Override
public int updateByProperties(String[] propertyName, Object[] propertyValue,DdlPackBean packBean) { public int updateByProperties(String[] propertyName, Object[] propertyValue, DdlPackBean packBean) {
return updateByPropertiesDdlPack(propertyName, propertyValue,packBean,true); return updateByPropertiesDdlPack(propertyName, propertyValue, packBean, true);
} }
@Override @Override
public int updateByPropertiesWithVal(String propertyName, Object propertyValue,DdlPackBean packBean) { public int updateByPropertiesWithVal(String propertyName, Object propertyValue, DdlPackBean packBean) {
return updateByPropertiesWithVal(new String[] { propertyName }, new Object[] { propertyValue },packBean); return updateByPropertiesWithVal(new String[]{propertyName}, new Object[]{propertyValue}, packBean);
} }
@Override @Override
public int updateByPropertiesWithVal(String[] propertyName, Object[] propertyValue,DdlPackBean packBean) { public int updateByPropertiesWithVal(String[] propertyName, Object[] propertyValue, DdlPackBean packBean) {
return updateByPropertiesDdlPack(propertyName, propertyValue,packBean,false); return updateByPropertiesDdlPack(propertyName, propertyValue, packBean, false);
} }
private int updateByPropertiesDdlPack(String[] propertyName, Object[] propertyValue, private int updateByPropertiesDdlPack(String[] propertyName, Object[] propertyValue,
DdlPackBean packBean,boolean valWithSimple) { DdlPackBean packBean, boolean valWithSimple) {
if ((propertyName != null) && (propertyName.length > 0) && (propertyValue != null) if ((propertyName != null) && (propertyName.length > 0) && (propertyValue != null)
&& (propertyValue.length > 0) && (propertyName.length == propertyValue.length)) { && (propertyValue.length > 0) && (propertyName.length == propertyValue.length)) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append("update " + persistentClass.getName() + " model set "); sb.append("update " + persistentClass.getName() + " model set ");
for (int i = 0; i < propertyName.length; i++) { for (int i = 0; i < propertyName.length; i++) {
if(valWithSimple) { if (valWithSimple) {
sb.append(propertyName[i] + " = :p_" + propertyName[i] + ","); sb.append(propertyName[i] + " = :p_" + propertyName[i] + ",");
}else{ } else {
sb.append(propertyName[i] + " = " + propertyName[i] + " + :p_" + propertyName[i] + ","); sb.append(propertyName[i] + " = " + propertyName[i] + " + :p_" + propertyName[i] + ",");
} }
} }
@ -304,7 +306,7 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
} }
//查询条件 //查询条件
for (String key : packBean.getHqlPreparedMap().keySet()) { for (String key : packBean.getHqlPreparedMap().keySet()) {
query.setParameter("m_" + key,packBean.getHqlPreparedMap().get(key)); query.setParameter("m_" + key, packBean.getHqlPreparedMap().get(key));
} }
return query.executeUpdate(); return query.executeUpdate();
@ -345,9 +347,9 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
@Override @Override
public T getById(long id) { public T getById(long id) {
try{ try {
return entityManager.find(persistentClass,id); return entityManager.find(persistentClass, id);
}catch (Exception e){ } catch (Exception e) {
return null; return null;
} }
} }
@ -361,12 +363,12 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
@Override @Override
public List<T> listPager(Pager pager) { public List<T> listPager(Pager pager) {
if(pager.getTotalRows() > 0) { if (pager.getTotalRows() > 0) {
return entityManager.createQuery("from " + persistentClass.getName()) return entityManager.createQuery("from " + persistentClass.getName())
.setFirstResult(pager.getStartRow()) .setFirstResult(pager.getStartRow())
.setMaxResults(pager.getPageSize()) .setMaxResults(pager.getPageSize())
.getResultList(); .getResultList();
}else{ } else {
return new ArrayList<T>(); return new ArrayList<T>();
} }
} }
@ -379,16 +381,16 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
@Override @Override
public List<T> findByPage(DdlPackBean packBean, int offset, int pageSize) { public List<T> findByPage(DdlPackBean packBean, int offset, int pageSize) {
Query query = null; Query query = null;
String ddl = "from " + persistentClass.getName() + " where 1=1 " +packBean.getPackedHql(); String ddl = "from " + persistentClass.getName() + " where 1=1 " + packBean.getPackedHql();
if(packBean.isHql()){ if (packBean.isHql()) {
query = entityManager.createQuery(ddl); query = entityManager.createQuery(ddl);
}else{ } else {
query = entityManager.createNativeQuery(ddl); query = entityManager.createNativeQuery(ddl);
} }
for (String key : packBean.getHqlPreparedMap().keySet()) { for (String key : packBean.getHqlPreparedMap().keySet()) {
query.setParameter("m_" + key,packBean.getHqlPreparedMap().get(key)); query.setParameter("m_" + key, packBean.getHqlPreparedMap().get(key));
} }
return query.setFirstResult(offset) return query.setFirstResult(offset)
@ -412,6 +414,34 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
} }
@Override @Override
public List<T> findByWasProperty(String[] propertyNames, Object[] values) {
if (propertyNames.length != values.length) {
throw new IllegalArgumentException("参数名的数量和参数值不匹配!propertyNames:" + propertyNames.length + ",values:" + values.length);
}
StringBuffer queryString = new StringBuffer();
queryString.append(" select new "+persistentClass.getSimpleName()+"(snStatus,whNo,locateNo,partNo,partNameRdd,lotNo,sum(qty)) from " + persistentClass.getSimpleName() + " ");
int size = propertyNames.length;
if (size > 0) {
queryString.append("where 1=1 and snStatus in ('"+ WmsEnumUtil.STOCK_SN_STATUS.PRE_INSTOCK.getValue()+"','"+WmsEnumUtil.STOCK_SN_STATUS.INSTOCKED.getValue()+"','"+WmsEnumUtil.STOCK_SN_STATUS.PICKED.getValue()+"','"+WmsEnumUtil.STOCK_SN_STATUS.QUALITY_CONTROL.getValue()+"') ");
}
for (int i = 0; i < size; i++) {
if (values[i] != null) {
queryString.append(" and " + propertyNames[i] + "= :" + propertyNames[i]);
}
}
queryString.append("/n group by lotNo,dateCode");
Query queryObject = entityManager.createQuery(queryString.toString());
for (int i = 0; i < size; i++) {
if (values[i] != null) {
queryObject.setParameter(propertyNames[i], values[i]);
}
}
return queryObject.getResultList();
}
@Override
public List<T> findByProperty(String[] propertyNames, Object[] values) { public List<T> findByProperty(String[] propertyNames, Object[] values) {
if(propertyNames.length != values.length){ if(propertyNames.length != values.length){
throw new IllegalArgumentException("参数名的数量和参数值不匹配!propertyNames:" + propertyNames.length + ",values:" + values.length); throw new IllegalArgumentException("参数名的数量和参数值不匹配!propertyNames:" + propertyNames.length + ",values:" + values.length);

@ -286,4 +286,15 @@ public class WmsStockSn extends BaseBean {
this.isDeleted = isDeleted; this.isDeleted = isDeleted;
this.isValid = isValid; this.isValid = isValid;
} }
public WmsStockSn(Integer snStatus,String whNo,String locateNo,String partNo,String partNameRdd,String lotNo,Double qty){
this.snStatus=snStatus;
this.whNo = whNo;
this.locateNo = locateNo;
this.partNo = partNo;
this.partNameRdd = partNameRdd;
this.lotNo = lotNo;
this.qty = qty;
}
} }

Loading…
Cancel
Save