|
|
@@ -3,19 +3,21 @@ package com.izouma.dingdong.service.merchant;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.izouma.dingdong.config.Constants;
|
|
|
import com.izouma.dingdong.converter.LongArrayConverter;
|
|
|
+import com.izouma.dingdong.converter.StringArrayConverter;
|
|
|
import com.izouma.dingdong.domain.merchant.Goods;
|
|
|
+import com.izouma.dingdong.domain.merchant.Merchant;
|
|
|
import com.izouma.dingdong.domain.merchant.MerchantClassification;
|
|
|
+import com.izouma.dingdong.domain.merchant.MerchantSettings;
|
|
|
import com.izouma.dingdong.enums.ApplyStatus;
|
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
|
import com.izouma.dingdong.repo.UserRepo;
|
|
|
-import com.izouma.dingdong.repo.merchant.GoodsRepo;
|
|
|
-import com.izouma.dingdong.repo.merchant.MerchantClassificationRepo;
|
|
|
-import com.izouma.dingdong.repo.merchant.MerchantRepo;
|
|
|
-import com.izouma.dingdong.repo.merchant.SalesRepo;
|
|
|
+import com.izouma.dingdong.repo.merchant.*;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.time.DayOfWeek;
|
|
|
+import java.time.LocalDate;
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
@@ -25,7 +27,7 @@ public class GoodsService {
|
|
|
|
|
|
private GoodsRepo goodsRepo;
|
|
|
|
|
|
- private MerchantRepo merchantRepo;
|
|
|
+ private MerchantSettingsRepo merchantSettingsRepo;
|
|
|
|
|
|
private MerchantService merchantService;
|
|
|
|
|
|
@@ -46,13 +48,18 @@ public class GoodsService {
|
|
|
if (ObjectUtil.isNull(goods.getMerchantId())) {
|
|
|
throw new BusinessException("缺少商家");
|
|
|
}
|
|
|
- merchantRepo.findById(goods.getMerchantId()).orElseThrow(new BusinessException("商家不存在"));
|
|
|
- if (goods.getInventory() < 1) {
|
|
|
+ MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(goods.getMerchantId()).orElseThrow(new BusinessException("商家不存在"));
|
|
|
+ if (ObjectUtil.isNull(goods.getInventory()) || goods.getInventory() < 1) {
|
|
|
throw new BusinessException("商品供应数量不足");
|
|
|
}
|
|
|
if (BigDecimal.ZERO.compareTo(goods.getAmount()) >= 0) {
|
|
|
throw new BusinessException("价格不得小于等于0");
|
|
|
}
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(goods.getDiscountAmount())) {
|
|
|
+ goods.setDiscountAmount(goods.getAmount());
|
|
|
+ }
|
|
|
+
|
|
|
if (BigDecimal.ZERO.compareTo(goods.getDiscountAmount()) >= 0) {
|
|
|
throw new BusinessException("折扣价不得小于等于0");
|
|
|
}
|
|
|
@@ -62,6 +69,14 @@ public class GoodsService {
|
|
|
throw new BusinessException("折扣价不得大于原价");
|
|
|
}
|
|
|
|
|
|
+ if (goods.getStartTime().isBefore(merchantSettings.getStartTime()) || goods.getEndTime().isAfter(merchantSettings.getEndTime())) {
|
|
|
+ throw new BusinessException("商品销售时间不得早于/晚于营业时间");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (containsWeek(goods.getWeek(),merchantSettings.getWeek())){
|
|
|
+ throw new BusinessException("商品销售日期不在营业日期内");
|
|
|
+ }
|
|
|
+
|
|
|
if (goods.getId() == null) {
|
|
|
goods.setLikes(0);
|
|
|
goods.setBad(0);
|
|
|
@@ -164,4 +179,28 @@ public class GoodsService {
|
|
|
}
|
|
|
goodsRepo.save(goods);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ 周几
|
|
|
+ */
|
|
|
+ public Boolean containsWeek(String goodsWeek, String merWeek) {
|
|
|
+
|
|
|
+
|
|
|
+ List<String> merWeeks = new StringArrayConverter().convertToEntityAttribute(merWeek);
|
|
|
+ List<String> goodsWeeks = new StringArrayConverter().convertToEntityAttribute(goodsWeek);
|
|
|
+
|
|
|
+ if (merWeeks.contains("EVERY")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String g:goodsWeeks ){
|
|
|
+ if (!merWeeks.contains(g)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
}
|