Commit 7bb538d0 by wuwenlong

check coupon dev;

parent 0da87a87
......@@ -28,6 +28,8 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
public static String YYYY_MM_DD_HH= "yyyy-MM-dd HH";
public static String YYYY_MM_DD_HH_MM= "yyyy-MM-dd HH:mm";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
......
......@@ -6,6 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import share.common.annotation.Excel;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
......@@ -29,6 +30,13 @@ public class CouponRequest {
@NotNull(message = "下单模式不能为空")
private Integer orderMode;
@ApiModelProperty(value = "下单类型(1:预定,2:续单)", required = true)
@NotNull(message = "下单类型不能为空")
private Integer orderType;
@ApiModelProperty(value = "套餐ID")
private Long packageId;
/**
* 预约开始时间
*/
......
......@@ -16,14 +16,12 @@ import share.common.enums.*;
import share.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import share.common.utils.StringUtils;
import share.system.domain.*;
import share.system.domain.vo.FrontTokenComponent;
import share.system.mapper.SConsumerCouponMapper;
import share.system.request.CouponRequest;
import share.system.service.ISConsumerCouponService;
import share.system.service.ISCouponService;
import share.system.service.ISRoomService;
import share.system.service.ISStoreService;
import share.system.service.*;
/**
* 优惠券领取记录Service业务层处理
......@@ -33,8 +31,6 @@ import share.system.service.ISStoreService;
*/
@Service
public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMapper, SConsumerCoupon> implements ISConsumerCouponService {
@Autowired
private SConsumerCouponMapper sConsumerCouponMapper;
@Autowired
private ISRoomService sRoomService;
......@@ -45,6 +41,9 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
@Autowired
private ISCouponService sCouponService;
@Autowired
private IPackService packService;
private static final Integer ZERO = 0;
/**
......@@ -55,7 +54,7 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
*/
@Override
public SConsumerCoupon selectSConsumerCouponById(Long id) {
return sConsumerCouponMapper.selectSConsumerCouponById(id);
return baseMapper.selectSConsumerCouponById(id);
}
/**
......@@ -66,12 +65,12 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
*/
@Override
public List<SConsumerCoupon> selectSConsumerCouponList(SConsumerCoupon sConsumerCoupon) {
List<SConsumerCoupon> sConsumerCoupons = sConsumerCouponMapper.selectSConsumerCouponList(sConsumerCoupon);
List<SConsumerCoupon> sConsumerCoupons = baseMapper.selectSConsumerCouponList(sConsumerCoupon);
sConsumerCoupons.forEach(item -> {
if (item.getUseStatus().equals(UserStatusEnum.UNUSED.getCode())) {
if (item.getEndDate().compareTo(new Date()) < 0) {
item.setUseStatus(UserStatusEnum.EXPIRED.getCode());
sConsumerCouponMapper.updateSConsumerCoupon(item);
baseMapper.updateSConsumerCoupon(item);
}
}
});
......@@ -88,7 +87,7 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
public int insertSConsumerCoupon(SConsumerCoupon sConsumerCoupon) {
sConsumerCoupon.setCreateTime(DateUtils.getNowDate());
sConsumerCoupon.setUseStatus(UserStatusEnum.UNUSED.getCode());
return sConsumerCouponMapper.insertSConsumerCoupon(sConsumerCoupon);
return baseMapper.insertSConsumerCoupon(sConsumerCoupon);
}
/**
......@@ -100,7 +99,7 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
@Override
public int updateSConsumerCoupon(SConsumerCoupon sConsumerCoupon) {
sConsumerCoupon.setUpdateTime(DateUtils.getNowDate());
return sConsumerCouponMapper.updateSConsumerCoupon(sConsumerCoupon);
return baseMapper.updateSConsumerCoupon(sConsumerCoupon);
}
/**
......@@ -113,7 +112,7 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
public int deleteSConsumerCouponByIds(Long[] ids) {
LambdaQueryWrapper<SConsumerCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SConsumerCoupon::getId, ids);
List<SConsumerCoupon> sConsumerCoupons = sConsumerCouponMapper.selectList(queryWrapper);
List<SConsumerCoupon> sConsumerCoupons = baseMapper.selectList(queryWrapper);
//获取sConsumerCoupons里的优惠券id
Long[] couponIds = sConsumerCoupons.stream().map(SConsumerCoupon::getCouponId).toArray(Long[]::new);
//根据优惠券id查询优惠券信息
......@@ -128,7 +127,7 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
}
});
return sConsumerCouponMapper.deleteSConsumerCouponByIds(ids);
return baseMapper.deleteSConsumerCouponByIds(ids);
}
/**
......@@ -139,12 +138,12 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
*/
@Override
public int deleteSConsumerCouponById(Long id) {
return sConsumerCouponMapper.deleteSConsumerCouponById(id);
return baseMapper.deleteSConsumerCouponById(id);
}
@Override
public int selectSConsumerCouponConut(SConsumerCoupon sConsumerCoupon) {
return sConsumerCouponMapper.selectSConsumerCouponConut(sConsumerCoupon);
return baseMapper.selectSConsumerCouponConut(sConsumerCoupon);
}
@Override
......@@ -154,9 +153,9 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
sConsumerCoupon.setUseStatus(ConsumerCouponUseStatusEnum.WAIT_USE.getCode());
sConsumerCoupon.setConsumerId(user.getId());
//查询用户未使用的优惠券
List<SConsumerCoupon> sConsumerCoupons = sConsumerCouponMapper.querySConsumerCouponList(sConsumerCoupon);
List<SConsumerCoupon> sConsumerCoupons = baseMapper.querySConsumerCouponList(sConsumerCoupon);
//计算时长
BigDecimal bigDecimal = DateUtils.differentHour(couponRequest.getPreStartDate(), couponRequest.getPreEndDate());
BigDecimal timeLong = DateUtils.differentHour(couponRequest.getPreStartDate(), couponRequest.getPreEndDate());
if (CollectionUtils.isEmpty(sConsumerCoupons)) {
return sConsumerCoupons;
}
......@@ -164,47 +163,119 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
List<SStore> sStores = sStoreService.list();
//查询所有房间
List<SRoom> sRooms = sRoomService.list();
//查询所有的套餐
List<SPack> sPacks = packService.list();
//迭代
sConsumerCoupons.forEach(item -> {
if (item.getStartDate().compareTo(couponRequest.getPreStartDate()) <= 0 && item.getEndDate().compareTo(couponRequest.getPreEndDate()) >= 0) {
SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");
String proStartTime = sdf1.format(couponRequest.getPreStartDate());
if (proStartTime.compareTo(item.getCouponTimeStart()) >= 0) {
SStore sStore = sStores.stream().filter(store -> store.getId().equals(couponRequest.getStoreId())).findFirst().orElse(null);
SRoom byId = sRooms.stream().filter(room -> room.getId().equals(couponRequest.getRoomId())).findFirst().orElse(null);
BigDecimal subtract = new BigDecimal(item.getMinDuration());
if (item.getCouponType().equals(CouponTypeEnum.PACKAGE.getCode())) {
if (couponRequest.getOrderMode().equals(BuyTypeEnum.PACK.getCode())) {
//计算价格
extracted(item, bigDecimal, byId, subtract, sStore);
} else {
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("仅支持套餐模式下单使用");
}
} else if (item.getCouponType().equals(CouponTypeEnum.DURATION.getCode())) {
//判断门槛时长
if (bigDecimal.compareTo(subtract) >= 0) {
//判断门店类型
isStoreType(item, sStore, byId);
} else {
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("下单时长等于" + bigDecimal + "小时,不满足优惠券门槛时长");
}
} else {
extracted(item, bigDecimal, byId, subtract, sStore);
}
} else {
checkCoupon(item,couponRequest,sStores,sRooms,sPacks,timeLong);
});
return sConsumerCoupons;
}
private Boolean checkCoupon(SConsumerCoupon item, CouponRequest couponRequest
, List<SStore> sStores, List<SRoom> sRooms, List<SPack> sPacks, BigDecimal timeLong){
return checkAvailableDate(item,couponRequest)
&&checkCouponPeriod(item,couponRequest)
&&checkStore(item,couponRequest,sStores)
&&checkCouponType(item,couponRequest,sStores,sRooms,timeLong)
&&checkOrderType(item,couponRequest)
&&checkPack(item,couponRequest,sPacks);
}
private Boolean checkPack(SConsumerCoupon item, CouponRequest couponRequest, List<SPack> sPacks){
if(Objects.nonNull(item.getPackageId())&&item.getPackageId().compareTo(couponRequest.getPackageId())!=0){
SPack pack = sPacks.stream().filter(obj -> obj.getId().compareTo(item.getPackageId())==0).findFirst().orElse(null);
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("优惠卷当前套餐不可用,【"+pack.getName()+"】套餐可用");
}
return item.getIsAvailable()==0;
}
private Boolean checkOrderType(SConsumerCoupon item, CouponRequest couponRequest){
if(Objects.nonNull(item.getOrderType())&&item.getOrderType().compareTo(couponRequest.getOrderType())!=0){
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("优惠卷"+OrderTypeEnum.getEnumByCode(item.getOrderType()).getName()+"可用");
}
return item.getIsAvailable()==0;
}
private Boolean checkAvailableDate(SConsumerCoupon item, CouponRequest couponRequest){
if ((Objects.nonNull(item.getStartDate())&&Objects.nonNull(item.getEndDate()))
&&(item.getStartDate().compareTo(couponRequest.getPreStartDate()) > 0
|| item.getEndDate().compareTo(couponRequest.getPreEndDate()) < 0)){
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("优惠卷未生效,优惠卷有效时间为" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, item.getStartDate())
+ "-" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, item.getEndDate()));
}
return item.getIsAvailable()==0;
}
private Boolean checkCouponPeriod(SConsumerCoupon item, CouponRequest couponRequest){
if(Objects.nonNull(item.getCouponTimeStart())&&Objects.nonNull(item.getCouponTimeEnd())
&& !StringUtils.equals(item.getCouponTimeStart(),"00:00")&&!StringUtils.equals(item.getCouponTimeEnd(),"24:00")){
String startStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,couponRequest.getPreStartDate());
//判断短是否跨日
if(DateUtils.parseDate(startStr+" "+item.getCouponTimeStart()).compareTo(DateUtils.parseDate(startStr+" "+item.getCouponTimeEnd()))>0){
if(DateUtils.parseDate(startStr+" "+item.getCouponTimeStart()).compareTo(couponRequest.getPreStartDate())>0
||DateUtils.addDays(DateUtils.parseDate(startStr+" "+item.getCouponTimeEnd()),1).compareTo(couponRequest.getPreEndDate())<0) {
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("优惠卷未生效,优惠卷有效时段为每天" + item.getCouponTimeStart() + "-" + item.getCouponTimeEnd());
item.setReason("优惠卷可用时段为" + item.getCouponTimeStart() + "-次日" + item.getCouponTimeEnd());
}
}else{
if(DateUtils.parseDate(startStr+" "+item.getCouponTimeStart()).compareTo(couponRequest.getPreStartDate())>0
||DateUtils.parseDate(startStr+" "+item.getCouponTimeEnd()).compareTo(couponRequest.getPreEndDate())<0) {
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("优惠卷可用时段为" + item.getCouponTimeStart() + "-" + item.getCouponTimeEnd());
}
} else {
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("优惠卷未生效,优惠卷有效时间为" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, item.getStartDate()) + "-" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, item.getEndDate()));
}
});
return sConsumerCoupons;
}
return item.getIsAvailable()==0;
}
private Boolean checkCouponType(SConsumerCoupon item, CouponRequest couponRequest
, List<SStore> sStores, List<SRoom> sRooms, BigDecimal timeLong){
SStore sStore = sStores.stream().filter(store -> store.getId().equals(couponRequest.getStoreId())).findFirst().orElse(null);
SRoom byId = sRooms.stream().filter(room -> room.getId().equals(couponRequest.getRoomId())).findFirst().orElse(null);
BigDecimal subtract = new BigDecimal(item.getMinDuration());
CouponTypeEnum couponTypeEnum = CouponTypeEnum.getTypeCode(item.getCouponType());
switch (couponTypeEnum){
case PACKAGE:
if (couponRequest.getOrderMode().equals(BuyTypeEnum.PACK.getCode())) {
//计算价格
extracted(item, timeLong, byId, subtract, sStore);
} else {
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("仅支持套餐模式下单使用");
}
break;
case DURATION:
//判断门槛时长
if (timeLong.compareTo(subtract) >= 0) {
//判断门店类型
isStoreType(item, sStore, byId);
} else {
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("下单时长等于" + timeLong + "小时,不满足优惠券门槛时长");
}
break;
default:
extracted(item, timeLong, byId, subtract, sStore);
break;
}
return item.getIsAvailable()==0;
}
private Boolean checkStore(SConsumerCoupon item,CouponRequest couponRequest,List<SStore> sStores){
if(Objects.nonNull(item.getStoreId())&&item.getStoreId().compareTo(couponRequest.getStoreId())==0){
SStore sStore = sStores.stream().filter(store -> store.getId().equals(item.getStoreId())).findFirst().orElse(null);
item.setIsAvailable(AvailableEnum.UNAVAILABLE.getCode());
item.setReason("当前门店不可用,适用门店【"+sStore.getName()+"】");
}
return item.getIsAvailable()==0;
}
@Override
public int give(SConsumerCoupon sConsumerCoupon) {
//根据优惠券id查询优惠券信息
......@@ -240,7 +311,7 @@ public class SConsumerCouponServiceImpl extends ServiceImpl<SConsumerCouponMappe
newSConsumerCoupon.setStartDate(sCoupon.getStartDate());
newSConsumerCoupon.setEndDate(sCoupon.getEndDate());
newSConsumerCoupon.setRemark(sCoupon.getRemark());
int insert = sConsumerCouponMapper.insert(newSConsumerCoupon);
int insert = baseMapper.insert(newSConsumerCoupon);
if (insert > 0) {
//优惠券数量减一
sCoupon.setNumber(sCoupon.getNumber() - 1);
......
......@@ -1048,7 +1048,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
// 判断优惠券是否可以使用
if(Objects.nonNull(user)&&Objects.nonNull(request.getCouponId())) {
SConsumerCoupon consumerCoupon = consumerCouponService.getById(request.getCouponId());
computedCouponPrice(priceResponse, user, consumerCoupon);
computedCouponPrice(request, priceResponse, user, consumerCoupon);
}
}
return priceResponse;
......@@ -1065,7 +1065,8 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
/**
* 计算使用优惠券后的订单金额
*/
private void computedCouponPrice(ComputedOrderPriceResponse priceResponse, SConsumer user, SConsumerCoupon consumerCoupon) {
private void computedCouponPrice(OrderComputedPriceRequest request, ComputedOrderPriceResponse priceResponse
, SConsumer user, SConsumerCoupon consumerCoupon) {
if (ObjectUtil.isNull(consumerCoupon) || !consumerCoupon.getConsumerId().equals(user.getId())) {
throw new BaseException("优惠券领取记录不存在!");
}
......@@ -1085,7 +1086,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
case DISCOUNT://TODO 折扣券
break;
case PACKAGE://套餐券
case CASH://现金券
case CASH://现金券(团购券)
//判断优惠券最小使用金额
if (consumerCoupon.getMinPrice().compareTo(priceResponse.getTotalFee()) > 0) {
throw new BaseException("总金额小于优惠券最小使用金额");
......@@ -1099,7 +1100,19 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
priceResponse.setPayFee(subtract);
}
break;
case DURATION://TODO 时长券
case DURATION://时长券
BigDecimal timeLong = DateUtils.differentHour(request.getPreStartDate(), request.getPreEndDate());
if(new BigDecimal(consumerCoupon.getMinDuration()).compareTo(timeLong)>0){
throw new BaseException("总时长小于优惠券最小使用时长");
}
BigDecimal duration = new BigDecimal(consumerCoupon.getDuration());
if(duration.compareTo(timeLong)>=0){
priceResponse.setPayFee(BigDecimal.ZERO);
} else {
BigDecimal unitPrice = priceResponse.getTotalFee().divide(timeLong);
priceResponse.setPayFee(unitPrice.multiply(timeLong.subtract(duration)));
priceResponse.setCouponFee(priceResponse.getTotalFee().subtract(priceResponse.getPayFee()));
}
break;
default:
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment