|
|
@@ -0,0 +1,579 @@
|
|
|
+package shop.alien.storeplatform.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import shop.alien.entity.result.R;
|
|
|
+import shop.alien.entity.store.*;
|
|
|
+import shop.alien.entity.store.dto.LifeDiscountCouponStoreFriendDto;
|
|
|
+import shop.alien.entity.store.vo.LifeCouponStatusVo;
|
|
|
+import shop.alien.mapper.*;
|
|
|
+import shop.alien.storeplatform.service.LifeCouponPlatformService;
|
|
|
+import shop.alien.util.common.UniqueRandomNumGenerator;
|
|
|
+import shop.alien.util.common.constant.OrderStatusEnum;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.time.*;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.format.TextStyle;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 优惠券
|
|
|
+ *
|
|
|
+ * @author ssk
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2024/12/23 15:08
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class LifeCouponPlatformServiceImpl extends ServiceImpl<LifeCouponMapper, LifeCoupon> implements LifeCouponPlatformService {
|
|
|
+
|
|
|
+ private final LifeCouponMapper lifeCouponMapper;
|
|
|
+
|
|
|
+ private final LifeUserOrderMapper lifeUserOrderMapper;
|
|
|
+
|
|
|
+ private final StoreIncomeDetailsRecordMapper storeIncomeDetailsRecordMapper;
|
|
|
+
|
|
|
+ private final StoreDictionaryMapper storeDictionaryMapper;
|
|
|
+
|
|
|
+ private final EssentialHolidayComparisonMapper essentialHolidayComparisonMapper;
|
|
|
+
|
|
|
+ private final StoreUserMapper storeUserMapper;
|
|
|
+
|
|
|
+ private final OrderCouponMiddleMapper orderCouponMiddleMapper;
|
|
|
+
|
|
|
+ private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;
|
|
|
+
|
|
|
+ private final LifeDiscountCouponMapper lifeDiscountCouponMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LifeCoupon addOrUpdateCoupon(LifeCoupon lifeCoupon) {
|
|
|
+
|
|
|
+ // 添加优惠tag
|
|
|
+ //获取经营板块id
|
|
|
+// Integer businessSection = lifeCoupon.getBusinessSection();
|
|
|
+ //查询经营板块名称
|
|
|
+ StoreDictionary youhuiName = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getDictId, "180").eq(StoreDictionary::getTypeName, "youhui"));
|
|
|
+ //查询经营种类
|
|
|
+ List<String> discountTypes = lifeCoupon.getTempDiscountTag();
|
|
|
+ List<String> businessTypeNames = new ArrayList<>();
|
|
|
+ //获取经营种类名称
|
|
|
+ if (!CollectionUtils.isEmpty(discountTypes)) {
|
|
|
+ for (String businessType : discountTypes) {
|
|
|
+ StoreDictionary storeDictionary =
|
|
|
+ storeDictionaryMapper
|
|
|
+ .selectOne(
|
|
|
+ new LambdaQueryWrapper<StoreDictionary>()
|
|
|
+ .eq(StoreDictionary::getDictId, businessType).eq(StoreDictionary::getParentId, youhuiName.getId()));
|
|
|
+ businessTypeNames.add(storeDictionary.getDictDetail());
|
|
|
+ }
|
|
|
+ lifeCoupon.setDiscountTag(String.join(",", discountTypes));
|
|
|
+ lifeCoupon.setDiscountTagName(String.join(",", businessTypeNames));
|
|
|
+ }
|
|
|
+
|
|
|
+// int status = 0;
|
|
|
+// Date now = new Date();
|
|
|
+// Date startDate = lifeCoupon.getStartDate();
|
|
|
+// Date endDate = lifeCoupon.getEndDate();
|
|
|
+// if (now.compareTo(startDate) >= 0 && now.compareTo(endDate) <= 0) {
|
|
|
+// status = 1;
|
|
|
+// } else if (now.compareTo(startDate) < 0) {
|
|
|
+// status = 0;
|
|
|
+// } else if (now.compareTo(endDate) >= 0) {
|
|
|
+// status = 3;
|
|
|
+// }
|
|
|
+ lifeCoupon.setStatus(1);
|
|
|
+ if (StringUtils.isEmpty(lifeCoupon.getId())) {
|
|
|
+ lifeCoupon.setType(1);
|
|
|
+ lifeCoupon.setCouponCode(UniqueRandomNumGenerator.generateUniqueCode(12));
|
|
|
+ this.save(lifeCoupon);
|
|
|
+ } else {
|
|
|
+ this.updateById(lifeCoupon);
|
|
|
+ }
|
|
|
+ return lifeCoupon;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改库存数量
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ * @param singleQty 库存数量
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean updateCouponSingleQty(Integer id, Integer singleQty) {
|
|
|
+ //判断库存数为0 增加库存数修改状态为进行中
|
|
|
+ LambdaQueryWrapper<LifeCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ lambdaQueryWrapper.eq(LifeCoupon :: getId, id);
|
|
|
+ LifeCoupon lifeCoupon = lifeCouponMapper.selectOne(lambdaQueryWrapper);
|
|
|
+
|
|
|
+ if(lifeCoupon.getSingleQty() == 0){
|
|
|
+ LambdaUpdateWrapper<LifeCoupon> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ lambdaUpdateWrapper.eq(LifeCoupon :: getId, id);
|
|
|
+ lambdaUpdateWrapper.set(LifeCoupon :: getSingleQty,singleQty);
|
|
|
+ lambdaUpdateWrapper.set(LifeCoupon :: getStatus, 5);
|
|
|
+ lifeCouponMapper.update(null, lambdaUpdateWrapper);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ LambdaUpdateWrapper<LifeCoupon> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ lambdaUpdateWrapper.eq(LifeCoupon :: getId, id);
|
|
|
+ lambdaUpdateWrapper.set(LifeCoupon :: getSingleQty,singleQty);
|
|
|
+ lifeCouponMapper.update(null, lambdaUpdateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<LifeCoupon> getCouponList(int page, int size, String storeId, String status, String name, Integer dataType) {
|
|
|
+ LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(storeId != null && !storeId.isEmpty(), LifeCoupon::getStoreId, storeId);
|
|
|
+ wrapper.eq(status != null && !status.isEmpty(), LifeCoupon::getStatus, status);
|
|
|
+ wrapper.like(name != null && !name.isEmpty(), LifeCoupon::getName, name);
|
|
|
+ wrapper.eq(LifeCoupon::getType, 1);
|
|
|
+ wrapper.eq(LifeCoupon::getDataType, dataType);
|
|
|
+// 修改排序 按照status(0草稿/1待审核/2未开始/3审核拒绝/4已售罄/5进行中/6已下架/7已结束/8=2+手动下架) 审核拒绝>待审核>进行中>已售罄,已下架,已结束(同等优先级的情况下,按照审核时间的倒序显示)
|
|
|
+ wrapper.last("ORDER BY CASE " +
|
|
|
+ "WHEN status = 3 THEN 0 " + // 审核拒绝
|
|
|
+ "WHEN status = 1 THEN 1 " + // 待审核
|
|
|
+ "WHEN status = 5 THEN 2 " + // 进行中
|
|
|
+ "WHEN status IN (4, 6, 7) THEN 3 " + // 已售罄、已下架、已结束
|
|
|
+ "ELSE 4 END ASC, " +
|
|
|
+ "created_time DESC");
|
|
|
+ IPage<LifeCoupon> lifeCouponIPage = new Page<>(page, size);
|
|
|
+ return lifeCouponMapper.selectPage(lifeCouponIPage, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int updateCoupon(String id, Integer status) {
|
|
|
+ LifeCoupon coupon = lifeCouponMapper.selectById(id);
|
|
|
+ if (null == coupon) {
|
|
|
+ throw new RuntimeException("该券不存在");
|
|
|
+ }
|
|
|
+ // 已结束
|
|
|
+ if (LocalDate.now().isAfter(coupon.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())) {
|
|
|
+ status = 7;
|
|
|
+ }
|
|
|
+ LifeCoupon lifeCoupon = new LifeCoupon();
|
|
|
+ lifeCoupon.setId(id);
|
|
|
+ lifeCoupon.setStatus(status);
|
|
|
+ return lifeCouponMapper.updateById(lifeCoupon);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验券
|
|
|
+ *
|
|
|
+ * @param storeId
|
|
|
+ * @param quanCode
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String, String> couponVerify(String storeId, String quanCode) {
|
|
|
+ LambdaUpdateWrapper<LifeUserOrder> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(LifeUserOrder::getQuanCode, quanCode);
|
|
|
+ LifeUserOrder order = lifeUserOrderMapper.selectOne(wrapper);
|
|
|
+ Map<String, String> resultMap = new HashMap<>();
|
|
|
+ String errMessage = "";
|
|
|
+ if (order == null) {
|
|
|
+ errMessage = "没有这张券";
|
|
|
+ } else {
|
|
|
+ if (order.getStatus() == 5) {
|
|
|
+ errMessage = "该券已核销";
|
|
|
+ }
|
|
|
+ if (order.getStatus() == 6) {
|
|
|
+ errMessage = "该券已过期";
|
|
|
+ }
|
|
|
+ if (order.getStatus() == 7) {
|
|
|
+ errMessage = "该券待退款";
|
|
|
+ }
|
|
|
+ if (order.getStatus() == 4) {
|
|
|
+ errMessage = "该券已退款";
|
|
|
+ }
|
|
|
+ if (!order.getStoreId().equals(storeId)) {
|
|
|
+ errMessage = "核销商家与券不一致";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!StringUtils.isEmpty(errMessage)) {
|
|
|
+ resultMap.put("code", "false");
|
|
|
+ resultMap.put("message", errMessage);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ order.setStatus(5);
|
|
|
+ order.setUsedTime(new Date());
|
|
|
+ lifeUserOrderMapper.updateById(order);
|
|
|
+
|
|
|
+ LambdaUpdateWrapper<LifeCoupon> couponWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ couponWrapper.eq(LifeCoupon::getId, order.getQuanId());
|
|
|
+ LifeCoupon coupon = lifeCouponMapper.selectOne(couponWrapper);
|
|
|
+ // TODO 抽成比例应该从商户里取
|
|
|
+ BigDecimal amounts = new BigDecimal(order.getFinalPrice()).multiply(new BigDecimal(100));
|
|
|
+ BigDecimal commission = amounts.multiply(new BigDecimal(0.04)).setScale(0, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal money = amounts.subtract(commission);
|
|
|
+
|
|
|
+ // 插入收入明细表数据
|
|
|
+ StoreIncomeDetailsRecord record = new StoreIncomeDetailsRecord();
|
|
|
+ record.setStoreId(Integer.parseInt(storeId));
|
|
|
+ record.setUserOrderId(Integer.parseInt(order.getId()));
|
|
|
+ record.setIncomeType(coupon.getType());
|
|
|
+ record.setBusinessId(Integer.parseInt(order.getQuanId()));
|
|
|
+ record.setCommission(commission.intValue());
|
|
|
+ record.setMoney(money.intValue());
|
|
|
+ storeIncomeDetailsRecordMapper.insert(record);
|
|
|
+ // 店铺账户余额增加
|
|
|
+ UpdateWrapper<StoreUser> updateWrapper = new UpdateWrapper();
|
|
|
+ updateWrapper.eq("store_id", storeId);
|
|
|
+ updateWrapper.eq("delete_flag", 0);
|
|
|
+ updateWrapper.setSql("money = money + " + money);
|
|
|
+ storeUserMapper.update(null, updateWrapper);
|
|
|
+ //发放好友优惠券
|
|
|
+ LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto = new LifeDiscountCouponStoreFriendDto();
|
|
|
+ lifeDiscountCouponStoreFriendDto.setOrderId(Integer.parseInt(order.getId()));
|
|
|
+// lifeDiscountCouponStoreFriendService.issueFriendCoupon(lifeDiscountCouponStoreFriendDto);
|
|
|
+ resultMap.put("code", "true");
|
|
|
+ resultMap.put("message", "核销成功");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 获取代金券状态
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author zhangchen
|
|
|
+ * @since 2025-07-14
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public LifeCouponStatusVo getCouponStatus(String couponId) {
|
|
|
+ if (couponId == null || couponId.isEmpty()) {
|
|
|
+ log.error("获取代金券状态失败:couponId为空");
|
|
|
+ throw new IllegalArgumentException("couponId不能为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ LifeCoupon coupon = lifeCouponMapper.selectById(couponId);
|
|
|
+ LifeCouponStatusVo lifeCouponStatusVo = new LifeCouponStatusVo();
|
|
|
+ if (coupon != null) {
|
|
|
+ lifeCouponStatusVo.setCouponId(couponId);
|
|
|
+ lifeCouponStatusVo.setSingleQty(coupon.getSingleQty());
|
|
|
+ lifeCouponStatusVo.setBuyLimit(coupon.getBuyLimit());
|
|
|
+ } else {
|
|
|
+ // 可选:设置默认值或标记状态为无效
|
|
|
+ lifeCouponStatusVo.setSingleQty(0); // 或其他默认逻辑
|
|
|
+ }
|
|
|
+ return lifeCouponStatusVo;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取代金券状态失败: {}", e);
|
|
|
+ throw new RuntimeException("获取代金券状态失败", e); // 或者根据项目规范处理
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<EssentialHolidayComparison> getHolidayList(int page, int size, String year, String openFlag, String holidayName) {
|
|
|
+ if (StringUtils.isEmpty(year)) {
|
|
|
+ LambdaQueryWrapper<EssentialHolidayComparison> essentialHolidayComparisonLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ essentialHolidayComparisonLambdaQueryWrapper.eq(EssentialHolidayComparison::getDelFlag, 0).groupBy(EssentialHolidayComparison::getParticularYear).orderByDesc(EssentialHolidayComparison::getParticularYear);
|
|
|
+ IPage<EssentialHolidayComparison> essentialHolidayComparisonIPage = new Page<>(page, size);
|
|
|
+ return essentialHolidayComparisonMapper.selectPage(essentialHolidayComparisonIPage, essentialHolidayComparisonLambdaQueryWrapper);
|
|
|
+ } else {
|
|
|
+ LambdaQueryWrapper<EssentialHolidayComparison> essentialHolidayComparisonLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ essentialHolidayComparisonLambdaQueryWrapper.eq(EssentialHolidayComparison::getDelFlag, 0)
|
|
|
+ .eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(year), EssentialHolidayComparison::getParticularYear, year)
|
|
|
+ .eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(openFlag), EssentialHolidayComparison::getOpenFlag, openFlag)
|
|
|
+ .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(holidayName), EssentialHolidayComparison::getFestivalName, holidayName)
|
|
|
+ .orderByAsc(EssentialHolidayComparison::getStartTime);
|
|
|
+ IPage<EssentialHolidayComparison> essentialHolidayComparisonIPage = new Page<>(page, size);
|
|
|
+ return essentialHolidayComparisonMapper.selectPage(essentialHolidayComparisonIPage, essentialHolidayComparisonLambdaQueryWrapper);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean saveHoliday(EssentialHolidayComparison essentialHolidayComparison) {
|
|
|
+ if (ObjectUtils.isNotEmpty(essentialHolidayComparison.getId())) {
|
|
|
+ if (ObjectUtils.isNotEmpty(essentialHolidayComparison.getStartTime())) {
|
|
|
+ essentialHolidayComparison.setFestivalDate(essentialHolidayComparison.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
|
|
|
+ }
|
|
|
+ essentialHolidayComparisonMapper.updateById(essentialHolidayComparison);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ if (ObjectUtils.isNotEmpty(essentialHolidayComparison.getStartTime())) {
|
|
|
+ essentialHolidayComparison.setFestivalDate(essentialHolidayComparison.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
|
|
|
+ }
|
|
|
+ essentialHolidayComparisonMapper.insert(essentialHolidayComparison);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean delHoliday(String id) {
|
|
|
+ essentialHolidayComparisonMapper.update(null, new LambdaUpdateWrapper<EssentialHolidayComparison>().eq(EssentialHolidayComparison::getId, id).set(EssentialHolidayComparison::getDelFlag, 1));
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean delHolidayByYear(String year) {
|
|
|
+ essentialHolidayComparisonMapper.update(null, new LambdaUpdateWrapper<EssentialHolidayComparison>().eq(EssentialHolidayComparison::getParticularYear, year).set(EssentialHolidayComparison::getDelFlag, 1));
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean openCloseHoliday(String id) {
|
|
|
+ EssentialHolidayComparison essentialHolidayComparison = essentialHolidayComparisonMapper.selectById(id);
|
|
|
+ if (essentialHolidayComparison.getOpenFlag() == 0) {
|
|
|
+ essentialHolidayComparisonMapper.update(null, new LambdaUpdateWrapper<EssentialHolidayComparison>().eq(EssentialHolidayComparison::getId, id).set(EssentialHolidayComparison::getOpenFlag, 1));
|
|
|
+ } else {
|
|
|
+ essentialHolidayComparisonMapper.update(null, new LambdaUpdateWrapper<EssentialHolidayComparison>().eq(EssentialHolidayComparison::getId, id).set(EssentialHolidayComparison::getOpenFlag, 0));
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 核销订单前效验
|
|
|
+ *
|
|
|
+ * @param orderCode
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<String> orderVerify(String orderCode) {
|
|
|
+ OrderCouponMiddle orderCouponMiddle = orderCouponMiddleMapper.selectOne(new LambdaQueryWrapper<OrderCouponMiddle>().eq(OrderCouponMiddle::getCouponCode, orderCode));
|
|
|
+ if (!StringUtils.isEmpty(orderCouponMiddle) && orderCouponMiddle.getStatus() == 1) {
|
|
|
+ LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectOne(new LambdaQueryWrapper<LifeUserOrder>().eq(LifeUserOrder::getId, orderCouponMiddle.getOrderId()));
|
|
|
+ //类型为:1 代金券订单
|
|
|
+ if (lifeUserOrder.getCouponType() == 1) {
|
|
|
+ return couponVerification(orderCouponMiddle);
|
|
|
+ } else if (lifeUserOrder.getCouponType() == 2) {//类型为:2 团购订单
|
|
|
+ return groupVerification(orderCouponMiddle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.fail("该劵不是待使用状态");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isCurrentDateInAnyRange(String[] dateRanges) {
|
|
|
+ // 获取当前日期
|
|
|
+ LocalDate currentDate = LocalDate.now();
|
|
|
+ // 定义日期格式
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+
|
|
|
+ // 遍历数组中的每一对日期范围
|
|
|
+ for (String range : dateRanges) {
|
|
|
+ // 分割每一对日期(开始日期和结束日期)
|
|
|
+ String[] dates = range.split(",");
|
|
|
+ if (dates.length != 2) {
|
|
|
+ // 格式不正确,跳过这一对
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 解析开始日期和结束日期
|
|
|
+ LocalDate startDate = LocalDate.parse(dates[0], formatter);
|
|
|
+ LocalDate endDate = LocalDate.parse(dates[1], formatter);
|
|
|
+
|
|
|
+ // 检查当前日期是否在范围内(包括开始和结束日期)
|
|
|
+ if (!currentDate.isBefore(startDate) &&
|
|
|
+ !currentDate.isAfter(endDate)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 日期解析错误,跳过这一对
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 没有任何一对日期范围包含当前日期
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public R<String> groupVerification(OrderCouponMiddle orderCouponMiddle) {
|
|
|
+ LifeGroupBuyMain lifeGroupBuyMain = lifeGroupBuyMainMapper.selectOne(new LambdaQueryWrapper<LifeGroupBuyMain>().eq(LifeGroupBuyMain::getId, orderCouponMiddle.getCouponId()));
|
|
|
+ //团购有效期类型为:0 指定天数
|
|
|
+ if (lifeGroupBuyMain.getEffectiveDateType() == 0) {
|
|
|
+ //订单支付时间加上指定天数 为团购劵有效期
|
|
|
+ LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectOne(new LambdaQueryWrapper<LifeUserOrder>().eq(LifeUserOrder::getId, orderCouponMiddle.getOrderId()));
|
|
|
+ LocalDate localDate = lifeUserOrder.getPayTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
+ LocalDate validityPeriod = localDate.plusDays(Long.parseLong(lifeGroupBuyMain.getEffectiveDateValue()));
|
|
|
+ LocalDate nowDate = LocalDate.now(); // 获取当前时间
|
|
|
+ if (nowDate.isAfter(validityPeriod)) {
|
|
|
+ return R.fail("该劵不在有效期内");
|
|
|
+ }
|
|
|
+ } else if (lifeGroupBuyMain.getEffectiveDateType() == 1) {//类型为:1 指定时间段
|
|
|
+ String[] strings = lifeGroupBuyMain.getEffectiveDateValue().split(",");
|
|
|
+ String startDate = strings[0];
|
|
|
+ String endDate = strings[1];
|
|
|
+ LocalDate localStartDate = LocalDate.parse(startDate);
|
|
|
+ LocalDate localEndDate = LocalDate.parse(endDate);
|
|
|
+ LocalDate nowDate = LocalDate.now(); // 获取当前时间
|
|
|
+ if (nowDate.isAfter(localEndDate) || nowDate.isBefore(localStartDate)) {
|
|
|
+ return R.fail("该劵不在有效期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断订单是否在不可用日期内
|
|
|
+ //判断当前日期是否在不可用星期
|
|
|
+ if (lifeGroupBuyMain.getDisableDateType() == 1) {//限制日期: 1234567;节日id
|
|
|
+ LocalDate nowDate = LocalDate.now(); // 获取当前时间
|
|
|
+ DayOfWeek dayOfWeek = nowDate.getDayOfWeek();
|
|
|
+ String week = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
|
|
|
+ String beforeSemicolon = lifeGroupBuyMain.getDisableDateValue().split(";")[0];
|
|
|
+ if (!StringUtils.isEmpty(beforeSemicolon)) {
|
|
|
+ List<String> collectUnavailableDate = Arrays.stream(beforeSemicolon.split(",")).map(String::trim).collect(Collectors.toList());
|
|
|
+ boolean isExist = collectUnavailableDate.stream().anyMatch(s -> s.equals(week));
|
|
|
+ if (isExist) {
|
|
|
+ return R.fail("该劵在不可用日期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断当前日期是否在不可用节日
|
|
|
+ String[] strings = lifeGroupBuyMain.getDisableDateValue().split(";");
|
|
|
+ if (strings.length > 1) {
|
|
|
+ String afterSemicolon = lifeGroupBuyMain.getDisableDateValue().split(";")[1];
|
|
|
+ List<String> collectUnavailableDate = Arrays.stream(afterSemicolon.split(",")).map(String::trim).collect(Collectors.toList());
|
|
|
+ List<EssentialHolidayComparison> essentialHolidayComparisons = essentialHolidayComparisonMapper.
|
|
|
+ selectList(new LambdaQueryWrapper<EssentialHolidayComparison>().in(EssentialHolidayComparison::getId, collectUnavailableDate));
|
|
|
+ boolean isExist = essentialHolidayComparisons.stream().anyMatch(s -> !nowDate.isBefore(s.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
|
|
|
+ && !nowDate.isAfter(s.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()));
|
|
|
+ if (isExist) {
|
|
|
+ return R.fail("该劵在不可用日期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断当前日期是否在自定义不可用日期内
|
|
|
+ if (lifeGroupBuyMain.getDisableDateType() == 2) {
|
|
|
+ String[] customDate = lifeGroupBuyMain.getDisableDateValue().split(";");
|
|
|
+ boolean isExist = isCurrentDateInAnyRange(customDate);
|
|
|
+ if (isExist) {
|
|
|
+ return R.fail("该劵在不可用日期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.success("效验通过");
|
|
|
+ }
|
|
|
+
|
|
|
+ public R<String> couponVerification(OrderCouponMiddle orderCouponMiddle) {
|
|
|
+ LifeCoupon lifeCoupon = lifeCouponMapper.selectOne(new LambdaQueryWrapper<LifeCoupon>().eq(LifeCoupon::getId, orderCouponMiddle.getCouponId()));
|
|
|
+ //代金券有效期类型为:1 指定天数
|
|
|
+ if (lifeCoupon.getExpirationType().equals("1")) {
|
|
|
+ //订单支付时间加上指定天数 为团购劵有效期
|
|
|
+ LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectOne(new LambdaQueryWrapper<LifeUserOrder>().eq(LifeUserOrder::getId, orderCouponMiddle.getOrderId()));
|
|
|
+ LocalDate localDate = lifeUserOrder.getPayTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
+ LocalDate validityPeriod = localDate.plusDays(lifeCoupon.getExpirationDate());
|
|
|
+ LocalDate nowDate = LocalDate.now(); // 获取当前时间
|
|
|
+ if (nowDate.isAfter(validityPeriod)) {
|
|
|
+ return R.fail("该劵不在有效期内");
|
|
|
+ }
|
|
|
+ } else if (lifeCoupon.getExpirationType().equals("2")) {//类型为:2 指定时间段
|
|
|
+ String[] strings = lifeCoupon.getValidityPeriod().split(",");
|
|
|
+ long start = Long.parseLong(strings[0]);
|
|
|
+ long end = Long.parseLong(strings[1]);
|
|
|
+ LocalDate startDate = Instant.ofEpochMilli(start)
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
+ .toLocalDate();
|
|
|
+ LocalDate endDate = Instant.ofEpochMilli(end)
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
+ .toLocalDate();
|
|
|
+ LocalDate nowDate = LocalDate.now(); // 获取当前时间
|
|
|
+ if (nowDate.isAfter(endDate) || nowDate.isBefore(startDate)) {
|
|
|
+ return R.fail("该劵不在有效期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断代金券订单是否在不可用日期内
|
|
|
+ //判断当前日期是否在不可用星期
|
|
|
+ if (lifeCoupon.getUnusedType().equals("2")) {//限制日期: 1234567;节日id
|
|
|
+ LocalDate nowDate = LocalDate.now(); // 获取当前时间
|
|
|
+ DayOfWeek dayOfWeek = nowDate.getDayOfWeek();
|
|
|
+ String week = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
|
|
|
+ String beforeSemicolon = lifeCoupon.getUnavaiLableDate().split(";")[0];
|
|
|
+ if (!StringUtils.isEmpty(beforeSemicolon)) {
|
|
|
+ List<String> collectUnavailableDate = Arrays.stream(beforeSemicolon.split(",")).map(String::trim).collect(Collectors.toList());
|
|
|
+ boolean isExist = collectUnavailableDate.stream().anyMatch(s -> s.equals(week));
|
|
|
+ if (isExist) {
|
|
|
+ return R.fail("该劵在不可用日期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断当前日期是否在不可用节日
|
|
|
+ String[] strings = lifeCoupon.getUnavaiLableDate().split(";");
|
|
|
+ if (strings.length > 1) {
|
|
|
+ String afterSemicolon = lifeCoupon.getUnavaiLableDate().split(";")[1];
|
|
|
+ List<String> collectUnavailableDate = Arrays.stream(afterSemicolon.split(",")).map(String::trim).collect(Collectors.toList());
|
|
|
+ List<EssentialHolidayComparison> essentialHolidayComparisons = essentialHolidayComparisonMapper.
|
|
|
+ selectList(new LambdaQueryWrapper<EssentialHolidayComparison>().in(EssentialHolidayComparison::getId, collectUnavailableDate));
|
|
|
+ boolean isExist = essentialHolidayComparisons.stream().anyMatch(s -> !nowDate.isBefore(s.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
|
|
|
+ && !nowDate.isAfter(s.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()));
|
|
|
+ if (isExist) {
|
|
|
+ return R.fail("该劵在不可用日期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断当前日期是否在自定义不可用日期内
|
|
|
+ if (lifeCoupon.getUnusedType().equals("3")) {
|
|
|
+ String[] customDate = lifeCoupon.getUnavaiLableDate().split(";");
|
|
|
+ boolean isExist = isCurrentDateInAnyRange(customDate);
|
|
|
+ if (isExist) {
|
|
|
+ return R.fail("该劵在不可用日期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /* //有效期到
|
|
|
+ Date date = DateUtils.calcDays(orderCouponMiddle.getCreatedTime(), lifeCoupon.getExpirationDate());
|
|
|
+ //开始时间段
|
|
|
+ int beginUseTime = Integer.parseInt(lifeCoupon.getBuyUseStartTime());
|
|
|
+ //结束时间段
|
|
|
+ int endUseTime = Integer.parseInt(lifeCoupon.getBuyUseEndTime());
|
|
|
+ if (new Date().after(date)) {
|
|
|
+ return R.fail("该劵已过期");
|
|
|
+ }
|
|
|
+ if (endUseTime < beginUseTime) {
|
|
|
+ if (!(LocalDateTime.now().getHour() >= beginUseTime || LocalDateTime.now().getHour() < endUseTime)) {
|
|
|
+ return R.fail("该劵未到使用时间");
|
|
|
+ }
|
|
|
+ } else if (endUseTime > beginUseTime) {
|
|
|
+ if (LocalDateTime.now().getHour() < beginUseTime || LocalDateTime.now().getHour() >= endUseTime) {
|
|
|
+ return R.fail("该劵未到使用时间");
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+ // 判断是否在使用时间内
|
|
|
+ Integer buyUseStartTime = Integer.parseInt(lifeCoupon.getBuyUseStartTime());
|
|
|
+ Integer buyUseEndTime = Integer.parseInt(lifeCoupon.getBuyUseEndTime());
|
|
|
+ // 获取当前小时
|
|
|
+ LocalTime now = LocalTime.now();
|
|
|
+ int currentHour = now.getHour();
|
|
|
+ // 验证输入的小时是否有效
|
|
|
+ if (buyUseStartTime < 0 || buyUseStartTime > 24 || buyUseEndTime < 0 || buyUseEndTime > 24) {
|
|
|
+ return R.fail("小时必须在0-23之间");
|
|
|
+ }
|
|
|
+ // 处理跨天的情况,例如22点到次日3点
|
|
|
+ if (buyUseStartTime >= buyUseEndTime) {
|
|
|
+ if (currentHour < buyUseStartTime && currentHour > buyUseEndTime) {
|
|
|
+ return R.fail("该劵不在有效期内");
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ if (!(currentHour >= buyUseStartTime && currentHour <= buyUseEndTime)) {
|
|
|
+ return R.fail("该劵不在有效期内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当天开始时间(00:00:00)
|
|
|
+ LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
|
|
|
+ // 获取当天结束时间(23:59:59.999)
|
|
|
+ LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
|
|
|
+ if(!lifeCoupon.getSingleCanUse().equals("0")){
|
|
|
+ Integer orderNum = orderCouponMiddleMapper.selectCount(new LambdaQueryWrapper<OrderCouponMiddle>()
|
|
|
+ .eq(OrderCouponMiddle :: getOrderId, orderCouponMiddle.getOrderId())
|
|
|
+ .between(OrderCouponMiddle :: getUsedTime,todayStart,todayEnd)
|
|
|
+ .eq(OrderCouponMiddle ::getStatus, 2)
|
|
|
+ );
|
|
|
+ if(orderNum >= Integer.parseInt(lifeCoupon.getSingleCanUse())){
|
|
|
+ return R.fail("该订单已经超过今日单次核销数量");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.success("效验通过");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|