标签:
/** * @param param * @author mercy 查询主副产品(主副产品是多对多的关系) * @return */ public String queryProductSpecAttr(JSONObject param){ ResultInfo result = new ResultInfo(0,"success",""); Grid grid = new Grid(); String productSpecId=param.getString("productSpecId"); ProductSpec ps=productSpecDao.findByProductSpecId(productSpecId); String prodCode=ps.getProdCode();//prodCode相同的话则可能存在主副产品 int prodType=ps.getProdType(); //主副产品标识(0:主产品,1:附属产品) //查当前产品属性 List<ProductSpecAttr> productSpecAttrList=productSpecAttrDao.findByProductSpecId(productSpecId); //产品属性List List<ProductDto> listPsaList=new ArrayList<ProductDto>() ; //主产品 if(prodType==0){ //主产品属性 ProductDto mainDto=new ProductDto(); mainDto.setMainList(productSpecAttrList); mainDto.setProductName(ps.getName()); mainDto.setProductSpecId(ps.getProductSpecId()); //存主主产品属性,名称 listPsaList.add(mainDto); //查出附属产品 List<ProductSpec> psSubList=productSpecDao.findByProdCodeAndProdType(prodCode,1); if(psSubList.size()>0){ //查附属产品数据 for(ProductSpec psl:psSubList){ Integer productId = psl.getId(); if(ps.getId()!=productId){ ProductDto subDto=new ProductDto(); List<ProductSpecAttr> psaSubList=productSpecAttrDao.findByProductSpecId(psl.getProductSpecId()); subDto.setSubList(psaSubList); subDto.setProductName(psl.getName()); subDto.setProductSpecId(psl.getProductSpecId()); //附属产品属性,名称 listPsaList.add(subDto); } } } //附属产品 }else if(prodType==1){ ProductDto subDto=new ProductDto(); List<ProductSpec> psMainList=productSpecDao.findByProdCodeAndProdType(prodCode,0); //存附属产品名称 subDto.setProductName(ps.getName()); subDto.setProductSpecId(ps.getProductSpecId()); //存附属产品属性 subDto.setSubList(productSpecAttrList); //存附属产品属性,名称 listPsaList.add(subDto); if(psMainList.size()>0){ //查主产品数据 for(ProductSpec psl:psMainList){ Integer productId = psl.getId(); if(ps.getId()!=productId){ ProductDto mainDto=new ProductDto(); List<ProductSpecAttr> psaMainList=productSpecAttrDao.findByProductSpecId(psl.getProductSpecId()); mainDto.setProductName(psl.getName()); mainDto.setProductSpecId(psl.getProductSpecId()); mainDto.setMainList(psaMainList); //主产品属性,名称 listPsaList.add(mainDto); } } } } grid.setContent(listPsaList); result.setResult_data(JSONObject.toJSONString(grid)); return JSONObject.toJSONString(result); }
上面这段代码输出的是一个主产品包含多个附属产品,1个主产品有多个产品属性,一个附属产品有多个产品属性
ProductDto类:
package com.eshore.ismp.product.dto; import java.util.List; import com.eshore.ismp.product.model.ProductSpecAttr; public class ProductDto { private String productName; private String productSpecId; private List<ProductSpecAttr> subList; private List<ProductSpecAttr> mainList; public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductSpecId() { return productSpecId; } public void setProductSpecId(String productSpecId) { this.productSpecId = productSpecId; } public List<ProductSpecAttr> getSubList() { return subList; } public void setSubList(List<ProductSpecAttr> subList) { this.subList = subList; } public List<ProductSpecAttr> getMainList() { return mainList; } public void setMainList(List<ProductSpecAttr> mainList) { this.mainList = mainList; } }
标签:
原文地址:http://www.cnblogs.com/JAYIT/p/5685916.html