一坨代码的痛定思痛,我的眼睛真的近视 。(反思一)
package com.loveinway.service.impl; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.loveinway.common.entity.MallShopPlacesSetEntity; import com.loveinway.common.enums.CommonIsDeleteEnum; import com.loveinway.common.enums.LanguageDefaultEnum; import com.loveinway.common.exception.ErrorCode; import com.loveinway.common.page.PageBuilder; import com.loveinway.common.page.PageData; import com.loveinway.common.service.SysParamsService; import com.loveinway.common.service.impl.CrudServiceImpl; import com.loveinway.common.utils.Result; import com.loveinway.config.LanguageContextHolder; import com.loveinway.dao.MallShopGoodsCategoryDao; import com.loveinway.dao.MallShopProductDao; import com.loveinway.dto.MallShopProductDTO; import com.loveinway.dto.query.MallShopProductQuery; import com.loveinway.dto.query.MallShopSingleProductQuery; import com.loveinway.dto.request.RequestSysDynamicTranslationInfoDTO; import com.loveinway.dto.resp.*; import com.loveinway.entity.*; import com.loveinway.service.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * 门店商品表 * * @author * @since 2.0 2025-02-16 */ @Slf4j @Service public class MallShopProductServiceImpl extends CrudServiceImpl<mallshopproductdao, mallshopproductentity,="" mallshopproductdto=""> implements MallShopProductService { @Autowired private MallShopService mallShopService; @Autowired private SysUserService sysUserService; @Autowired private MallShopProductUnitService mallShopProductUnitService; @Autowired private MallShopProductMediaService mallShopProductMediaService; @Autowired private SysNumSeqService sysNumSeqService; @Autowired private MallShopGoodsCategoryService mallShopGoodsCategoryService; @Autowired private MallShopGoodsCategoryDao mallShopGoodsCategoryDao; @Autowired private PmsGoodsService pmsGoodsService; @Autowired private PmsGoodsSkuService pmsGoodsSkuService; @Autowired private PmsGoodsSkuUnitService pmsGoodsSkuUnitService; @Autowired private PmsGoodsCategoryService pmsGoodsCategoryService; @Autowired private MallShopUnitService mallShopUnitService; @Autowired private PmsGoodsUnitService pmsGoodsUnitService; @Autowired private SysParamsService sysParamsService; @Autowired private SysDynamicTranslationService sysDynamicTranslationService; @Autowired private CusCustomerGradePriceService cusCustomerGradePriceService; @Override public QueryWrapper getWrapper(Map<string, object=""> params) { QueryWrapper wrapper = new QueryWrapper<>(); String goodsName = (String) params.get( "goodsName" ); wrapper.like(StringUtils.isNotBlank(goodsName), "goods_name" , goodsName); String shopId = (String) params.get( "shopId" ); wrapper.eq(StringUtils.isNotBlank(shopId), "shop_id" , shopId); String enabled = (String) params.get( "enabled" ); wrapper.eq(StringUtils.isNotBlank(enabled), "enabled" , enabled); wrapper.eq( "del_flag" , 0 ); wrapper.orderByDesc( "sort_code" , "create_date" ); return wrapper; } @Override public PageData findMallShopProductPage(HttpServletRequest request, MallShopProductQuery query){ // 商品分类,如果传递的是第一级,需要把它下面的子集都查询出来 if ( null != query.getCategoryId()) { query.getCategoryIdList().add(query.getCategoryId()); // 需要查询当前值是不是父类,如果是需要把它下面的子集都查询出来 MallShopGoodsCategoryEntity mallShopGoodsCategoryEntity = mallShopGoodsCategoryService.selectById(query.getCategoryId()); if ( null != mallShopGoodsCategoryEntity && mallShopGoodsCategoryEntity.getParentId().equals(0L)) { List mallShopGoodsCategoryEntityList = mallShopGoodsCategoryService.selectSecCategoryListByFirCategoryId(mallShopGoodsCategoryEntity.getId()); if (CollectionUtils.isNotEmpty(mallShopGoodsCategoryEntityList)) { for (MallShopGoodsCategoryEntity temp : mallShopGoodsCategoryEntityList) { query.getCategoryIdList().add(temp.getId()); } } } } IPage page = new Page<>(query.getPage(), query.getLimit()); String languageCode = LanguageContextHolder.getLanguage(); if (StringUtils.isNotEmpty(query.getGoodsName())){ List entityIdList = new ArrayList<>(); List sysDynamicTranslationList = sysDynamicTranslationService.listByFieldSearch( "mall_shop_product" , languageCode, "goodsName" ,query.getGoodsName()); if (CollectionUtils.isNotEmpty(sysDynamicTranslationList)){ entityIdList = sysDynamicTranslationList.stream().map(SysDynamicTranslationEntity::getEntityId).collect(Collectors.toList()); } query.getEntityIdList().addAll(entityIdList); } List selectList = baseDao.findMallShopProductPage(page, query); if (CollectionUtils.isNotEmpty(selectList)) { Set userIdSet = new HashSet<>(); Set shopIdSet = new HashSet<>(); Set shopGoodsIdSet = new HashSet<>(); Set categoryIdSet = new HashSet<>(); Set goodsInfoIdSet = new HashSet<>(); Set unitIdSet = new HashSet<>(); for (RespMallShopProductPageDTO temp : selectList) { categoryIdSet.add(temp.getCategoryId()); shopGoodsIdSet.add(temp.getId()); userIdSet.add(temp.getCreator()); userIdSet.add(temp.getUpdater()); shopIdSet.add(temp.getShopId()); } List mallShopProductUnitEntityList1 = mallShopProductUnitService.getMallShopProductUnitByGoodsIdList( new ArrayList<>(shopGoodsIdSet)); if (CollectionUtils.isNotEmpty(mallShopProductUnitEntityList1)) { for (MallShopProductUnitEntity mallShopProductUnit : mallShopProductUnitEntityList1) { goodsInfoIdSet.add(mallShopProductUnit.getId()); unitIdSet.add(mallShopProductUnit.getUnitId()); } } // 获取当前用户选择的语言 Map<string,sysdynamictranslationentity> sysDynamicTranslationEntityMap = sysDynamicTranslationService.mapByIdsTypeLangField( new ArrayList<>(shopGoodsIdSet), "mall_shop_product" ,languageCode, "goodsName" ); Map<string,sysdynamictranslationentity> sysDynamicTranslationGoodsDescMap = sysDynamicTranslationService.mapByIdsTypeLangField( new ArrayList<>(shopGoodsIdSet), "mall_shop_product" ,languageCode, "goodsDesc" ); Map<string,sysdynamictranslationentity> sysDynamicTranslationGoodsInfoDescMap = sysDynamicTranslationService.mapByIdsTypeLangField( new ArrayList<>(shopGoodsIdSet), "mall_shop_product" ,languageCode, "goodsInfoDesc" ); // 云商品库的单位map Map<long, mallshopunitentity=""> mallShopUnitEntityMap = mallShopUnitService.selectMallShopUnitMapByUnitIdList( new ArrayList<>(unitIdSet)); // 门店商品规格-国际化 Map<string,sysdynamictranslationentity> sysDynamicTranslationGoodsInfoSkuNameMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(goodsInfoIdSet)){ sysDynamicTranslationGoodsInfoSkuNameMap = sysDynamicTranslationService.mapByIdsTypeLangField( new ArrayList<>(goodsInfoIdSet), "mall_shop_product_unit" ,languageCode, "skuName" ); } // 还需要获取一级分类的id,根据二级分类来查询 Set categoryFirIdSet = new HashSet<>(); List mallShopGoodsCategoryEntityList = mallShopGoodsCategoryDao.getPmsGoodsCategoryListByCategoryIdList( new ArrayList<>(categoryIdSet)); if (CollectionUtils.isNotEmpty(mallShopGoodsCategoryEntityList)) { for (MallShopGoodsCategoryEntity temp : mallShopGoodsCategoryEntityList) { if (temp.getParentId() != 0 ) { categoryFirIdSet.add(temp.getParentId()); } } } // 商品一二级分类 Map<string, sysdynamictranslationentity=""> sysDynamicTranslationFirMap = sysDynamicTranslationService.mapByIdsTypeLangField( new ArrayList<>(categoryFirIdSet), "pms_goods_category" , languageCode, "categoryName" ); Map<string, sysdynamictranslationentity=""> sysDynamicTranslationCategorySecMap = sysDynamicTranslationService.mapByIdsTypeLangField( new ArrayList<>(categoryIdSet), "pms_goods_category" , languageCode, "categoryName" ); // 系统用户 Map<long, sysuserentity=""> sysUserEntityMap = sysUserService.getSysUserMapByUserIdList( new ArrayList<>(userIdSet)); // 门店 Map<long, mallshopentity=""> mallShopEntityMap = mallShopService.getMallShopMapByShopIdList( new ArrayList<>(shopIdSet), 0 ); // 商品规格集合map Map<long,list> mallShopProductUnitMap = mallShopProductUnitService.getMallShopProductUnitMapByGoodsIdList( new ArrayList<>(shopGoodsIdSet)); // 商品分类获取对应的 二级分类map Map<long, mallshopgoodscategoryentity=""> mallShopGoodsCategoryEntityMap = mallShopGoodsCategoryService.selectCategoryMapByIdList( new ArrayList<>(categoryIdSet)); // 商品分类获取对应的 一级分类map Map<long, mallshopgoodscategoryentity=""> mallShopGoodsCategoryEntityMap1 = mallShopGoodsCategoryService.selectFirstCategoryListBySecCategoryIdList( new ArrayList<>(categoryIdSet)); // 商品图片map Map<long, list<mallshopproductmediaentity="">> mallShopProductMediaMap = mallShopProductMediaService.getMallShopProductMediaMapByShopGoodsIdList( new ArrayList<>(shopGoodsIdSet), 1 ); // 获取系统对应的小数点位对象 MallShopPlacesSetEntity mallShopPlacesSetEntity = sysParamsService.getMallShopPlacesSet(); for (RespMallShopProductPageDTO temp : selectList) { // 加载动态字段国际化 if ( null != sysDynamicTranslationEntityMap) { StringBuffer sbf = new StringBuffer(); sbf.append(temp.getId()).append( "-" ).append( "mall_shop_product" ).append( "-" ).append(languageCode).append( "-" ).append( "goodsName" ); SysDynamicTranslationEntity sysDynamicTranslationEntity = sysDynamicTranslationEntityMap.get(sbf.toString()); if ( null != sysDynamicTranslationEntity){ temp.setGoodsName(sysDynamicTranslationEntity.getFieldValue()); } } if ( null != sysDynamicTranslationGoodsDescMap) { StringBuffer sbf = new StringBuffer(); sbf.append(temp.getId()).append( "-" ).append( "mall_shop_product" ).append( "-" ).append(languageCode).append( "-" ).append( "goodsDesc" ); SysDynamicTranslationEntity sysDynamicTranslationEntity = sysDynamicTranslationGoodsDescMap.get(sbf.toString()); if ( null != sysDynamicTranslationEntity){ temp.setGoodsDesc(sysDynamicTranslationEntity.getFieldValue()); } } if ( null != sysDynamicTranslationGoodsInfoDescMap) { StringBuffer sbf = new StringBuffer(); sbf.append(temp.getId()).append( "-" ).append( "mall_shop_product" ).append( "-" ).append(languageCode).append( "-" ).append( "goodsInfoDesc" ); SysDynamicTranslationEntity sysDynamicTranslationEntity = sysDynamicTranslationGoodsInfoDescMap.get(sbf.toString()); if ( null != sysDynamicTranslationEntity){ temp.setGoodsInfoDesc(sysDynamicTranslationEntity.getFieldValue()); } } // 创建人 if ( null != sysUserEntityMap && !sysUserEntityMap.isEmpty()) { SysUserEntity sysUser = sysUserEntityMap.get(temp.getCreator()); if ( null != sysUser) { temp.setCreatorName(sysUser.getRealName()); } } // 店铺 if ( null != mallShopEntityMap && !mallShopEntityMap.isEmpty()) { MallShopEntity shopEntity = mallShopEntityMap.get(temp.getShopId()); if ( null != shopEntity) { temp.setShopName(shopEntity.getShopName()); } } if ( null != mallShopProductUnitMap && !mallShopProductUnitMap.isEmpty()) { // 商品规格集合 List mallShopProductUnitDTOList = new ArrayList<>(); // 循环map List itemMap = mallShopProductUnitMap.get(temp.getId()); if (CollectionUtils.isNotEmpty(itemMap)) { for (MallShopProductUnitEntity mallShopProductUnit : itemMap) { RespMallShopProductUnitDTO respMallShopProductUnitDTO = new RespMallShopProductUnitDTO(); BeanUtils.copyProperties(mallShopProductUnit, respMallShopProductUnitDTO); if ( null != sysDynamicTranslationGoodsInfoSkuNameMap) { StringBuffer sbf = new StringBuffer(); sbf.append(temp.getId()).append( "-" ).append( "mall_shop_product_unit" ).append( "-" ).append(languageCode).append( "-" ).append( "skuName" ); SysDynamicTranslationEntity sysDynamicTranslationEntity = sysDynamicTranslationGoodsInfoSkuNameMap.get(sbf.toString()); if ( null != sysDynamicTranslationEntity){ respMallShopProductUnitDTO.setSkuName(sysDynamicTranslationEntity.getFieldValue()); } } // 商品单位 if ( null != mallShopUnitEntityMap && !mallShopUnitEntityMap.isEmpty()) { MallShopUnitEntity mallShopUnitEntity = mallShopUnitEntityMap.get(respMallShopProductUnitDTO.getUnitId()); if ( null != mallShopUnitEntity) { respMallShopProductUnitDTO.setUnitName(mallShopUnitEntity.getUnitName()); respMallShopProductUnitDTO.setMeterType(mallShopUnitEntity.getMeterType()); } } // roundingMethod 金额计算方式 0=四舍五入 1=舍入法 if ( null != mallShopPlacesSetEntity && mallShopPlacesSetEntity.getRoundingMethod().equals( 0 )) { respMallShopProductUnitDTO.setUnitPrice( new BigDecimal(mallShopProductUnit.getUnitPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setCostPrice( new BigDecimal(mallShopProductUnit.getCostPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setMarketPrice( new BigDecimal(mallShopProductUnit.getMarketPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitPrice2( new BigDecimal(mallShopProductUnit.getUnitPrice2().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitRelation( new BigDecimal(mallShopProductUnit.getUnitRelation().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setVirtualQuantity( new BigDecimal(mallShopProductUnit.getVirtualQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setStockQuantity( new BigDecimal(mallShopProductUnit.getStockQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); } else { respMallShopProductUnitDTO.setUnitPrice( new BigDecimal(mallShopProductUnit.getUnitPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setCostPrice( new BigDecimal(mallShopProductUnit.getCostPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setMarketPrice( new BigDecimal(mallShopProductUnit.getMarketPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitPrice2( new BigDecimal(mallShopProductUnit.getUnitPrice2().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitRelation( new BigDecimal(mallShopProductUnit.getUnitRelation().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setVirtualQuantity( new BigDecimal(mallShopProductUnit.getVirtualQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setStockQuantity( new BigDecimal(mallShopProductUnit.getStockQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); } mallShopProductUnitDTOList.add(respMallShopProductUnitDTO); } temp.getMallShopProductUnitDTOList().addAll(mallShopProductUnitDTOList); } } // 商品分类 一级和二级名称 MallShopGoodsCategoryEntity mallShopGoodsCategory = mallShopGoodsCategoryEntityMap.get(temp.getCategoryId()); if ( null != mallShopGoodsCategory) { SysDynamicTranslationEntity sysDynamicTranslation = sysDynamicTranslationService.getOne(temp.getCategoryId(), "mall_shop_goods_category" ,languageCode, "categoryName" ); if ( null != sysDynamicTranslation){ temp.setCategoryName(sysDynamicTranslation.getFieldValue()); } else { temp.setCategoryName(mallShopGoodsCategory.getCategoryName()); } MallShopGoodsCategoryEntity mallShopGoodsCategoryEntity = mallShopGoodsCategoryEntityMap1.get(temp.getCategoryId()); if ( null != mallShopGoodsCategoryEntity) { temp.setCategoryFirstId(mallShopGoodsCategoryEntity.getId()); SysDynamicTranslationEntity sysDynamicTranslation2 = sysDynamicTranslationService.getOne(temp.getCategoryId(), "mall_shop_goods_category" ,languageCode, "categoryName" ); if ( null != sysDynamicTranslation2){ temp.setCategoryNameFirst(sysDynamicTranslation2.getFieldValue()); } else { temp.setCategoryNameFirst(mallShopGoodsCategoryEntity.getCategoryName()); } } } // 商品图片 if ( null != mallShopProductMediaMap && !mallShopProductMediaMap.isEmpty()) { List mallShopProductMediaEntityList = mallShopProductMediaMap.get(temp.getId()); if (CollectionUtils.isNotEmpty(mallShopProductMediaEntityList)) { temp.setGoodsImage(mallShopProductMediaEntityList.get( 0 ).getMediaUrl()); } } } } return PageBuilder.copyAndConvert(page, selectList); } /** * 门店商品规格+单位-分页 * * @param query * @return */ @Override public PageData findMallShopSingleProductPage(MallShopSingleProductQuery query){ // 商品分类,如果传递的是第一级,需要把它下面的子集都查询出来 if ( null != query.getCategoryId()) { query.getCategoryIdList().add(query.getCategoryId()); // 需要查询当前值是不是父类,如果是需要把它下面的子集都查询出来 MallShopGoodsCategoryEntity mallShopGoodsCategoryEntity = mallShopGoodsCategoryService.selectById(query.getCategoryId()); if ( null != mallShopGoodsCategoryEntity && mallShopGoodsCategoryEntity.getParentId().equals(0L)) { List mallShopGoodsCategoryEntityList = mallShopGoodsCategoryService.selectSecCategoryListByFirCategoryId(mallShopGoodsCategoryEntity.getId()); if (CollectionUtils.isNotEmpty(mallShopGoodsCategoryEntityList)) { for (MallShopGoodsCategoryEntity temp : mallShopGoodsCategoryEntityList) { query.getCategoryIdList().add(temp.getId()); } } } } IPage page = new Page<>(query.getPage(), query.getLimit()); List selectList = baseDao.findMallShopSingleProductPage(page, query); if (CollectionUtils.isNotEmpty(selectList)) { String languageCode = LanguageContextHolder.getLanguage(); Set userIdSet = new HashSet<>(); Set shopIdSet = new HashSet<>(); Set shopGoodsIdSet = new HashSet<>(); Set categoryIdSet = new HashSet<>(); Set unitIdSet = new HashSet<>(); for (RespMallShopSingleProductPageDTO temp : selectList) { categoryIdSet.add(temp.getCategoryId()); shopGoodsIdSet.add(temp.getGoodsId()); shopIdSet.add(temp.getShopId()); unitIdSet.add(temp.getUnitId()); } // 门店商品单位 Map<long, mallshopunitentity=""> mallShopUnitEntityMap = mallShopUnitService.selectGoodsUnitMapByUnitIdList( new ArrayList<>(unitIdSet)); // 供应链中心商品单位 Map<long, pmsgoodsunitentity=""> pmsGoodsUnitEntityMap = pmsGoodsUnitService.selectGoodsUnitMapByUnitIdList( new ArrayList<>(unitIdSet),languageCode); // 门店 Map<long, mallshopentity=""> mallShopEntityMap = mallShopService.getMallShopMapByShopIdList( new ArrayList<>(shopIdSet), 0 ); // 商品规格集合map Map<long,list> mallShopProductUnitMap = mallShopProductUnitService.getMallShopProductUnitMapByGoodsIdList( new ArrayList<>(shopGoodsIdSet)); // 商品分类获取对应的 二级分类map Map<long, mallshopgoodscategoryentity=""> mallShopGoodsCategoryEntityMap = mallShopGoodsCategoryService.selectCategoryMapByIdList( new ArrayList<>(categoryIdSet)); // 商品分类获取对应的 一级分类map Map<long, mallshopgoodscategoryentity=""> mallShopGoodsCategoryEntityMap1 = mallShopGoodsCategoryService.selectFirstCategoryListBySecCategoryIdList( new ArrayList<>(categoryIdSet)); // 商品图片map Map<long, list<mallshopproductmediaentity="">> pmsGoodsMediaByGoodsIdListMap = mallShopProductMediaService.getMallShopProductMediaMapByShopGoodsIdList( new ArrayList<>(shopGoodsIdSet), 1 ); for (RespMallShopSingleProductPageDTO temp : selectList) { // 店铺 if ( null != mallShopEntityMap && !mallShopEntityMap.isEmpty()) { MallShopEntity shopEntity = mallShopEntityMap.get(temp.getShopId()); if ( null != shopEntity) { temp.setShopName(shopEntity.getShopName()); } } // 商品单位 if ( null != temp.getSourceType()) { // 来源类型 0=自创 1=门店同步 2=供应链同步 if (temp.getSourceType().equals( 0 ) || temp.getSourceType().equals( 1 )) { // 商品单位 if ( null != mallShopUnitEntityMap && !mallShopUnitEntityMap.isEmpty()) { MallShopUnitEntity mallShopUnitEntity = mallShopUnitEntityMap.get(temp.getUnitId()); if ( null != mallShopUnitEntity) { temp.setUnitName(mallShopUnitEntity.getUnitName()); temp.setMeterType(mallShopUnitEntity.getMeterType()); } } } else { // 商品单位 if ( null != pmsGoodsUnitEntityMap && !pmsGoodsUnitEntityMap.isEmpty()) { PmsGoodsUnitEntity pmsGoodsUnit = pmsGoodsUnitEntityMap.get(temp.getUnitId()); if ( null != pmsGoodsUnit) { temp.setUnitName(pmsGoodsUnit.getUnitName()); temp.setMeterType(pmsGoodsUnit.getMeterType()); } } } } // 商品分类 一级和二级名称 if ( null != mallShopGoodsCategoryEntityMap && null != temp.getCategoryId()) { MallShopGoodsCategoryEntity mallShopGoodsCategory = mallShopGoodsCategoryEntityMap.get(temp.getCategoryId()); if ( null != mallShopGoodsCategory) { temp.setCategoryName(mallShopGoodsCategory.getCategoryName()); MallShopGoodsCategoryEntity mallShopGoodsCategoryEntity = mallShopGoodsCategoryEntityMap1.get(temp.getCategoryId()); if ( null != mallShopGoodsCategoryEntity) { temp.setCategoryNameFirst(mallShopGoodsCategoryEntity.getCategoryName()); } } } // 商品图片 if ( null != pmsGoodsMediaByGoodsIdListMap && !pmsGoodsMediaByGoodsIdListMap.isEmpty()) { List mallShopProductMediaEntityList = pmsGoodsMediaByGoodsIdListMap.get(temp.getGoodsId()); if (CollectionUtils.isNotEmpty(mallShopProductMediaEntityList)) { temp.setGoodsImage(mallShopProductMediaEntityList.get( 0 ).getMediaUrl()); } } } } return PageBuilder.copyAndConvert(page, selectList); } /** * 门店商品-详情 * @param id * @return */ @Override public Result getMallShopProductInfo(HttpServletRequest request, Long id) { Result result = new Result(); if ( null == id) { result.setCode(ErrorCode.PARAM_IS_NULL); result.setMsg( "门店商品id不能为空" ); return result; } MallShopProductEntity mallShopProductEntity = baseDao.selectById(id); if ( null == mallShopProductEntity) { result.setCode(ErrorCode.DATA_NOT_EXIST); result.setMsg( "门店商品不存在" ); return result; } RespMallShopProductDTO respMallShopProductDTO = new RespMallShopProductDTO(); BeanUtils.copyProperties(mallShopProductEntity, respMallShopProductDTO); String languageCode = LanguageContextHolder.getLanguage(); Set userIdSet = new HashSet<>(); Set shopIdSet = new HashSet<>(); Set goodsIdSet = new HashSet<>(); Set shopGoodsIdSet = new HashSet<>(); Set categoryIdSet = new HashSet<>(); userIdSet.add(mallShopProductEntity.getCreator()); shopIdSet.add(mallShopProductEntity.getShopId()); shopGoodsIdSet.add(mallShopProductEntity.getId()); goodsIdSet.add(mallShopProductEntity.getGoodsId()); categoryIdSet.add(mallShopProductEntity.getCategoryId()); // 系统用户 Map<long, sysuserentity=""> sysUserEntityMap = sysUserService.getSysUserMapByUserIdList( new ArrayList<>(userIdSet)); // 门店 Map<long, mallshopentity=""> mallShopEntityMap = mallShopService.getMallShopMapByShopIdList( new ArrayList<>(shopIdSet), 0 ); // 商品图片map Map<long, list<mallshopproductmediaentity="">> mallShopProductMediaMap = mallShopProductMediaService.getMallShopProductMediaMapByShopGoodsIdList( new ArrayList<>(shopGoodsIdSet), 1 ); // 商品规格集合 List mallShopProductUnitEntityList = mallShopProductUnitService.getMallShopProductUnitByGoodsIdList( new ArrayList<>(shopGoodsIdSet)); // 商品分类获取对应的 二级分类map Map<long, mallshopgoodscategoryentity=""> mallShopGoodsCategoryEntityMap = mallShopGoodsCategoryService.selectCategoryMapByIdList( new ArrayList<>(categoryIdSet)); // 商品分类获取对应的 一级分类map Map<long, mallshopgoodscategoryentity=""> mallShopGoodsCategoryEntityMap1 = mallShopGoodsCategoryService.selectFirstCategoryListBySecCategoryIdList( new ArrayList<>(categoryIdSet)); SysDynamicTranslationEntity sysDynamicTranslationGoodsName = sysDynamicTranslationService.getOne(mallShopProductEntity.getId(), "mall_shop_product" ,languageCode, "goodsName" ); if ( null != sysDynamicTranslationGoodsName){ respMallShopProductDTO.setGoodsName(sysDynamicTranslationGoodsName.getFieldValue()); } SysDynamicTranslationEntity sysDynamicTranslationGoodsDesc = sysDynamicTranslationService.getOne(mallShopProductEntity.getId(), "mall_shop_product" ,languageCode, "goodsDesc" ); if ( null != sysDynamicTranslationGoodsDesc){ respMallShopProductDTO.setGoodsDesc(sysDynamicTranslationGoodsDesc.getFieldValue()); } SysDynamicTranslationEntity sysDynamicTranslationInfoDesc = sysDynamicTranslationService.getOne(mallShopProductEntity.getId(), "mall_shop_product" ,languageCode, "goodsInfoDesc" ); if ( null != sysDynamicTranslationInfoDesc){ respMallShopProductDTO.setGoodsInfoDesc(sysDynamicTranslationInfoDesc.getFieldValue()); } // 店铺 if ( null != mallShopEntityMap && !mallShopEntityMap.isEmpty()) { MallShopEntity mallShopEntity = mallShopEntityMap.get(mallShopProductEntity.getShopId()); if ( null != mallShopEntity) { respMallShopProductDTO.setShopName(mallShopEntity.getShopName()); } } // 创建人 if ( null != sysUserEntityMap && !sysUserEntityMap.isEmpty()) { SysUserEntity sysUser = sysUserEntityMap.get(respMallShopProductDTO.getCreator()); if ( null != sysUser) { respMallShopProductDTO.setCreatorName(sysUser.getRealName()); } } // 商品分类 一级和二级名称 if ( null != mallShopGoodsCategoryEntityMap && null != respMallShopProductDTO.getCategoryId()) { MallShopGoodsCategoryEntity mallShopGoodsCategory = mallShopGoodsCategoryEntityMap.get(respMallShopProductDTO.getCategoryId()); if ( null != mallShopGoodsCategory) { SysDynamicTranslationEntity sysDynamicTranslation = sysDynamicTranslationService.getOne(mallShopProductEntity.getId(), "mall_shop_goods_category" ,languageCode, "categoryName" ); if ( null != sysDynamicTranslation){ respMallShopProductDTO.setCategoryName(sysDynamicTranslation.getFieldValue()); } else { respMallShopProductDTO.setCategoryName(mallShopGoodsCategory.getCategoryName()); } MallShopGoodsCategoryEntity mallShopGoodsCategoryEntity = mallShopGoodsCategoryEntityMap1.get(respMallShopProductDTO.getCategoryId()); if ( null != mallShopGoodsCategoryEntity) { respMallShopProductDTO.setCategoryFirstId(mallShopGoodsCategoryEntity.getId()); SysDynamicTranslationEntity sysDynamicTranslation2 = sysDynamicTranslationService.getOne(mallShopProductEntity.getId(), "mall_shop_goods_category" ,languageCode, "categoryName" ); if ( null != sysDynamicTranslation2){ respMallShopProductDTO.setCategoryNameFirst(sysDynamicTranslation2.getFieldValue()); } else { respMallShopProductDTO.setCategoryNameFirst(mallShopGoodsCategoryEntity.getCategoryName()); } } } } // 商品信息-国际化 List sysDynamicTranslationDTOList = new ArrayList<>(); // 查询动态翻译数据 List sysDynamicTranslationBrandNameList = sysDynamicTranslationService.listById(mallShopProductEntity.getId(), "mall_shop_product" ); if (CollectionUtils.isNotEmpty(sysDynamicTranslationBrandNameList)) { for (SysDynamicTranslationEntity temp : sysDynamicTranslationBrandNameList) { RequestSysDynamicTranslationInfoDTO requestSysDynamicTranslationDTO = new RequestSysDynamicTranslationInfoDTO(); BeanUtils.copyProperties(temp,requestSysDynamicTranslationDTO); sysDynamicTranslationDTOList.add(requestSysDynamicTranslationDTO); } } RequestSysDynamicTranslationInfoDTO requestSysDynamicTranslationGoodsName = new RequestSysDynamicTranslationInfoDTO(); requestSysDynamicTranslationGoodsName.setEntityId(mallShopProductEntity.getId()); requestSysDynamicTranslationGoodsName.setEntityType( "mall_shop_product" ); requestSysDynamicTranslationGoodsName.setFieldName( "goodsName" ); requestSysDynamicTranslationGoodsName.setFieldValue(mallShopProductEntity.getGoodsName()); requestSysDynamicTranslationGoodsName.setLanguageCode(LanguageDefaultEnum.LANGUAGE_DEFAULT.getName()); RequestSysDynamicTranslationInfoDTO requestSysDynamicTranslationGoodsDesc = new RequestSysDynamicTranslationInfoDTO(); requestSysDynamicTranslationGoodsDesc.setEntityId(mallShopProductEntity.getId()); requestSysDynamicTranslationGoodsDesc.setEntityType( "mall_shop_product" ); requestSysDynamicTranslationGoodsDesc.setFieldName( "goodsDesc" ); requestSysDynamicTranslationGoodsDesc.setFieldValue(mallShopProductEntity.getGoodsDesc()); requestSysDynamicTranslationGoodsDesc.setLanguageCode(LanguageDefaultEnum.LANGUAGE_DEFAULT.getName()); RequestSysDynamicTranslationInfoDTO requestSysDynamicTranslationGoodsDescM = new RequestSysDynamicTranslationInfoDTO(); requestSysDynamicTranslationGoodsDescM.setEntityId(mallShopProductEntity.getId()); requestSysDynamicTranslationGoodsDescM.setEntityType( "mall_shop_product" ); requestSysDynamicTranslationGoodsDescM.setFieldName( "goodsInfoDesc" ); requestSysDynamicTranslationGoodsDescM.setFieldValue(mallShopProductEntity.getGoodsInfoDesc()); requestSysDynamicTranslationGoodsDescM.setLanguageCode(LanguageDefaultEnum.LANGUAGE_DEFAULT.getName()); sysDynamicTranslationDTOList.add(requestSysDynamicTranslationGoodsName); sysDynamicTranslationDTOList.add(requestSysDynamicTranslationGoodsDesc); sysDynamicTranslationDTOList.add(requestSysDynamicTranslationGoodsDescM); respMallShopProductDTO.setSysDynamicTranslationList(sysDynamicTranslationDTOList); // 商品规格集合 List mallShopProductUnitDTOList = new ArrayList<>(); // 商品图片集合 List mallShopProductMediaDTODTOList = new ArrayList<>(); // 商品视频集合 List mallShopProductVideoDTODTOList = new ArrayList<>(); // 规格名称国际化 Map<string,sysdynamictranslationentity> sysDynamicTranslationEntityskuNameMap = sysDynamicTranslationService.mapByIdsTypeLangField( new ArrayList<>(shopGoodsIdSet), "mall_shop_product_unit" ,languageCode, "skuName" ); // 获取系统对应的小数点位对象 MallShopPlacesSetEntity mallShopPlacesSetEntity = sysParamsService.getMallShopPlacesSet(); if (CollectionUtils.isNotEmpty(mallShopProductUnitEntityList)){ Set unitIdSet = new HashSet<>(); for (MallShopProductUnitEntity productUnit : mallShopProductUnitEntityList){ unitIdSet.add(productUnit.getUnitId()); } Map<long, mallshopunitentity=""> mallShopUnitEntityMap = mallShopUnitService.selectMallShopUnitMapByUnitIdList( new ArrayList<>(unitIdSet)); for (MallShopProductUnitEntity productUnit : mallShopProductUnitEntityList){ RespMallShopProductUnitDTO respMallShopProductUnitDTO = new RespMallShopProductUnitDTO(); BeanUtils.copyProperties(productUnit,respMallShopProductUnitDTO); // roundingMethod 金额计算方式 0=四舍五入 1=舍入法 if ( null != mallShopPlacesSetEntity && mallShopPlacesSetEntity.getRoundingMethod().equals( 0 )) { respMallShopProductUnitDTO.setUnitPrice( new BigDecimal(productUnit.getUnitPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setCostPrice( new BigDecimal(productUnit.getCostPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setMarketPrice( new BigDecimal(productUnit.getMarketPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitPrice2( new BigDecimal(productUnit.getUnitPrice2().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitRelation( new BigDecimal(productUnit.getUnitRelation().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setVirtualQuantity( new BigDecimal(productUnit.getVirtualQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setStockQuantity( new BigDecimal(productUnit.getStockQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString())); } else { respMallShopProductUnitDTO.setUnitPrice( new BigDecimal(productUnit.getUnitPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setCostPrice( new BigDecimal(productUnit.getCostPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setMarketPrice( new BigDecimal(productUnit.getMarketPrice().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitPrice2( new BigDecimal(productUnit.getUnitPrice2().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setUnitRelation( new BigDecimal(productUnit.getUnitRelation().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setVirtualQuantity( new BigDecimal(productUnit.getVirtualQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); respMallShopProductUnitDTO.setStockQuantity( new BigDecimal(productUnit.getStockQuantity().setScale(mallShopPlacesSetEntity.getAmountDecimal(), BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString())); } if ( null != mallShopUnitEntityMap){ MallShopUnitEntity mallShopUnitEntity = mallShopUnitEntityMap.get(productUnit.getUnitId()); if ( null != mallShopUnitEntity){ respMallShopProductUnitDTO.setUnitName(mallShopUnitEntity.getUnitName()); } } // 查询动态翻译数据 List sysDynamicTranslationSkuList = sysDynamicTranslationService.listById(productUnit.getId(), "mall_shop_product_unit" ); if (CollectionUtils.isNotEmpty(sysDynamicTranslationSkuList)) { // 商品规格-国际化 List sysDynamicTranslationInfoList = new ArrayList<>(); for (SysDynamicTranslationEntity temp : sysDynamicTranslationSkuList) { RequestSysDynamicTranslationInfoDTO requestSysDynamicTranslationDTO = new RequestSysDynamicTranslationInfoDTO(); BeanUtils.copyProperties(temp,requestSysDynamicTranslationDTO); sysDynamicTranslationInfoList.add(requestSysDynamicTranslationDTO); } RequestSysDynamicTranslationInfoDTO requestSysDynamicTranslationSkuName = new RequestSysDynamicTranslationInfoDTO(); requestSysDynamicTranslationSkuName.setEntityId(productUnit.getId()); requestSysDynamicTranslationSkuName.setEntityType( "mall_shop_product_unit" ); requestSysDynamicTranslationSkuName.setFieldName( "skuName" ); requestSysDynamicTranslationSkuName.setFieldValue(productUnit.getSkuName()); requestSysDynamicTranslationSkuName.setLanguageCode(LanguageDefaultEnum.LANGUAGE_DEFAULT.getName()); sysDynamicTranslationInfoList.add(requestSysDynamicTranslationSkuName); respMallShopProductUnitDTO.setSysDynamicTranslationList(sysDynamicTranslationInfoList); } // 除了默认语言外的其他国际化数据 if (!LanguageDefaultEnum.LANGUAGE_DEFAULT.getName().equals(languageCode)) { // 商品名称 // 加载动态字段国际化 if ( null != sysDynamicTranslationEntityskuNameMap) { StringBuffer sbf2 = new StringBuffer(); sbf2.append(productUnit.getId()).append( "-" ).append( "mall_shop_product_unit" ).append( "-" ).append(languageCode).append( "-" ).append( "skuName" ); SysDynamicTranslationEntity sysDynamicTranslationEntity = sysDynamicTranslationEntityskuNameMap.get(sbf2.toString()); if ( null != sysDynamicTranslationEntity) { respMallShopProductUnitDTO.setSkuName(sysDynamicTranslationEntity.getFieldValue()); } } } mallShopProductUnitDTOList.add(respMallShopProductUnitDTO); } } if ( null != mallShopProductMediaMap && mallShopProductMediaMap.size() > 0 ) { // 循环map for (Map.Entry<long, list<mallshopproductmediaentity="">> itemMap : mallShopProductMediaMap.entrySet()) { if (CollectionUtils.isNotEmpty(itemMap.getValue())){ for (MallShopProductMediaEntity temp : itemMap.getValue()){ // 图片:1,视频,2 if (temp.getMediaType().equals( 1 )) { RespMallShopProductMediaDTO respMallShopProductMediaDTO = new RespMallShopProductMediaDTO(); BeanUtils.copyProperties(temp, respMallShopProductMediaDTO); mallShopProductMediaDTODTOList.add(respMallShopProductMediaDTO); } else { RespMallShopProductMediaDTO respMallShopProductMediaDTO = new RespMallShopProductMediaDTO(); BeanUtils.copyProperties(temp, respMallShopProductMediaDTO); mallShopProductVideoDTODTOList.add(respMallShopProductMediaDTO); } } } } } // 商品图片和视频 respMallShopProductDTO.setMallShopProductMediaDTODTOList(mallShopProductMediaDTODTOList); respMallShopProductDTO.setMallShopProductVideoDTODTOList(mallShopProductVideoDTODTOList); // 商品规格集合 respMallShopProductDTO.setMallShopProductUnitDTOList(mallShopProductUnitDTOList); result.setCode( 0 ); result.setData(respMallShopProductDTO); return result; } /** * 门店商品集合 * @param idList * @return */ @Override public List selectMallShopProductByIdList(List idList) { List mallShopProductEntityList = new ArrayList<>(); if (CollectionUtils.isEmpty(idList)) { return mallShopProductEntityList; } LambdaQueryWrapper lwq = new LambdaQueryWrapper<>(); lwq.eq(MallShopProductEntity::getDelFlag, CommonIsDeleteEnum.DISABLE.getCode()); lwq.in(MallShopProductEntity::getId, idList); return baseDao.selectList(lwq); } /** * 根据商品名称或者分类id 查询商品集合对象 * * @param goodsName * @param categoryId * @return */ @Override public List selectMallShopProductByGoodsNameCategoryId(String goodsName, Long categoryId) { List mallShopProductList = new ArrayList<>(); if (StrUtil.isBlank(goodsName) && null == categoryId) { return mallShopProductList; } LambdaQueryWrapper lwq = new LambdaQueryWrapper<>(); lwq.eq(MallShopProductEntity::getDelFlag, CommonIsDeleteEnum.DISABLE.getCode()); if (StringUtils.isNotBlank(goodsName)) { lwq.like(MallShopProductEntity::getGoodsName, goodsName); } if ( null != categoryId) { lwq.eq(MallShopProductEntity::getCategoryId, categoryId); } return baseDao.selectList(lwq); } @Override public List selectByGoodsIdList(List goodsIdList ,Long shopId) { List mallShopProductList = new ArrayList<>(); if (CollectionUtils.isEmpty(goodsIdList)) { return mallShopProductList; } LambdaQueryWrapper lwq = new LambdaQueryWrapper<>(); lwq.eq(MallShopProductEntity::getDelFlag, CommonIsDeleteEnum.DISABLE.getCode()); lwq.eq(MallShopProductEntity::getShopId, shopId); lwq.in(MallShopProductEntity::getGoodsId, goodsIdList); return baseDao.selectList(lwq); } }</long,></long,></string,sysdynamictranslationentity></long,></long,></long,></long,></long,></long,></long,></long,></long,list</long,></long,></long,></long,></long,></long,></long,list</long,></long,></string,></string,></string,sysdynamictranslationentity></long,></string,sysdynamictranslationentity></string,sysdynamictranslationentity></string,sysdynamictranslationentity></string,></mallshopproductdao,>
# 代码分析报告(实事求是版)——MallShopProduct 聚合查询 + i18n + Redis
> 目标:对当前 Service 层“商品分页聚合查询”相关代码做工程化分析,指出真实问题、已有正确点、风险点与可落地优化路径。
> 原则:**不夸大、不推翻、不凭空假设**,仅基于你现有架构(RedisUtils / SysDynamicTranslationService / I18nTranslator / I18nKeyUtil)给出建议。
---
## 1. 代码的职责与上下文
该段代码处于 **MallShopProduct 相关 ServiceImpl**(或相近的聚合 Service)中,承担的职责同时覆盖:
1. **分页查询商品**(基于 query 条件)
2. **补齐关联数据**(分类、单位、SKU、图片、用户、门店等)
3. **金额格式化**(小数位、舍入方式由系统参数控制)
4. **动态翻译(i18n)**:
- 批量翻译:`mapByIdsTypeLangField`
- 单条翻译:`getOne`
- 搜索翻译:`listByFieldSearch`
5. **DTO 组装**(将 Entity 聚合为 Resp DTO 返回)
从工程角度看,它已经是一个 **“聚合编排方法”**,逻辑复杂度高,属于典型的“业务中枢型方法”。
---
## 2. 当前实现的整体流程拆解(事实)
下面按执行流程拆解(从你的描述与片段可推断的典型结构):
### 2.1 条件构建与预处理
- 分类条件:支持一级分类输入,自动补充二级分类(或根据父子关系扩展)
- 商品名称条件:可能通过翻译表进行搜索(keyword -> translation -> entityIdList),再用于主表查询
- 其他条件:门店/状态/时间/启用等
✅ **结论**:条件预处理符合业务逻辑需求
⚠️ **注意**:若商品名搜索走翻译表 LIKE,会产生“慢查询风险”,需结合缓存策略(已开始做)
---
### 2.2 主分页查询(核心)
通常是类似:
```java
page = baseDao.findMallShopProductPage(page, query);

浙公网安备 33010602011771号