Ver código fonte

add update product to combination prduct

Mr.qian 2 semanas atrás
pai
commit
87c234e28f

+ 10 - 0
mall-service/src/main/java/com/txz/mall/dao/StoreCombinationMapper.java

@@ -1,7 +1,17 @@
 package com.txz.mall.dao;
 
 import com.txz.mall.core.Mapper;
+import com.txz.mall.dao.impl.StoreCombinationMapperImpl;
 import com.txz.mall.model.StoreCombination;
+import dto.StoreProductAddRequest;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.SelectProvider;
+import org.apache.ibatis.annotations.UpdateProvider;
+
+import java.util.List;
 
 public interface StoreCombinationMapper extends Mapper<StoreCombination> {
+    
+    @UpdateProvider(type = StoreCombinationMapperImpl.class, method = "updateCombinationByProductId")
+    int updateCombinationByProductId(StoreProductAddRequest request);
 }

+ 24 - 0
mall-service/src/main/java/com/txz/mall/dao/impl/StoreCombinationMapperImpl.java

@@ -0,0 +1,24 @@
+package com.txz.mall.dao.impl;
+
+import dto.StoreProductAddRequest;
+import org.apache.ibatis.jdbc.SQL;
+
+/**
+ * @author: MTD®️
+ * @date: 2025/8/25
+ */
+
+public class StoreCombinationMapperImpl {
+    
+    public String updateCombinationByProductId(StoreProductAddRequest request) {
+        return new SQL() {{
+            UPDATE("m_store_combination")
+                    .SET("product_name=#{storeName}")
+                    .SET("image=#{image}")
+                    .SET("images=#{sliderImage}")
+                    .SET("info=#{storeInfo}")
+                    .WHERE("product_id = #{id}")
+            ;
+        }}.toString();
+    }
+}

+ 66 - 61
mall-service/src/main/java/com/txz/mall/service/impl/StoreProductServiceImpl.java

@@ -14,11 +14,9 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.txz.mall.constants.Constants;
 import com.txz.mall.core.AbstractService;
 import com.txz.mall.core.ServiceException;
+import com.txz.mall.dao.StoreCombinationMapper;
 import com.txz.mall.dao.StoreProductMapper;
-import com.txz.mall.model.ProductAttr;
-import com.txz.mall.model.ProductAttrValue;
-import com.txz.mall.model.StoreOrder;
-import com.txz.mall.model.StoreProduct;
+import com.txz.mall.model.*;
 import com.txz.mall.service.ProductAttrService;
 import com.txz.mall.service.ProductAttrValueService;
 import com.txz.mall.service.StoreProductService;
@@ -61,7 +59,10 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
     private ProductAttrService attrService;
     @Resource
     private ProductAttrValueService attrValueService;
-
+    
+    @Resource
+    private StoreCombinationMapper combinationMapper;
+    
     @Override
     public void importFile(MultipartFile file) {
         String name = file.getOriginalFilename();
@@ -75,12 +76,12 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             if (CollectionUtils.isEmpty(list)) {
                 throw new ServiceException("请填写内容后再提交!");
             }
-
+            
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
-
+    
     @Override
     public void exportFile(HttpServletResponse response) {
         OutputStream outputStream = null;
@@ -98,7 +99,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             throw new ServiceException("模板下载失败, 请联系管理员");
         }
     }
-
+    
     @Override
     public StoreProductInfoVO getInfo(Long id) {
         StoreProduct storeProduct = findById(id);
@@ -107,13 +108,13 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
         }
         StoreProductInfoVO storeProductResponse = new StoreProductInfoVO();
         BeanUtils.copyProperties(storeProduct, storeProductResponse);
-
+        
         // 设置商品所参与的活动
         if (StrUtil.isNotBlank(storeProduct.getActivity())) {
             List<String> activityList = getProductActivityList(storeProduct.getActivity());
             storeProductResponse.setActivity(activityList);
         }
-
+        
         List<ProductAttr> attrList = attrService.getListByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
         List<AttrVO> attrResponseList = attrList.stream().map(e -> {
             AttrVO attrResponse = new AttrVO();
@@ -124,7 +125,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             return attrResponse;
         }).collect(Collectors.toList());
         storeProductResponse.setAttr(attrResponseList);
-
+        
         List<ProductAttrValue> attrValueList = attrValueService.getListByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
         List<AttrValueVO> valueResponseList = attrValueList.stream().map(e -> {
             AttrValueVO valueResponse = new AttrValueVO();
@@ -132,25 +133,26 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             return valueResponse;
         }).collect(Collectors.toList());
         storeProductResponse.setAttrValue(valueResponseList);
