Przeglądaj źródła

经销商核销

chunping 3 lat temu
rodzic
commit
d22e62092a

+ 123 - 0
mp-admin/src/main/java/com/qs/mp/web/controller/api/channel/ChannelCouponVerifyController.java

@@ -0,0 +1,123 @@
+package com.qs.mp.web.controller.api.channel;
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.qs.mp.channel.domain.ChannelCouponVerifyLog;
+import com.qs.mp.channel.service.IChannelCouponVerifyLogService;
+import com.qs.mp.channel.service.IChannelService;
+import com.qs.mp.common.core.domain.AjaxResult;
+import com.qs.mp.common.core.page.TableDataInfo;
+import com.qs.mp.common.enums.ErrorCodeEnum;
+import com.qs.mp.common.enums.UserCouponStatusEnum;
+import com.qs.mp.user.domain.UserCoupon;
+import com.qs.mp.user.domain.UserCouponChannel;
+import com.qs.mp.user.service.IUserCouponChannelService;
+import com.qs.mp.user.service.IUserCouponService;
+import com.qs.mp.utils.SecurityUtils;
+import com.qs.mp.web.controller.common.BaseApiController;
+import io.swagger.annotations.Api;
+import java.util.Date;
+import java.util.List;
+import ma.glasnost.orika.MapperFacade;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @auther zhongcp
+ * @create 2022-02-28 16:17:48
+ * @describe 渠道优惠券核销前端控制器
+ */
+@Api("渠道管理API")
+@RestController
+@RequestMapping("/api/v1/mp/channel/*")
+@Component
+public class ChannelCouponVerifyController extends BaseApiController {
+
+  @Autowired
+  private IChannelService channelService;
+
+  @Autowired
+  private IUserCouponService userCouponService;
+
+  @Autowired
+  private IUserCouponChannelService userCouponChannelService;
+
+  @Autowired
+  private IChannelCouponVerifyLogService channelCouponVerifyLogService;
+
+  @Autowired
+  private MapperFacade mapperFacade;
+
+  /**
+   * 优惠券核销
+   *
+   * @param
+   * @return
+   */
+  @PostMapping(value = "coupon/verify")
+  public AjaxResult verify(@RequestBody JSONObject param) {
+    String verifyCode = param.getString("verifyCode");
+    if (StringUtils.isBlank(verifyCode)) {
+      return error(ErrorCodeEnum.ERROR_CODE_1001);
+    }
+    Long channelId = SecurityUtils.getLoginUser().getChannelId();
+    UserCoupon userCoupon = userCouponService.getOne(new LambdaQueryWrapper<UserCoupon>()
+        .eq(UserCoupon::getVerifyCode, verifyCode));
+    if (null == userCoupon) {
+      return error("该券不存在!");
+    }
+    if (userCoupon.getStatus() == UserCouponStatusEnum.USED) {
+      return error("该券已使用!");
+    }
+    if (userCoupon.getValidEnd().before(new Date())) {
+      return error("该券已过期!");
+    }
+    UserCouponChannel couponChannel = userCouponChannelService.getOne(
+        new LambdaQueryWrapper<UserCouponChannel>()
+            .eq(UserCouponChannel::getUserCouponId, userCoupon.getId())
+            .eq(UserCouponChannel::getChannelId, channelId));
+    if (null == couponChannel) {
+      return error("无权核销该券");
+    }
+    channelCouponVerifyLogService.verify(channelId, userCoupon);
+    return AjaxResult.success("核销成功");
+  }
+
+  /**
+   * 获取我的核销记录
+   *
+   * @return
+   */
+  @PostMapping("coupon/verify/log/list")
+  public TableDataInfo listChannel(@RequestBody JSONObject jsonObject) {
+    Long channelId = SecurityUtils.getLoginUser().getChannelId();
+    startPage();
+    List<ChannelCouponVerifyLog> verifyLogList = channelCouponVerifyLogService.list(
+        new LambdaQueryWrapper<ChannelCouponVerifyLog>()
+            .eq(ChannelCouponVerifyLog::getChannelId, channelId)
+            .orderByDesc(ChannelCouponVerifyLog::getCreatedTime));
+    return getDataTable(verifyLogList);
+  }
+
+	/**
+	 * 优惠券核销记录详情
+	 *
+	 * @param
+	 * @return
+	 */
+	@PostMapping(value = "coupon/verify/log/detail")
+	public AjaxResult detail(@RequestBody JSONObject param) {
+		Long logId = param.getLong("logId");
+		if (null == logId || 0 == logId) {
+			return error(ErrorCodeEnum.ERROR_CODE_1001);
+		}
+		ChannelCouponVerifyLog verifyLog = channelCouponVerifyLogService.getById(logId);
+		return AjaxResult.success(verifyLog);
+	}
+
+}

