|
|
@@ -3,20 +3,33 @@ package shop.alien.store.service;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
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.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import shop.alien.entity.result.BusinessException;
|
|
|
+import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.*;
|
|
|
+import shop.alien.entity.store.dto.LifeUserOrderDto;
|
|
|
import shop.alien.entity.store.excelVo.LifeUserOrderExcelVo;
|
|
|
-import shop.alien.entity.store.excelVo.StoreInfoExcelVo;
|
|
|
import shop.alien.entity.store.excelVo.util.ExcelGenerator;
|
|
|
+import shop.alien.entity.store.vo.LifeUserOrderVo;
|
|
|
import shop.alien.entity.store.vo.StoreInfoVo;
|
|
|
import shop.alien.mapper.*;
|
|
|
import shop.alien.store.config.GaoDeMapUtil;
|
|
|
import shop.alien.util.ali.AliOSSUtil;
|
|
|
import shop.alien.util.common.AlipayTradeRefund;
|
|
|
+import shop.alien.util.common.UniqueRandomNumGenerator;
|
|
|
+import shop.alien.util.common.constant.CouponStatusEnum;
|
|
|
+import shop.alien.util.common.constant.CouponTypeEnum;
|
|
|
+import shop.alien.util.common.constant.DiscountCouponEnum;
|
|
|
+import shop.alien.util.common.constant.OrderStatusEnum;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
@@ -32,7 +45,7 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
|
-public class LifeUserOrderService {
|
|
|
+public class LifeUserOrderService extends ServiceImpl<LifeUserOrderMapper, LifeUserOrder> {
|
|
|
|
|
|
private final LifeUserOrderMapper lifeUserOrderMapper;
|
|
|
|
|
|
@@ -61,8 +74,12 @@ public class LifeUserOrderService {
|
|
|
private final StoreImgMapper storeImgMapper;
|
|
|
|
|
|
private final StoreUserMapper storeUserMapper;
|
|
|
+ private final LifeDiscountCouponUserMapper lifeDiscountCouponUserMapper;
|
|
|
+ private final OrderCouponMiddleService orderCouponMiddleService;
|
|
|
|
|
|
private final AliOSSUtil aliOSSUtil;
|
|
|
+ private final OrderCouponMiddleMapper orderCouponMiddleMapper;
|
|
|
+ private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;
|
|
|
|
|
|
@Value("${spring.web.resources.excel-path}")
|
|
|
private String excelPath;
|
|
|
@@ -434,7 +451,7 @@ public class LifeUserOrderService {
|
|
|
public Map<String, Object> refundCheck(LifeRefundOrder refundOrder) {
|
|
|
Map<String, Object> returnMap = new HashMap<>();
|
|
|
LifeUserOrder order = lifeUserOrderMapper.selectById(refundOrder.getOrderId());
|
|
|
- if (order.getStatus() != 0) {
|
|
|
+ if (order.getStatus() != 1) {
|
|
|
returnMap.put("success", "不可退款");
|
|
|
returnMap.put("reason", "当前订单状态不是待消费");
|
|
|
return returnMap;
|
|
|
@@ -551,4 +568,397 @@ public class LifeUserOrderService {
|
|
|
String filePath = ExcelGenerator.generateExcel(excelPath + excelGeneratePath + fileName + ".xlsx", lifeUserOrderExcelVos, LifeUserOrderExcelVo.class);
|
|
|
return aliOSSUtil.uploadFile(new File(filePath), "excel/" + fileName + ".xlsx");
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建用户订单
|
|
|
+ * @param lifeUserOrderDto 构造数据用
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Map<String, Object> createUserOrder(LifeUserOrderDto lifeUserOrderDto) {
|
|
|
+ Date date = new Date();
|
|
|
+ //1.创建订单
|
|
|
+ LifeUserOrder lifeUserOrder = new LifeUserOrder();
|
|
|
+ // 用户id
|
|
|
+ lifeUserOrder.setUserId(lifeUserOrderDto.getUserId());
|
|
|
+ // 店铺id
|
|
|
+ lifeUserOrder.setStoreId(lifeUserOrderDto.getStoreId());
|
|
|
+ // 订单号
|
|
|
+ lifeUserOrder.setOrderNo(lifeUserOrderDto.getOrderNo());
|
|
|
+ // 优惠券id
|
|
|
+ lifeUserOrder.setQuanId(lifeUserOrderDto.getYhquanId());
|
|
|
+ // 付款金额
|
|
|
+ lifeUserOrder.setPrice(lifeUserOrderDto.getPrice());
|
|
|
+ // 订单金额
|
|
|
+ lifeUserOrder.setFinalPrice(lifeUserOrderDto.getFinalPrice());
|
|
|
+ // 订单类型
|
|
|
+ lifeUserOrder.setCouponType(null == lifeUserOrderDto.getCouponType() ? 1 : lifeUserOrderDto.getCouponType());
|
|
|
+ // 订单状态
|
|
|
+ lifeUserOrder.setStatus(null == lifeUserOrderDto.getStatus() ? 0 : lifeUserOrderDto.getStatus());
|
|
|
+ // 购买时间
|
|
|
+ lifeUserOrder.setBuyTime(date);
|
|
|
+ this.saveOrUpdate(lifeUserOrder);
|
|
|
+ //2.判断是否使用优惠券
|
|
|
+ //查询优惠券信息
|
|
|
+ if (StringUtils.isNotEmpty(lifeUserOrderDto.getYhquanId())) {
|
|
|
+ LifeDiscountCouponUser lifeDiscountCouponUser = lifeDiscountCouponUserMapper.selectById(lifeUserOrderDto.getYhquanId());
|
|
|
+ //将优惠券状态变更为已使用
|
|
|
+ lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_BEEN_USED.getValue()));
|
|
|
+ //将优惠券使用时间存入
|
|
|
+ lifeDiscountCouponUser.setUseTime(date);
|
|
|
+ lifeDiscountCouponUserMapper.updateById(lifeDiscountCouponUser);
|
|
|
+ }
|
|
|
+ //3.根据购买数量增加中间关系 订单id+券编号 确定一条数据
|
|
|
+ BigDecimal sumPrice = new BigDecimal(0);
|
|
|
+ for (int i = 0; i < lifeUserOrderDto.getCount(); i++) {
|
|
|
+ String code = UniqueRandomNumGenerator.generateUniqueCode(12);
|
|
|
+ OrderCouponMiddle orderCouponMiddle = new OrderCouponMiddle();
|
|
|
+ // 订单id
|
|
|
+ orderCouponMiddle.setOrderId(lifeUserOrder.getId());
|
|
|
+ // 团购/代金券id
|
|
|
+ orderCouponMiddle.setCouponId(lifeUserOrderDto.getCouponId());
|
|
|
+ // 团购/代金券 code
|
|
|
+ orderCouponMiddle.setCouponCode(code);
|
|
|
+ // 团购/代金券价格
|
|
|
+ if(i == lifeUserOrderDto.getCount() - 1){
|
|
|
+ orderCouponMiddle.setPrice(new BigDecimal(lifeUserOrderDto.getFinalPrice()).subtract(sumPrice).setScale(2,BigDecimal.ROUND_HALF_UP));
|
|
|
+ } else {
|
|
|
+ BigDecimal divide = new BigDecimal(lifeUserOrderDto.getFinalPrice()).divide(new BigDecimal(lifeUserOrderDto.getCount()), 2, BigDecimal.ROUND_HALF_UP);
|
|
|
+ orderCouponMiddle.setPrice(divide);
|
|
|
+ sumPrice = sumPrice.add(divide);
|
|
|
+ }
|
|
|
+ // 订单状态
|
|
|
+ orderCouponMiddle.setStatus(0);
|
|
|
+ orderCouponMiddleService.save(orderCouponMiddle);
|
|
|
+ // 使用时间
|
|
|
+ // 退款时间
|
|
|
+ // 删除标记
|
|
|
+ }
|
|
|
+ //4. 代金券/团购库存扣除 coupon_type 1 代金券 2团购
|
|
|
+ int successful = 0;
|
|
|
+ if(lifeUserOrderDto.getCouponType() == 2){
|
|
|
+ // 团购库存扣除
|
|
|
+ successful = lifeGroupBuyMainMapper.update(null, new UpdateWrapper<LifeGroupBuyMain>()
|
|
|
+ .eq("id", lifeUserOrderDto.getCouponId()).setSql("inventory_num = inventory_num - " + lifeUserOrderDto.getCount()));
|
|
|
+ } else {
|
|
|
+ successful = lifeCouponMapper.update(null, new UpdateWrapper<LifeCoupon>()
|
|
|
+ .eq("id", lifeUserOrderDto.getCouponId()).setSql("stock_qty = stock_qty - " + lifeUserOrderDto.getCount()));
|
|
|
+ }
|
|
|
+ if(successful == 0){
|
|
|
+ log.error("库存不足");
|
|
|
+ throw new BusinessException("库存不足");
|
|
|
+ }
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();
|
|
|
+ returnMap.put("success", "下单成功");
|
|
|
+ returnMap.put("orderNo", lifeUserOrderDto.getOrderNo());
|
|
|
+ returnMap.put("lifeUserOrder", lifeUserOrder);
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户订单
|
|
|
+ *
|
|
|
+ * @param lifeUserOrderDto 构造数据用
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public boolean updateUserOrder(LifeUserOrderDto lifeUserOrderDto) {
|
|
|
+ Date date = new Date();
|
|
|
+ LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectById(lifeUserOrderDto.getId());
|
|
|
+ if ( null == lifeUserOrder){
|
|
|
+ log.error("updateUserOrder未查询到订单");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ UpdateWrapper<OrderCouponMiddle> orderCouponMiddleUpdateWrapper = new UpdateWrapper<>();
|
|
|
+ orderCouponMiddleUpdateWrapper.eq("order_id",lifeUserOrderDto.getId());
|
|
|
+ // 根据状态判断怎么更新数据 目前只进行已支付,已取消,已过期判断
|
|
|
+ // TODO 后续再进行已核销和已退款判断 ()
|
|
|
+ switch (lifeUserOrderDto.getStatus()){
|
|
|
+ case 1:
|
|
|
+ lifeUserOrder.setPayTime(date);
|
|
|
+ lifeUserOrder.setPayMethod(lifeUserOrderDto.getPayMethod());
|
|
|
+ case 3:
|
|
|
+ case 4:
|
|
|
+ lifeUserOrder.setStatus(lifeUserOrderDto.getStatus());
|
|
|
+ orderCouponMiddleUpdateWrapper.set("status",lifeUserOrderDto.getStatus());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.saveOrUpdate(lifeUserOrder) && orderCouponMiddleService.update(orderCouponMiddleUpdateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除用户订单,删除需要判断订单状态并且删除中间关系
|
|
|
+ * @param orderId 订单id
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public boolean deleteUserOrder(String orderId) {
|
|
|
+ LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectById(orderId);
|
|
|
+ if ( null == lifeUserOrder){
|
|
|
+ log.error("deleteUserOrder未查询到订单");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 判断订单状态
|
|
|
+ // 已核销才可以删除
|
|
|
+ if (lifeUserOrder.getStatus() != 2) {
|
|
|
+ log.error("deleteUserOrder订单状态错误,订单已核销才可以删除");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 删除中间关系
|
|
|
+ UpdateWrapper<OrderCouponMiddle> orderCouponMiddleUpdateWrapper = new UpdateWrapper<>();
|
|
|
+ orderCouponMiddleUpdateWrapper.eq("order_id",orderId);
|
|
|
+ return this.removeById(orderId) && orderCouponMiddleService.remove(orderCouponMiddleUpdateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询用户订单列表
|
|
|
+ * @param page 页码
|
|
|
+ * @param size 每页数量
|
|
|
+ * @param userId 用户id
|
|
|
+ * @param couponType 订单类型
|
|
|
+ * @param name 店铺名称
|
|
|
+ */
|
|
|
+ public IPage<LifeUserOrderVo> queryUserOrderList(Integer page, Integer size, String userId, String storeId, String couponType,String orderStatus, String name, String startTime, String endTime) {
|
|
|
+ IPage<LifeUserOrderVo> brandedPage = new Page<>(page, size);
|
|
|
+ QueryWrapper<LifeUserOrderVo> lifeUserOrderQueryWrapper = new QueryWrapper<>();
|
|
|
+ lifeUserOrderQueryWrapper.eq(org.apache.commons.lang3.StringUtils.isNotBlank(userId),"luo.user_id",userId);
|
|
|
+ lifeUserOrderQueryWrapper.eq(org.apache.commons.lang3.StringUtils.isNotBlank(storeId),"luo.store_id",storeId);
|
|
|
+ lifeUserOrderQueryWrapper.eq(null != couponType && !"-1".equals(couponType),"luo.coupon_type",couponType);
|
|
|
+
|
|
|
+ // 提取SQL基础部分,避免重复定义
|
|
|
+ String baseSql = "select DISTINCT ocm1.order_id from order_coupon_middle ocm1 where 1=1";
|
|
|
+
|
|
|
+ if (!"-1".equals(orderStatus)) {
|
|
|
+ // 非-1状态:直接添加状态条件
|
|
|
+ String sql = baseSql + " and ocm1.status = " + orderStatus;
|
|
|
+ lifeUserOrderQueryWrapper.inSql("luo.id", sql);
|
|
|
+ } else {
|
|
|
+ // -1状态且storeId不为空时添加条件
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(storeId)) {
|
|
|
+ // 注意:原代码中的concat不会修改原字符串,需要重新赋值
|
|
|
+ String sql = baseSql + " and ocm1.status != 0";
|
|
|
+ lifeUserOrderQueryWrapper.inSql("luo.id", sql);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ lifeUserOrderQueryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(name),"tc.coupon_name",name);
|
|
|
+ lifeUserOrderQueryWrapper.gt(org.apache.commons.lang3.StringUtils.isNotBlank(startTime),"luo.created_time",startTime + " 00:00:00");
|
|
|
+ lifeUserOrderQueryWrapper.lt(org.apache.commons.lang3.StringUtils.isNotBlank(endTime),"luo.created_time",endTime + " 23:59:59");
|
|
|
+ lifeUserOrderQueryWrapper.orderByDesc("luo.created_time");
|
|
|
+ lifeUserOrderQueryWrapper.groupBy("luo.coupon_type","luo.id");
|
|
|
+ IPage<LifeUserOrderVo> lifeUserOrderVoIPage = lifeUserOrderMapper.queryUserOrderList(brandedPage, lifeUserOrderQueryWrapper);
|
|
|
+ if (!"-1".equals(orderStatus)) {
|
|
|
+ lifeUserOrderVoIPage.getRecords().forEach(x->x.setStatus(Integer.parseInt(orderStatus)));
|
|
|
+ }
|
|
|
+ // 计算退款金额
|
|
|
+ for (LifeUserOrderVo record : lifeUserOrderVoIPage.getRecords()) {
|
|
|
+ record.setOrderCouponMiddleList(orderCouponMiddleMapper.selectList(new QueryWrapper<OrderCouponMiddle>().eq("order_id",record.getId())));
|
|
|
+ calcExpectIncome(record);
|
|
|
+ }
|
|
|
+ return lifeUserOrderVoIPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询用户订单详情
|
|
|
+ * @param orderId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public R<LifeUserOrderVo> queryUserOrderDetail(String orderId,String longitude, String latitude) {
|
|
|
+ LifeUserOrderVo lifeUserOrderVo = lifeUserOrderMapper.queryUserOrderDetail(orderId,longitude+","+latitude);
|
|
|
+ if ( null == lifeUserOrderVo){
|
|
|
+ log.error("queryUserOrderDetail未查询到订单");
|
|
|
+ return R.fail("未查询到订单");
|
|
|
+ }
|
|
|
+ calcExpectIncome(lifeUserOrderVo);
|
|
|
+ return R.data(lifeUserOrderVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void calcExpectIncome(LifeUserOrderVo lifeUserOrderVo) {
|
|
|
+ // 退款记录
|
|
|
+ List<OrderCouponMiddle> orderCouponMiddleList = lifeUserOrderVo.getOrderCouponMiddleList();
|
|
|
+ List<OrderCouponMiddle> refundList = orderCouponMiddleList.stream().filter(x -> x.getStatus() == 5).collect(Collectors.toList());
|
|
|
+ lifeUserOrderVo.setRefundList(refundList);
|
|
|
+ // 预计收入
|
|
|
+ // 退款金额
|
|
|
+ BigDecimal refundAmount = new BigDecimal(0);
|
|
|
+ if(!CollectionUtils.isEmpty(refundList)){
|
|
|
+ refundAmount = refundList.stream().map(OrderCouponMiddle::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ }
|
|
|
+ // 预计收入
|
|
|
+ BigDecimal expectIncome = new BigDecimal(lifeUserOrderVo.getPrice()).subtract(refundAmount);
|
|
|
+ String commissionRateStr = lifeUserOrderVo.getCommissionRate();
|
|
|
+ BigDecimal commissionRate = BigDecimal.ZERO;
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(commissionRateStr)) {
|
|
|
+ try {
|
|
|
+ // 将字符串转换为 BigDecimal,并除以 100(转换为小数)
|
|
|
+ commissionRate = new BigDecimal(commissionRateStr)
|
|
|
+ .divide(new BigDecimal("100"), 4, RoundingMode.HALF_UP);
|
|
|
+ // 保留 4 位小数,四舍五入(确保计算精度)
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ // 处理非数字格式的异常(如 commissionRate 为空或非法字符)
|
|
|
+ e.printStackTrace();
|
|
|
+ // 可根据业务需求设置默认值或抛出提示
|
|
|
+ } catch (ArithmeticException e) {
|
|
|
+ // 处理除法异常(理论上除以 100 不会出现,此处为稳妥处理)
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ expectIncome = expectIncome.subtract(expectIncome.multiply(commissionRate)).setScale(2, RoundingMode.HALF_UP);
|
|
|
+ lifeUserOrderVo.setExpectIncome(expectIncome);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 平台端-查询订单列表
|
|
|
+ * @param page 页码
|
|
|
+ * @param size 每页数量
|
|
|
+ * @param orderNo 订单号
|
|
|
+ * @param orderStatus 订单状态
|
|
|
+ * @param couponName 优惠券名称
|
|
|
+ * @param couponType 优惠券类型
|
|
|
+ * @param storeName 店铺名称
|
|
|
+ * @param buyStartTime 购买时间开始
|
|
|
+ * @param buyEndTime 购买时间结束
|
|
|
+ * @param payStartTime 支付时间开始
|
|
|
+ * @param payEndTime 支付时间结束
|
|
|
+ * @param finishStartTime 完成时间开始
|
|
|
+ * @param finishEndTime 完成时间结束
|
|
|
+ */
|
|
|
+ public IPage<LifeUserOrderVo> queryOrderList(Integer page, Integer size, String orderNo, String orderStatus, String couponName, String couponType, String storeName, String buyStartTime, String buyEndTime, String payStartTime, String payEndTime, String finishStartTime, String finishEndTime) {
|
|
|
+ IPage<LifeUserOrderVo> brandedPage = new Page<>(page, size);
|
|
|
+ QueryWrapper<LifeUserOrderVo> lifeUserOrderQueryWrapper = new QueryWrapper<>();
|
|
|
+ lifeUserOrderQueryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(orderNo),"luo.order_no",orderNo);
|
|
|
+ lifeUserOrderQueryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(couponName),"tc.coupon_name",couponName);
|
|
|
+ lifeUserOrderQueryWrapper.eq(org.apache.commons.lang3.StringUtils.isNotBlank(couponType),"tc.coupon_type",couponType);
|
|
|
+ lifeUserOrderQueryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(storeName),"si.store_name",storeName);
|
|
|
+ lifeUserOrderQueryWrapper.gt(org.apache.commons.lang3.StringUtils.isNotBlank(buyStartTime),"luo.buy_time",buyStartTime + " 00:00:00");
|
|
|
+ lifeUserOrderQueryWrapper.lt(org.apache.commons.lang3.StringUtils.isNotBlank(buyEndTime),"luo.buy_time",buyEndTime + " 23:59:59");
|
|
|
+ lifeUserOrderQueryWrapper.gt(org.apache.commons.lang3.StringUtils.isNotBlank(payStartTime),"luo.pay_time",payStartTime + " 00:00:00");
|
|
|
+ lifeUserOrderQueryWrapper.lt(org.apache.commons.lang3.StringUtils.isNotBlank(payEndTime),"luo.pay_time",payEndTime + " 23:59:59");
|
|
|
+ lifeUserOrderQueryWrapper.gt(org.apache.commons.lang3.StringUtils.isNotBlank(finishStartTime),"luo.finish_time",finishStartTime + " 00:00:00");
|
|
|
+ lifeUserOrderQueryWrapper.lt(org.apache.commons.lang3.StringUtils.isNotBlank(finishEndTime),"luo.finish_time",finishEndTime + " 23:59:59");
|
|
|
+// @ApiImplicitParam(name = "orderStatus", value = "订单状态,-1,全部(可以不传);0,待支付;1,已支付/待使用;2,已核销;3,已过期;4,已取消;5.已退款,全退款了才算", required = false),
|
|
|
+ // 提取SQL基础部分,避免重复定义
|
|
|
+// String baseSql = "select DISTINCT ocm1.order_id from order_coupon_middle ocm1 where 1=1";
|
|
|
+
|
|
|
+ if (!"-1".equals(orderStatus)) {
|
|
|
+ // 非-1状态:直接添加状态条件
|
|
|
+// String sql = baseSql + " and ocm1.status = " + orderStatus;
|
|
|
+// lifeUserOrderQueryWrapper.inSql("luo.id", sql);
|
|
|
+ lifeUserOrderQueryWrapper.eq("ocm.status",orderStatus);
|
|
|
+ }
|
|
|
+ lifeUserOrderQueryWrapper.groupBy("tc.coupon_type","tc.coupon_id");
|
|
|
+ IPage<LifeUserOrderVo> lifeUserOrderVoIPage = lifeUserOrderMapper.queryUserOrderList(brandedPage, lifeUserOrderQueryWrapper);
|
|
|
+
|
|
|
+ if (!"-1".equals(orderStatus)) {
|
|
|
+ lifeUserOrderVoIPage.getRecords().forEach(x->x.setStatus(Integer.parseInt(orderStatus)));
|
|
|
+ }
|
|
|
+ return lifeUserOrderVoIPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 平台端-查询订单详情
|
|
|
+ * @param orderId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public R queryOrderDetail(String orderId) {
|
|
|
+ LifeUserOrderVo lifeUserOrderVo = lifeUserOrderMapper.queryUserOrderDetail(orderId,null);
|
|
|
+ if ( null == lifeUserOrderVo){
|
|
|
+ log.error("queryUserOrderDetail未查询到订单");
|
|
|
+ return R.fail("未查询到订单");
|
|
|
+ }
|
|
|
+ // 退款记录
|
|
|
+ calcExpectIncome(lifeUserOrderVo);
|
|
|
+ lifeUserOrderVo.setCouponCount(lifeUserOrderVo.getOrderCouponMiddleList().size());
|
|
|
+ return R.data(lifeUserOrderVo);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 团购/代金券-校验用户是否可以购买
|
|
|
+ * @param couponId 团购/代金券id
|
|
|
+ * @param couponType 团购/代金券类型
|
|
|
+ * @param userId 用户id
|
|
|
+ * @param count 购买数量
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, Object> buyCouponCheck(String couponId, Integer couponType, String userId,String storeId, Integer count) {
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();
|
|
|
+ // 获取团购/代金券信息
|
|
|
+ Integer status = 0;
|
|
|
+ // 限购数量, 已购数量
|
|
|
+ Integer buyLimit = 0;
|
|
|
+ Integer buyCount = 0;
|
|
|
+ // 库存
|
|
|
+ Integer stockQty = 0;
|
|
|
+ // 结束时间
|
|
|
+ Date endTime = null;
|
|
|
+ if (couponType == CouponTypeEnum.GROUP_BUY.getCode()) {
|
|
|
+ // 团购
|
|
|
+ LifeGroupBuyMain lifeGroupBuyMain = lifeGroupBuyMainMapper.selectById(couponId);
|
|
|
+ status = lifeGroupBuyMain.getStatus();
|
|
|
+ // 限购类型
|
|
|
+ Integer quotaType = lifeGroupBuyMain.getQuotaType();
|
|
|
+ if (0 != quotaType) {
|
|
|
+ // 限购数量
|
|
|
+ buyLimit = Integer.parseInt(lifeGroupBuyMain.getQuotaValue());
|
|
|
+ // 已购数量
|
|
|
+ buyCount = orderCouponMiddleMapper.selectCount(new QueryWrapper<OrderCouponMiddle>().eq("coupon_id", couponId).notIn("status", OrderStatusEnum.CANCEL.getStatus(), OrderStatusEnum.REFUND.getStatus())
|
|
|
+ .inSql("order_id", "select order_id from life_user_order where user_id = " + userId + " and store_id = " + storeId));
|
|
|
+ }
|
|
|
+ // 库存 inventory_num
|
|
|
+ stockQty = lifeGroupBuyMain.getInventoryNum();
|
|
|
+ endTime = lifeGroupBuyMain.getEndTime();
|
|
|
+ } else if (1 == CouponTypeEnum.COUPON.getCode()) {
|
|
|
+ // 代金券
|
|
|
+ LifeCoupon lifeCoupon = lifeCouponMapper.selectById(couponId);
|
|
|
+ status = lifeCoupon.getStatus();
|
|
|
+ // 限购数量
|
|
|
+ buyLimit = lifeCoupon.getBuyLimit();
|
|
|
+ // 已购数量
|
|
|
+ buyCount = orderCouponMiddleMapper.selectCount(new QueryWrapper<OrderCouponMiddle>().eq("coupon_id", couponId).notIn("status", OrderStatusEnum.CANCEL.getStatus(), OrderStatusEnum.REFUND.getStatus())
|
|
|
+ .inSql("order_id", "select order_id from life_user_order where user_id = " + userId + " and store_id = " + storeId));
|
|
|
+ stockQty = lifeCoupon.getStockQty();
|
|
|
+ endTime = lifeCoupon.getEndDate();
|
|
|
+ } else {
|
|
|
+ log.error("buyCouponCheck-团购/代金券类型错误");
|
|
|
+ returnMap.put("success", false);
|
|
|
+ returnMap.put("reason", "团购/代金券类型错误");
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+ // 状态(0草稿/1待审核/2未开始/3审核拒绝/4已售罄/5进行中/6已下架/7已结束/8=2+手动下架)
|
|
|
+ if (status != CouponStatusEnum.ONGOING.getCode()) {
|
|
|
+ returnMap.put("success", false);
|
|
|
+ returnMap.put("reason", "团购/代金券未开始");
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+ // 限购
|
|
|
+ if (0 != buyLimit) {
|
|
|
+ if (buyCount + count > buyLimit) {
|
|
|
+ returnMap.put("success", false);
|
|
|
+ returnMap.put("reason", "购买数量大于限购数量");
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 库存
|
|
|
+ if (count > stockQty) {
|
|
|
+ returnMap.put("success", false);
|
|
|
+ returnMap.put("reason", "购买数量大于库存数量");
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+ // 时间
|
|
|
+ Date now = new Date();
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(now);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date nowDay = calendar.getTime();
|
|
|
+ if (nowDay.compareTo(endTime) > 0) {
|
|
|
+ if (couponType == CouponTypeEnum.GROUP_BUY.getCode()) {
|
|
|
+ lifeGroupBuyMainMapper.update(null, new UpdateWrapper<LifeGroupBuyMain>().set("status", CouponStatusEnum.ENDED.getCode()).eq("id", couponId));
|
|
|
+ } else {
|
|
|
+ lifeCouponMapper.update(null, new UpdateWrapper<LifeCoupon>().set("status", CouponStatusEnum.ENDED.getCode()).eq("id", couponId));
|
|
|
+ }
|
|
|
+ returnMap.put("success", false);
|
|
|
+ returnMap.put("reason", "活动已结束");
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
}
|