@Service
@Transactional
public class BrandServiceBean extends DaoSupport<Brand> implements BrandService {
@Override
public void save(Object entity) {
((Brand)entity).setCode(UUID.randomUUID().toString());
super.save(entity);
}
}
----------------------------------------------------------------
@Service
@Transactional
public class ProductInfoServiceBean extends DaoSupport<ProductInfo> implements ProductInfoService {
@Resource(name="productTypeServiceBean") private ProductTypeService productTypeService;
@SuppressWarnings("unchecked")
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public List<ProductInfo> getViewHistory(Integer[] productids, int maxResult){
StringBuffer jpql = new StringBuffer();
for(int i=0 ;i<productids.length; i++){
jpql.append('?').append(i).append(',');
}
jpql.deleteCharAt(jpql.length()-1);
Query query = em.createQuery("select o from ProductInfo o where o.id in("+ jpql.toString()+")");
for(int i=0 ;i<productids.length; i++){
query.setParameter(i, productids[i]);
}
query.setFirstResult(0).setMaxResults(maxResult);
return query.getResultList();
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public List<ProductInfo> getTopSell(Integer typeid, int maxResult){
List<Integer> typeids = new ArrayList<Integer>();
typeids.add(typeid);
getTypeids(typeids, new Integer[]{typeid});
StringBuffer n = new StringBuffer();
for(int i=0; i<typeids.size();i++){
n.append('?').append((i+2)).append(',');
}
n.deleteCharAt(n.length()-1);
Query query = em.createQuery("select o from ProductInfo o where o.commend=?1 and o.type.typeid in("+ n.toString()+") order by o.sellcount desc");
query.setParameter(1, true);
for(int i=0; i<typeids.size();i++){
query.setParameter(i+2, typeids.get(i));
}
query.setFirstResult(0).setMaxResults(maxResult);
return query.getResultList();
}
private void getTypeids(List<Integer> outtypeids, Integer[] typeids){
List<Integer> subtypeids = productTypeService.getSubTypeid(typeids);
if(subtypeids!=null && subtypeids.size()>0){
outtypeids.addAll(subtypeids);
Integer[] ids = new Integer[subtypeids.size()];
for(int i=0;i<subtypeids.size();i++){
ids[i]=subtypeids.get(i);
}
getTypeids(outtypeids, ids);
}
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public List<Brand> getBrandsByProductTypeid(Integer[] typeids){
if(typeids!=null && typeids.length>0){
StringBuffer jpql = new StringBuffer();
for(int i=0;i<typeids.length;i++){
jpql.append('?').append((i+1)).append(',');
}
jpql.deleteCharAt(jpql.length()-1);
Query query = em.createQuery("select o from Brand o where o.code in(select p.brand.code from ProductInfo p where p.type.typeid in("+ jpql.toString()+") group by p.brand.code)");
for(int i=0;i<typeids.length;i++){
query.setParameter(i+1, typeids[i]);
}
return query.getResultList();
}
return null;
}
public void setCommendStatu(Integer[] productids, boolean statu) {
if(productids!=null && productids.length>0){
StringBuffer jpql = new StringBuffer();
for(int i=0;i<productids.length;i++){
jpql.append('?').append((i+2)).append(',');
}
jpql.deleteCharAt(jpql.length()-1);
Query query = em.createQuery("update ProductInfo o set o.commend=?1 where o.id in("+ jpql.toString()+ ")");
query.setParameter(1, statu);
for(int i=0;i<productids.length;i++){
query.setParameter(i+2, productids[i]);
}
query.executeUpdate();
}
}
public void setVisibleStatu(Integer[] productids, boolean statu) {
if(productids!=null && productids.length>0){
StringBuffer jpql = new StringBuffer();
for(int i=0;i<productids.length;i++){
jpql.append('?').append((i+2)).append(',');
}
jpql.deleteCharAt(jpql.length()-1);
Query query = em.createQuery("update ProductInfo o set o.visible=?1 where o.id in("+ jpql.toString()+ ")");
query.setParameter(1, statu);
for(int i=0;i<productids.length;i++){
query.setParameter(i+2, productids[i]);
}
query.executeUpdate();
}
}
}
-------------------------------------------------------------------
@Service
public class ProductSearchBean implements ProductSearch {
private CompassTemplate compassTemplate;
@Resource
public void setCompass(Compass compass){
compassTemplate = new CompassTemplate(compass);
}
public QueryResult<ProductInfo> query(String key, int firstResult, int maxResult) {
return compassTemplate.execute(new QueryCallback(key, firstResult, maxResult));
}
}
-------------------------------------------------------------------
@Service
@Transactional
public class ProductStyleServiceBean extends DaoSupport<ProductStyle> implements ProductStyleService {
public void setVisibleStatu(Integer[] productstyleids, boolean statu) {
if(productstyleids!=null && productstyleids.length>0){
StringBuffer jpql = new StringBuffer();
for(int i=0;i<productstyleids.length;i++){
jpql.append('?').append((i+2)).append(',');
}
jpql.deleteCharAt(jpql.length()-1);
Query query = em.createQuery("update ProductStyle o set o.visible=?1 where o.id in("+ jpql.toString()+ ")");
query.setParameter(1, statu);
for(int i=0;i<productstyleids.length;i++){
query.setParameter(i+2, productstyleids[i]);
}
query.executeUpdate();
}
}
}
----------------------------------------------------------------------------
@Service
@Transactional
public class ProductTypeServiceBean extends DaoSupport<ProductType> implements ProductTypeService {
@Override
public void delete(Serializable ... entityids) {
if(entityids!=null && entityids.length>0){
StringBuffer jpql = new StringBuffer();
for(int i=0 ;i<entityids.length;i++){
jpql.append("?").append(i+2).append(",");
}
jpql.deleteCharAt(jpql.length()-1);
Query query = em.createQuery("update ProductType o set o.visible=?1 where o.typeid in("+ jpql.toString()+")")
.setParameter(1, false);
for(int i=0 ;i<entityids.length;i++){
query.setParameter(i+2, entityids[i]);
}
query.executeUpdate();
}
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public List<Integer> getSubTypeid(Integer[] parentids){
if(parentids!=null && parentids.length>0){
StringBuffer jpql = new StringBuffer();
for(int i=0; i<parentids.length; i++){
jpql.append('?').append((i+1)).append(',');
}
jpql.deleteCharAt(jpql.length()-1);
Query query = em.createQuery("select o.typeid from ProductType o where o.parent.typeid in("+ jpql.toString()+ ")");
for(int i=0; i<parentids.length; i++){
query.setParameter(i+1, parentids[i]);
}
return query.getResultList();
}
return null;
}
}
-----------------------------------------------------------------------------
public class QueryCallback implements CompassCallback<QueryResult<ProductInfo>> {
private String key;
private int firstResult;
private int maxResult;
public QueryCallback(String key, int firstResult, int maxResult) {
this.key = key;
this.firstResult = firstResult;
this.maxResult = maxResult;
}
public QueryResult<ProductInfo> doInCompass(CompassSession session) throws CompassException {
CompassHits hits = session.find(key);
CompassQueryBuilder queryBuilder = session.queryBuilder();
/*
* 查询产品类别等于12,并且任意索引属性匹配查询关键词的记录,记录按销售价降序排列
CompassHits queryhits = queryBuilder.bool().addMust(queryBuilder.spanEq("typeid", 12))
.addMust(queryBuilder.queryString(key).toQuery())
.toQuery().addSort("sellprice", SortPropertyType.FLOAT, SortDirection.REVERSE)
.hits();
*/
QueryResult<ProductInfo> qr = new QueryResult<ProductInfo>();
qr.setTotalrecord(hits.length());//得到匹配记录的总数 1
int length = firstResult+ maxResult;
if(length>hits.length()) length = hits.length();
List<ProductInfo> products = new ArrayList<ProductInfo>();
for(int i=firstResult; i<length; i++){
ProductInfo product = (ProductInfo)hits.data(i);
if(hits.highlighter(i).fragment("productName")!=null){//处理高亮显示
product.setName(hits.highlighter(i).fragment("productName"));
}
if(hits.highlighter(i).fragment("description")!=null){//处理高亮显示
product.setDescription(hits.highlighter(i).fragment("description"));
}
products.add(product);
}
qr.setResultlist(products);
return qr;
}
}