+ 2 - 1
mp-common/src/main/java/com/qs/mp/common/enums/ChannelMoneyEnum.java

@@ -13,7 +13,8 @@ public enum ChannelMoneyEnum implements IEnum<Integer> {
   COMMISSION(1, "佣金收入"),
   WITHDRAW(2, "提现"),
   WITHDRAW_FEE(3, "提现手续费"),
-  PURCHASE(4, "进票");
+  PURCHASE(4, "进票"),
+  COUPON(5, "优惠券核销结算");
 
 
   private final int value;

+ 40 - 0
mp-common/src/main/java/com/qs/mp/common/enums/CouponSettleStatusEnum.java

@@ -0,0 +1,40 @@
+package com.qs.mp.common.enums;
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.annotation.IEnum;
+
+/**
+ *
+ * 用户卡券使用状态
+ *
+ */
+public enum CouponSettleStatusEnum implements IEnum<Integer> {
+
+  INIT(1, "待结算"),
+  FINISHED(2, "已结算"),;
+
+  private final Integer value;
+  private final String desc;
+
+  CouponSettleStatusEnum(final Integer value, final String desc) {
+    this.value = value;
+    this.desc = desc;
+  }
+
+  @Override
+  public Integer getValue() {
+    return value;
+  }
+
+  /**
+   * 重写toString,单个转化成json
+   * @return
+   */
+  @Override
+  public String toString() {
+    JSONObject object = new JSONObject();
+    object.put("value",value);
+    object.put("desc", desc);
+    return object.toString();
+  }
+}

+ 1 - 1
mp-service/src/main/java/com/qs/mp/admin/service/impl/CouponServiceImpl.java

@@ -106,7 +106,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
       userCouponChannel.setChannelId(ticketOrder.getChannelId());
       userCouponChannelService.save(userCouponChannel);
 			Channel channel = channelService.getById(ticketOrder.getChannelId());
-			userCoupon.setUseAreaDesc(channel.getName());
+			userCoupon.setUseAreaDesc(channel.getSiteName());
     }
 		userCouponService.save(userCoupon);
   }

+ 97 - 0
mp-service/src/main/java/com/qs/mp/channel/domain/ChannelCouponVerifyLog.java

@@ -0,0 +1,97 @@
+package com.qs.mp.channel.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.qs.mp.common.enums.CouponDiscountTypeEnum;
+import com.qs.mp.common.enums.CouponSettleStatusEnum;
+import java.io.Serializable;
+import java.util.Date;
+import lombok.Data;
+
+/**
+ * @describe 渠道优惠券核销流水实体类
+ * @auther quanshu
+ * @create 2022-03-21 14:47:23
+ */
+@TableName("mp_channel_coupon_verify_log")
+@Data
+public class ChannelCouponVerifyLog implements Serializable {
+
+  private static final long serialVersionUID = 1L;
+
+  /**
+   * 主键
+   */
+  @TableId(value = "id", type = IdType.AUTO)
+  private Long id;
+
+  /**
+   * 渠道ID
+   */
+  @TableField("channel_id")
+  private Long channelId;
+
+  /**
+   * 用户优惠券ID
+   */
+  @TableField("user_coupon_id")
+  private String userCouponId;
+
+  /**
+   * 优惠类型;1代金券、2折扣券、3兑换券
+   */
+  @TableField("discount_type")
+  private CouponDiscountTypeEnum discountType;
+
+  /**
+   * 优惠金额(比例)
+   */
+  @TableField("discount")
+  private Integer discount;
+
+  /**
+   * 发放时间
+   */
+  @TableField("distribute_time")
+  private Date distributeTime;
+
+  /**
+   * 有效期开始日
+   */
+  @TableField("valid_start")
+  private Date validStart;
+
+  /**
+   * 有效期结束日
+   */
+  @TableField("valid_end")
+  private Date validEnd;
+
+  /**
+   * 核销时间
+   */
+  @TableField("verify_time")
+  private Date verifyTime;
+
+  /**
+   * 结算状态;1待结算 2已结算
+   */
+  @TableField("settle_status")
+  private CouponSettleStatusEnum settleStatus;
+
+  /**
+   * 创建时间
+   */
+  @TableField("created_time")
+  private Date createdTime;
+
+  /**
+   * 更新时间
+   */
+  @TableField("updated_time")
+  private Date updatedTime;
+
+
+}