-
-//        StoreProductDescription sd = storeProductDescriptionService.getByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
-//        if (ObjectUtil.isNotNull(sd)) {
-//            storeProductResponse.setContent(ObjectUtil.isNull(sd.getDescription()) ? "" : sd.getDescription());
-//        }
-//
-//        // 获取已关联的优惠券
-//        List<StoreProductCoupon> storeProductCoupons = storeProductCouponService.getListByProductId(storeProduct.getId());
-//        if (CollUtil.isNotEmpty(storeProductCoupons)) {
-//            List<Integer> ids = storeProductCoupons.stream().map(StoreProductCoupon::getIssueCouponId).collect(Collectors.toList());
-//            storeProductResponse.setCouponIds(ids);
-//        }
+        
+        //        StoreProductDescription sd = storeProductDescriptionService.getByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
+        //        if (ObjectUtil.isNotNull(sd)) {
+        //            storeProductResponse.setContent(ObjectUtil.isNull(sd.getDescription()) ? "" : sd.getDescription());
+        //        }
+        //
+        //        // 获取已关联的优惠券
+        //        List<StoreProductCoupon> storeProductCoupons = storeProductCouponService.getListByProductId(storeProduct.getId());
+        //        if (CollUtil.isNotEmpty(storeProductCoupons)) {
+        //            List<Integer> ids = storeProductCoupons.stream().map(StoreProductCoupon::getIssueCouponId).collect(Collectors.toList());
+        //            storeProductResponse.setCouponIds(ids);
+        //        }
         return storeProductResponse;
     }
-
+    
     /**
      * 商品活动字符列表
      *
      * @param activityStr 商品活动字符串
+     *
      * @return 商品活动字符列表
      */
     private List<String> getProductActivityList(String activityStr) {
@@ -180,23 +182,23 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
         }
         return activityList;
     }
-
+    
     @Override
     public StoreProductCountItemVO getOrderStatusNum() {
         StoreProductCountItemVO response = new StoreProductCountItemVO();
-
+        
         Condition condition = new Condition(StoreOrder.class);
         Example.Criteria criteria = condition.createCriteria();
         criteria.andEqualTo("isDelete", 0);
         List<StoreProduct> productList = this.findByCondition(condition);
-
+        
         response.setAll(productList.size());
         response.setAlreadyListed((int) productList.stream().filter(e -> e.getIsShow().equals(1)).count());
         response.setRemoved((int) productList.stream().filter(e -> e.getIsShow().equals(0)).count());
         response.setRecycle((int) productList.stream().filter(e -> e.getIsRecycle().equals(1)).count());
         return response;
     }
-
+    
     @Override
     @Transactional
     public void save(StoreProductAddRequest request) {
@@ -205,7 +207,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
         }
         assignAttributes(request, 0);
     }
