订单 优惠券部分 重写
This commit is contained in:
@@ -14,5 +14,18 @@
|
||||
<dependencies>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>21</source>
|
||||
<target>21</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,30 +1,62 @@
|
||||
package com.czg.service.market.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.exceptions.ValidateException;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.account.entity.ShopUser;
|
||||
import com.czg.account.service.ShopInfoService;
|
||||
import com.czg.account.service.ShopUserService;
|
||||
import com.czg.account.vo.UserCouponVo;
|
||||
import com.czg.market.dto.ShopCouponDTO;
|
||||
import com.czg.market.entity.MkShopCouponRecord;
|
||||
import com.czg.market.entity.ShopCoupon;
|
||||
import com.czg.market.service.MkCouponGiftService;
|
||||
import com.czg.market.service.MkShopCouponRecordService;
|
||||
import com.czg.market.service.ShopCouponService;
|
||||
import com.czg.product.service.ProductService;
|
||||
import com.czg.service.market.mapper.ShopCouponMapper;
|
||||
import com.czg.utils.AssertUtil;
|
||||
import com.czg.utils.PageUtil;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.mybatisflex.core.paginate.Page;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.lang.StringTemplate.STR;
|
||||
|
||||
/**
|
||||
* 优惠券信息表 服务层实现。
|
||||
*
|
||||
* @author ww
|
||||
* @since 2025-09-11
|
||||
*/
|
||||
@Slf4j
|
||||
@DubboService
|
||||
public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCoupon> implements ShopCouponService {
|
||||
@Resource
|
||||
private MkCouponGiftService couponGiftService;
|
||||
@Resource
|
||||
private MkShopCouponRecordService recordService;
|
||||
@Resource
|
||||
private ShopUserService shopUserService;
|
||||
@Resource
|
||||
private ShopInfoService shopInfoService;
|
||||
@DubboReference
|
||||
private ProductService productService;
|
||||
|
||||
@Override
|
||||
public Page<ShopCouponDTO> getCouponPage(ShopCouponDTO param) {
|
||||
@@ -81,4 +113,129 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
|
||||
updateById(new ShopCoupon().setIsDel(1).setId(id), true);
|
||||
couponGiftService.deleteJoinCouponGift(id, 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<MkShopCouponRecord> find(Long userId, Long shopId, Integer status) {
|
||||
Page<Object> page = PageUtil.buildPage();
|
||||
List<Long> shopUserIds = shopUserService.queryChain()
|
||||
.eq(ShopUser::getUserId, userId)
|
||||
.eq(ShopUser::getShopId, shopId)
|
||||
.select(ShopUser::getId).listAs(Long.class);
|
||||
if (CollectionUtil.isNotEmpty(shopUserIds)) {
|
||||
PageHelper.startPage(Math.toIntExact(page.getPageNumber()), Math.toIntExact(page.getPageSize()));
|
||||
return PageUtil.convert(new PageInfo<>(recordService.findByUser(shopUserIds, status)));
|
||||
}
|
||||
return new Page<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserCouponVo> findCoupon(Long shopId, Long shopUserId, Integer type) {
|
||||
List<UserCouponVo> tbUserCouponVos = recordService.queryByVipIdAndShopId(shopId, shopUserId, type);
|
||||
if (CollectionUtil.isNotEmpty(tbUserCouponVos)) {
|
||||
String week = DateUtil.dayOfWeekEnum(new Date()).toChinese("周");
|
||||
LocalTime now = LocalTime.now();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
|
||||
//券id 券使用描述
|
||||
Map<Long, JSONObject> coupons = new HashMap<>();
|
||||
for (UserCouponVo tbUserCouponVo : tbUserCouponVos) {
|
||||
if (!coupons.containsKey(tbUserCouponVo.getCouponId())) {
|
||||
setCouponInfo(coupons, tbUserCouponVo, null, week, now, formatter);
|
||||
}
|
||||
JSONObject couponJson = coupons.get(tbUserCouponVo.getCouponId());
|
||||
tbUserCouponVo.setUseRestrictions(couponJson.getString("useRestrictions"));
|
||||
tbUserCouponVo.setUse(couponJson.getBoolean("isUse"));
|
||||
|
||||
}
|
||||
tbUserCouponVos.sort(Comparator.comparing(UserCouponVo::isUse).reversed());
|
||||
return tbUserCouponVos;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean use(List<Long> ids, Long shopUserId, Long orderId) {
|
||||
List<MkShopCouponRecord> records = recordService.listByIds(ids);
|
||||
if (records.isEmpty()) {
|
||||
log.error("优惠券使用失败,订单Id:{}", orderId);
|
||||
return false;
|
||||
}
|
||||
// 使用流来统计 couponId 出现的次数
|
||||
Map<Long, Long> couponIdCountMap = records.stream()
|
||||
.collect(Collectors.groupingBy(MkShopCouponRecord::getCouponId,
|
||||
Collectors.counting()
|
||||
));
|
||||
couponIdCountMap.forEach((couponId, count) -> {
|
||||
ShopCoupon tbShopCoupon = getById(couponId);
|
||||
tbShopCoupon.setUseNum(tbShopCoupon.getUseNum() + count.intValue());
|
||||
ShopCoupon coupon1 = new ShopCoupon();
|
||||
coupon1.setId(couponId);
|
||||
coupon1.setUseNum(tbShopCoupon.getUseNum());
|
||||
updateById(coupon1);
|
||||
});
|
||||
return recordService.updateChain()
|
||||
.set(MkShopCouponRecord::getStatus, 1)
|
||||
.set(MkShopCouponRecord::getTargetId, orderId)
|
||||
.eq(MkShopCouponRecord::getShopUserId, shopUserId)
|
||||
.in(MkShopCouponRecord::getId, ids).update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 退还券
|
||||
*/
|
||||
@Override
|
||||
public Boolean refund(Long orderId, Long shopUserId) {
|
||||
return recordService.updateChain()
|
||||
.set(MkShopCouponRecord::getStatus, 0)
|
||||
.eq(MkShopCouponRecord::getShopUserId, shopUserId)
|
||||
.eq(MkShopCouponRecord::getTargetId, orderId)
|
||||
.update();
|
||||
}
|
||||
|
||||
private void setCouponInfo(Map<Long, JSONObject> coupons, UserCouponVo tbUserCouponVo, BigDecimal amount, String week, LocalTime now, DateTimeFormatter formatter) {
|
||||
JSONObject json = new JSONObject();
|
||||
boolean isUse = true;
|
||||
ShopCoupon tbShopCoupon = getById(tbUserCouponVo.getCouponId());
|
||||
StringBuilder useRestrictions = new StringBuilder("每天 ");
|
||||
if (amount != null && tbShopCoupon.getCouponType().equals(1)) {
|
||||
if (amount.compareTo(tbShopCoupon.getFullAmount()) < 0) {
|
||||
isUse = false;
|
||||
}
|
||||
}
|
||||
if (StrUtil.isNotBlank(tbShopCoupon.getUseDays())) {
|
||||
String[] split = tbShopCoupon.getUseDays().split(",");
|
||||
if (split.length != 7) {
|
||||
useRestrictions = new StringBuilder(STR."\{tbShopCoupon.getUseDays()} ");
|
||||
}
|
||||
if (!tbShopCoupon.getUseDays().contains(week)) {
|
||||
isUse = false;
|
||||
}
|
||||
}
|
||||
if ("custom".equals(tbShopCoupon.getUseTimeType())) {
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
LocalTime startTime = tbShopCoupon.getUseStartTime().toLocalTime();
|
||||
LocalTime endTime = tbShopCoupon.getUseEndTime().toLocalTime();
|
||||
if (currentTime.isBefore(startTime)) {
|
||||
if (startTime.isBefore(endTime)) {
|
||||
isUse = false;
|
||||
} else if (currentTime.isAfter(endTime)) {
|
||||
isUse = false;
|
||||
}
|
||||
} else if (startTime.isBefore(endTime)) {
|
||||
if (currentTime.isAfter(endTime)) {
|
||||
isUse = false;
|
||||
}
|
||||
}
|
||||
useRestrictions.append(
|
||||
STR."\{startTime.format(formatter)}-\{endTime.format(formatter)}");
|
||||
} else {
|
||||
useRestrictions.append("全时段");
|
||||
}
|
||||
useRestrictions.append(" 可用");
|
||||
json.put("isUse", isUse);
|
||||
json.put("useRestrictions", useRestrictions);
|
||||
|
||||
coupons.put(tbUserCouponVo.getCouponId(), json);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
<if test="status != null">
|
||||
and mk_shop_coupon_record.status = #{status}
|
||||
</if>
|
||||
and mk_shop_coupon_record.is_del = 0
|
||||
order by mk_shop_coupon_record.status , mk_shop_coupon_record.create_time desc
|
||||
</select>
|
||||
|
||||
@@ -78,6 +79,7 @@
|
||||
and inRecord.status = 0
|
||||
and inRecord.use_start_time < now()
|
||||
and inRecord.use_end_time > now()
|
||||
and inRecord.is_del = 0
|
||||
order by inRecord.use_end_time
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user