+ 13 - 0
mp-service/src/main/java/com/qs/mp/channel/mapper/ChannelCouponVerifyLogMapper.java

@@ -0,0 +1,13 @@
+package com.qs.mp.channel.mapper;
+
+import com.qs.mp.channel.domain.ChannelCouponVerifyLog;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * @auther quanshu
+ * @create 2022-03-21 14:47:23
+ * @describe 渠道优惠券核销流水mapper类
+ */
+public interface ChannelCouponVerifyLogMapper extends BaseMapper<ChannelCouponVerifyLog> {
+
+}

+ 23 - 0
mp-service/src/main/java/com/qs/mp/channel/service/IChannelCouponVerifyLogService.java

@@ -0,0 +1,23 @@
+package com.qs.mp.channel.service;
+
+import com.qs.mp.channel.domain.ChannelCouponVerifyLog;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.qs.mp.user.domain.UserCoupon;
+
+/**
+ * <p>
+ * 渠道优惠券核销流水 服务类
+ * </p>
+ *
+ * @author quanshu
+ * @since 2022-03-21
+ */
+public interface IChannelCouponVerifyLogService extends IService<ChannelCouponVerifyLog> {
+
+  /**
+   * 核销
+   * @param channelId
+   * @param userCoupon
+   */
+  void verify(Long channelId, UserCoupon userCoupon);
+}

+ 96 - 0
mp-service/src/main/java/com/qs/mp/channel/service/impl/ChannelCouponVerifyLogServiceImpl.java