-
+    
     /**
      * 添加商品属性
      *
@@ -223,28 +225,28 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
         BeanUtils.copyProperties(request, storeProduct);
         // 设置activity活动
         storeProduct.setActivity(getProductActivityStr(request.getActivity()));
-        //主图
+        // 主图
         storeProduct.setImage(storeProduct.getImage());
-        //轮播图
+        // 轮播图
         storeProduct.setSliderImage(storeProduct.getSliderImage());
         // 展示图
         if (StrUtil.isNotEmpty(storeProduct.getFlatPattern())) {
             storeProduct.setFlatPattern(storeProduct.getFlatPattern());
         }
-
+        
         List<StoreProductAttrValueAddRequest> attrValueAddRequestList = request.getAttrValue();
-        //计算价格
+        // 计算价格
         StoreProductAttrValueAddRequest minAttrValue = attrValueAddRequestList.stream().min(Comparator.comparing(StoreProductAttrValueAddRequest::getPrice)).get();
         storeProduct.setPrice(minAttrValue.getPrice());
         storeProduct.setOtPrice(minAttrValue.getOtPrice());
         storeProduct.setCost(minAttrValue.getCost());
         storeProduct.setStock(attrValueAddRequestList.stream().mapToInt(StoreProductAttrValueAddRequest::getStock).sum());
-
+        
         if (StringUtils.isBlank(storeProduct.getItemNumber())) {
             String itemNumber = generateItemNumber(storeProduct.getCateId());
             storeProduct.setItemNumber(itemNumber);
         }
-
+        
         Date date = new Date();
         List<StoreProductAttrAddRequest> addRequestList = request.getAttr();
         List<ProductAttr> attrList = addRequestList.stream().map(e -> {
@@ -256,7 +258,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             attr.setIsDelete(0);
             return attr;
         }).collect(Collectors.toList());
-
+        
         List<ProductAttrValue> attrValueList = attrValueAddRequestList.stream().map(e -> {
             ProductAttrValue attrValue = new ProductAttrValue();
             BeanUtils.copyProperties(e, attrValue);
@@ -282,7 +284,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             attrValue.setCreateTime(date);
             return attrValue;
         }).collect(Collectors.toList());
-
+        
         if (status.equals(0)) {
             storeProduct.setId(null);
             storeProduct.setIsShow(0);
@@ -294,37 +296,38 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             String attrIds = productAttrs.stream()
                     .map(attr -> attr.getId().toString())
                     .collect(Collectors.joining(","));
-
+            
             String attrValueIds = productAttrValues.stream()
                     .map(attr -> attr.getId().toString())
                     .collect(Collectors.joining(","));
-
+            
             attrService.deleteByIds(attrIds);
             attrValueService.deleteByIds(attrValueIds);
         }
-
+        
         attrList.forEach(attr -> attr.setProductId(storeProduct.getId()));
         attrValueList.forEach(value -> value.setProductId(storeProduct.getId()));
         attrService.save(attrList);
         attrValueService.save(attrValueList);
-
-        //优惠券
-//            if (CollUtil.isNotEmpty(request.getCouponIds())) {
-//                List<StoreProductCoupon> couponList = new ArrayList<>();
-//                for (Integer couponId : request.getCouponIds()) {
-//                    StoreProductCoupon spc = new StoreProductCoupon(storeProduct.getId(), couponId, DateUtil.getNowTime());
-//                    couponList.add(spc);
-//                }
-//                storeProductCouponService.saveBatch(couponList);
-//            }
+        
+        // 优惠券
+        //            if (CollUtil.isNotEmpty(request.getCouponIds())) {
+        //                List<StoreProductCoupon> couponList = new ArrayList<>();
+        //                for (Integer couponId : request.getCouponIds()) {
+        //                    StoreProductCoupon spc = new StoreProductCoupon(storeProduct.getId(), couponId, DateUtil.getNowTime());
+        //                    couponList.add(spc);
+        //                }
+        //                storeProductCouponService.saveBatch(couponList);
+        //            }
     }
-
+    
     @Override
     @Transactional
     public void update(StoreProductAddRequest request) {
         assignAttributes(request, 1);
+        combinationMapper.updateCombinationByProductId(request);
     }
-
+    
     @Override
     public void operationStock(Integer id, Integer num, String type) {
         UpdateWrapper<StoreProduct> updateWrapper = new UpdateWrapper<>();
@@ -341,12 +344,12 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             updateWrapper.last(StrUtil.format(" and (quota - {} >= 0)", num));
         }
         updateWrapper.eq("id", id);
-//        boolean update = update(updateWrapper);
-//        if (!update) {
-//            throw new ServiceException("更新商品库存失败!商品id = " + id);
-//        }
+        //        boolean update = update(updateWrapper);
+        //        if (!update) {
+        //            throw new ServiceException("更新商品库存失败!商品id = " + id);
+        //        }
     }
-
+    
     // 生成货号的方法
     private String generateItemNumber(String cateIdStr) {
         // 1. 获取类目ID的字符串形式
@@ -358,7 +361,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
         // 4. 拼接类目ID和随机部分
         return cateIdStr + randomPart;
     }
-
+    
     // 生成指定长度的随机字符串(数字和字母)
     private String generateRandomString(int length) {
         String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -369,11 +372,12 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
         }
         return sb.toString();
     }
-
+    
     /**
      * 商品sku
      *
      * @param attrValue json字符串
+     *
      * @return sku
      */
     private String getSku(String attrValue) {
@@ -384,14 +388,15 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             Map.Entry<String, String> next = iterator.next();
             strings.add(next.getValue());
         }
-//        List<String> strings = jsonObject.values().stream().map(o -> (String) o).collect(Collectors.toList());
+        //        List<String> strings = jsonObject.values().stream().map(o -> (String) o).collect(Collectors.toList());
         return String.join(",", strings);
     }
-
+    
     /**
      * 商品活动字符串
      *
      * @param activityList 活动数组
+     *
      * @return 商品活动字符串
      */
     private String getProductActivityStr(List<String> activityList) {