|
|
@@ -1,18 +1,82 @@
|
|
|
package com.seecoder.BlueWhale.serviceImpl;
|
|
|
|
|
|
+import com.seecoder.BlueWhale.enums.CouponTypeEnum;
|
|
|
+import com.seecoder.BlueWhale.exception.BlueWhaleException;
|
|
|
+import com.seecoder.BlueWhale.po.Coupon;
|
|
|
import com.seecoder.BlueWhale.po.CouponGroup;
|
|
|
+import com.seecoder.BlueWhale.po.User;
|
|
|
import com.seecoder.BlueWhale.repository.CouponGroupRepository;
|
|
|
+import com.seecoder.BlueWhale.repository.CouponRepository;
|
|
|
import com.seecoder.BlueWhale.service.CouponService;
|
|
|
+import com.seecoder.BlueWhale.util.SecurityUtil;
|
|
|
+import com.seecoder.BlueWhale.vo.CouponGroupVO;
|
|
|
import com.seecoder.BlueWhale.vo.CouponVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
@Service
|
|
|
public class CouponServiceImpl implements CouponService {
|
|
|
|
|
|
@Autowired
|
|
|
CouponGroupRepository couponGroupRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ CouponRepository couponRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ SecurityUtil securityUtil;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean createCouponGroup(CouponGroupVO couponGroupVO) {
|
|
|
+ if (couponGroupVO.getType()== CouponTypeEnum.FULL_REDUCTION){
|
|
|
+ if (couponGroupVO.getSatisfaction()==null || couponGroupVO.getMinus()==null ||
|
|
|
+ couponGroupVO.getSatisfaction()<couponGroupVO.getMinus()){
|
|
|
+ throw BlueWhaleException.fullReductionCouponError();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ CouponGroup couponGroup=couponGroupVO.toPO();
|
|
|
+ couponGroupRepository.save(couponGroup);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<CouponGroupVO> getCouponGroups() {
|
|
|
+ User user=securityUtil.getCurrentUser();
|
|
|
+ switch (user.getRole()){
|
|
|
+ case CUSTOMER:
|
|
|
+ List<CouponGroup> couponGroups=couponGroupRepository.findByRestGreaterThan(0);
|
|
|
+ List<Coupon> coupons=couponRepository.findByUserId(user.getId());
|
|
|
+ Set<Integer> received=coupons.stream()
|
|
|
+ .map(Coupon::getGroupId)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ return couponGroups.stream()
|
|
|
+ .filter(x->!received.contains(x.getId()))
|
|
|
+ .map(CouponGroup::toVO)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ case STAFF:
|
|
|
+ return couponGroupRepository.findByStoreId(user.getStoreId()).stream()
|
|
|
+ .map(CouponGroup::toVO).collect(Collectors.toList());
|
|
|
+ case MANAGER:
|
|
|
+ case CEO:
|
|
|
+ return couponGroupRepository.findAll().stream()
|
|
|
+ .map(CouponGroup::toVO).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean receiveCoupon(Integer couponGroupId) {
|
|
|
+ CouponGroup couponGroup=couponGroupRepository.findById(couponGroupId).get();
|
|
|
+ if (couponGroup.getRest()<=0){
|
|
|
+
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
private CouponVO wrapWithGroupId(CouponVO couponVO){
|
|
|
CouponGroup couponGroup=couponGroupRepository.findById(couponVO.getGroupId()).get();
|
|
|
couponVO.setSatisfaction(couponGroup.getSatisfaction());
|
|
|
@@ -21,4 +85,5 @@ public class CouponServiceImpl implements CouponService {
|
|
|
couponVO.setStoreId(couponGroup.getStoreId());
|
|
|
return couponVO;
|
|
|
}
|
|
|
+
|
|
|
}
|