@@ -0,0 +1,96 @@
+package com.qs.mp.channel.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.qs.mp.admin.domain.Coupon;
+import com.qs.mp.admin.service.ICouponService;
+import com.qs.mp.channel.domain.ChannelCouponVerifyLog;
+import com.qs.mp.channel.domain.ChannelMoneyLog;
+import com.qs.mp.channel.mapper.ChannelCouponVerifyLogMapper;
+import com.qs.mp.channel.service.IChannelCouponVerifyLogService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qs.mp.channel.service.IChannelMoneyLogService;
+import com.qs.mp.common.enums.ChannelMoneyEnum;
+import com.qs.mp.common.enums.CouponDiscountTypeEnum;
+import com.qs.mp.common.enums.CouponSettleStatusEnum;
+import com.qs.mp.common.enums.CouponStatusEnum;
+import com.qs.mp.common.enums.UserCouponStatusEnum;
+import com.qs.mp.common.utils.LogUtil;
+import com.qs.mp.user.domain.UserCoupon;
+import com.qs.mp.user.service.IUserCouponService;
+import java.util.Date;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
+
+/**
+ * <p>
+ * 渠道优惠券核销流水 服务实现类
+ * </p>
+ *
+ * @author quanshu
+ * @since 2022-03-21
+ */
+@Service
+public class ChannelCouponVerifyLogServiceImpl extends ServiceImpl<ChannelCouponVerifyLogMapper, ChannelCouponVerifyLog> implements IChannelCouponVerifyLogService {
+
+  protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
+
+  @Autowired
+  private ICouponService couponService;
+
+  @Autowired
+  private IUserCouponService userCouponService;
+
+  @Autowired
+  private IChannelMoneyLogService channelMoneyLogService;
+
+  @Override
+  @Transactional
+  public void verify(Long channelId, UserCoupon userCoupon) {
+    Date verifyDate = new Date();
+    // 核销券
+    boolean updateRst = userCouponService.update(new LambdaUpdateWrapper<UserCoupon>()
+        .set(UserCoupon::getStatus, UserCouponStatusEnum.USED)
+            .set(UserCoupon::getVerifyTime, verifyDate)
+        .eq(UserCoupon::getId, userCoupon.getId())
+        .eq(UserCoupon::getStatus, UserCouponStatusEnum.UNUSED));
+    Assert.isTrue(updateRst, "经销商核销券时,券状态更新失败。userCouponId:" + userCoupon.getId());
+    Coupon coupon = couponService.getById(userCoupon.getCouponId());
+
+    // 记录核销日志
+    ChannelCouponVerifyLog verifyLog = new ChannelCouponVerifyLog();
+    verifyLog.setChannelId(channelId);
+    verifyLog.setUserCouponId(userCoupon.getId());
+    verifyLog.setDiscountType(coupon.getDiscountType());
+    verifyLog.setDiscount(coupon.getDiscount());
+    verifyLog.setDistributeTime(userCoupon.getCreatedTime());
+    verifyLog.setValidStart(userCoupon.getValidStart());
+    verifyLog.setValidEnd(userCoupon.getValidEnd());
+    verifyLog.setVerifyTime(verifyDate);
+    verifyLog.setSettleStatus(CouponSettleStatusEnum.FINISHED);
+    save(verifyLog);
+
+    if (coupon.getDiscountType() == CouponDiscountTypeEnum.MONEY_OFF){
+      int logMoney = coupon.getDiscount() * (100 - coupon.getChannelSharedRate()) / 100;
+      if (logMoney == 0) {
+        LogUtil.warn(logger, "优惠券核销结算金额为0,忽略。userCouponId:{0}", userCoupon.getId());
+      } else {
+        // 结算
+        ChannelMoneyLog moneyLog = new ChannelMoneyLog();
+        moneyLog.setChannelId(channelId);
+        moneyLog.setType(ChannelMoneyEnum.COUPON);
+        moneyLog.setLogMoney(logMoney);
+        moneyLog.setLogText("券码:" + userCoupon.getVerifyCode());
+        moneyLog.setBizTime(new Date());
+        moneyLog.setRefId(String.valueOf(verifyLog.getId()));
+        channelMoneyLogService.changeMoney(moneyLog);
+      }
+    } else {
+      LogUtil.warn(logger, "优惠券核销结算,类型非代金券,忽略。userCouponId:{0}", userCoupon.getId());
+    }
+  }
+
+}

+ 26 - 0
mp-service/src/main/resources/mapper/channel/ChannelCouponVerifyLogMapper.xml

@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.qs.mp.channel.mapper.ChannelCouponVerifyLogMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.qs.mp.channel.domain.ChannelCouponVerifyLog">
+        <id column="id" property="id" />
+        <result column="channel_id" property="channelId" />
+        <result column="user_coupon_id" property="userCouponId" />
+        <result column="discount_type" property="discountType" />
+        <result column="discount" property="discount" />
+        <result column="distribute_time" property="distributeTime" />
+        <result column="valid_start" property="validStart" />
+        <result column="valid_end" property="validEnd" />
+        <result column="verify_time" property="verifyTime" />
+        <result column="settle_status" property="settleStatus" />
+        <result column="created_time" property="createdTime" />
+        <result column="updated_time" property="updatedTime" />
+    </resultMap>
+
+    <!-- 通用查询结果列 -->
+    <sql id="Base_Column_List">
+        id, channel_id, user_coupon_id, discount_type, discount, distribute_time, valid_start, valid_end, verify_time, settle_status, created_time, updated_time
+    </sql>
+
+</mapper>