public ProductVO model2VO(ProductModel model) { ProductVO vo = new ProductVO(); BeanUtils.copyProperties(model, vo); // 图片 ImgParam imgParam = new ImgParam(); imgParam.setToId(model.getId()); List<ImgModel> imgs = imgDao.getList(imgParam); if (!CollectionUtils.isEmpty(imgs)) { List<String> secondaryImgsUrl = new ArrayList<String>(); for (ImgModel img : imgs) { String url = URIUtils.getProductFullImgUrl(img.getName()); if (ImgType.SECONDARY.getValue().endsWith(img.getTypes())) { secondaryImgsUrl.add(url); } else if (ImgType.PRIMARY.getValue().endsWith(img.getTypes())) { vo.setPrimaryImgUrl(url); vo.setPrimaryImgId(img.getId()); } } vo.setSecondaryImgsUrl(secondaryImgsUrl); } // 如果没有主图,则设置一个默认值 if (StringUtils.isEmpty(vo.getPrimaryImgUrl())) { vo.setPrimaryImgUrl(URIUtils.getNoneFullImgUrl()); } // 详情页 vo.setDetailUrl(Constants.HOME_DOMAIN_URI + "product/detail?id=" + model.getId()); // 发布日期 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); vo.setGmtCreated(format.format(model.getGmtCreated())); return vo; }
@Override public List<ProductVO> getList(ProductParam param) { List<ProductVO> vos = new ArrayList<ProductVO>(); List<ProductModel> models = productDao.getList(param); List<Long> companyIds = new ArrayList<Long>(); List<Long> cityIds = new ArrayList<Long>(); List<Long> productIds = new ArrayList<Long>(); List<Long> provinceIds = new ArrayList<Long>(); if (!CollectionUtils.isEmpty(models)) { for (ProductModel model : models) { vos.add(model2VO(model)); productIds.add(model.getId()); // 公司 Long companyId = model.getCompanyId(); if (companyId != null && !companyIds.contains(companyId)) { companyIds.add(companyId); } // 城市 Long cityId = model.getCityId(); Long toCityId = model.getToCityId(); if (cityId != null && !cityIds.contains(cityId)) { cityIds.add(cityId); } if (toCityId != null && !cityIds.contains(toCityId)) { cityIds.add(toCityId); } // 省份 Long provinceId = model.getProvinceId(); Long toProvinceId = model.getToProvinceId(); if (provinceId != null && !provinceIds.contains(provinceId)) { provinceIds.add(provinceId); } if (toProvinceId != null && !provinceIds.contains(toProvinceId)) { provinceIds.add(toProvinceId); } } } // 公司 Map<Long, String> companyMap = new HashMap<Long, String>(); if (companyIds.size() > 0) { CompanyParam companyParam = new CompanyParam(); companyParam.setIds(companyIds); List<CompanyModel> companys = companyDao.getList(companyParam); for (CompanyModel company : companys) { companyMap.put(company.getId(), company.getName()); } } // 城市 Map<Long, String> cityMap = new HashMap<Long, String>(); if (cityIds.size() > 0) { CityParam cityParam = new CityParam(); cityParam.setIds(cityIds); List<CityModel> citys = cityDao.getList(cityParam); for (CityModel city : citys) { cityMap.put(city.getId(), city.getName()); } } // 省份 Map<Long, String> provinceMap = new HashMap<Long, String>(); if (provinceIds.size() > 0) { ProvinceParam provinceParam = new ProvinceParam(); provinceParam.setIds(provinceIds); List<ProvinceModel> provinces = provinceDao.getList(provinceParam); for (ProvinceModel province : provinces) { provinceMap.put(province.getId(), province.getName()); } } // 图片 Map<Long, ImgModel> primaryImgMap = new HashMap<Long, ImgModel>(); Map<Long, List<ImgModel>> secondaryImgMap = new HashMap<Long, List<ImgModel>>(); if (productIds.size() > 0) { ImgParam imgParam = new ImgParam(); // 如果查询多个商品,只查询附图,一般情况下,查询单个商品时才查询所有的图片 if (productIds.size() > 1) { imgParam.setTypes(ImgType.PRIMARY.getValue()); imgParam.setPageSize(50); } imgParam.setToIds(productIds); List<ImgModel> imgs = imgDao.getList(imgParam); if (!CollectionUtils.isEmpty(imgs)) { for (ImgModel img : imgs) { if (ImgType.SECONDARY.getValue().endsWith(img.getTypes())) { List<ImgModel> secondaryImgs = secondaryImgMap.get(img.getToId()); if (secondaryImgs == null) { secondaryImgs = new ArrayList<ImgModel>(); } secondaryImgs.add(img); secondaryImgMap.put(img.getToId(), secondaryImgs); } else if (ImgType.PRIMARY.getValue().endsWith(img.getTypes())) { primaryImgMap.put(img.getToId(), img); } } } } for (ProductVO productVO : vos) { // 公司名 productVO.setCompanyName(companyMap.get(productVO.getCompanyId())); // 城市 String city = cityMap.get(productVO.getCityId()); if (StringUtils.isNotBlank(city)) { productVO.setCityName(city + "市"); } // 配送城市 productVO.setToCityName(cityMap.get(productVO.getToCityId())); // 所在省份 String province = provinceMap.get(productVO.getProvinceId()); if (StringUtils.isNotBlank(province)) { productVO.setProvinceName(province + "省"); } // 配送省份 productVO.setToProvinceName(provinceMap.get(productVO.getToProvinceId())); // 主图 ImgModel primaryImg = primaryImgMap.get(productVO.getId()); if (primaryImg != null) { productVO.setPrimaryImgId(primaryImg.getId()); productVO.setPrimaryImgUrl(URIUtils.getProductFullImgUrl(primaryImg.getName())); } else { productVO.setPrimaryImgUrl(URIUtils.getNoneFullImgUrl()); } // 附图 List<ImgModel> secondaryImgs = secondaryImgMap.get(productVO.getId()); if (!CollectionUtils.isEmpty(secondaryImgs)) { List<String> secondaryImgsUrl = new ArrayList<String>(secondaryImgs.size()); for (ImgModel secondaryImg : secondaryImgs) { secondaryImgsUrl.add(URIUtils.getProductFullImgUrl(secondaryImg.getName())); } productVO.setSecondaryImgsUrl(secondaryImgsUrl); } } return vos; }