Browse Source

手动,自动完课发送课程优惠券

wjj 2 tuần trước cách đây
mục cha
commit
c63c3396ba

+ 3 - 0
fs-service/src/main/java/com/fs/course/mapper/FsUserCourseVideoRedPackageMapper.java

@@ -113,4 +113,7 @@ public interface FsUserCourseVideoRedPackageMapper
 
 
     Integer selectRedPacketByCompanyCount(@Param("videoId") Long videoId,@Param("companyId") Long companyId, @Param("periodId") Long periodId);
+
+    @Select("select * from fs_user_course_video_red_package where video_id =#{videoId} and company_id = #{companyId} and data_type = #{dataType}")
+    FsUserCourseVideoRedPackage selectRedPacketByVideoIdAndType(@Param("videoId") Long videoId,@Param("companyId") Long companyId,@Param("dataType") Integer dataType);
 }

+ 5 - 0
fs-service/src/main/java/com/fs/his/domain/FsCourseCouponUser.java

@@ -43,5 +43,10 @@ public class FsCourseCouponUser extends BaseEntity{
     @Excel(name = "核销状态 0-未核销 1-已核销")
     private Integer status;
 
+    /**
+     * 课程小节ID
+     */
+    private Long videoId;
+
 
 }

+ 5 - 0
fs-service/src/main/java/com/fs/his/mapper/FsCourseCouponUserMapper.java

@@ -3,6 +3,8 @@ package com.fs.his.mapper;
 import java.util.List;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.fs.his.domain.FsCourseCouponUser;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 
 /**
  * 用户看课优惠券Mapper接口
@@ -58,4 +60,7 @@ public interface FsCourseCouponUserMapper extends BaseMapper<FsCourseCouponUser>
      * @return 结果
      */
     int deleteFsCourseCouponUserByIds(Long[] ids);
+
+    @Select("SELECT count(*) FROM fs_course_coupon_user WHERE user_id = #{userId}  AND coupon_id = #{couponId}")
+    Integer selectCountByUserIdAndCouponId(@Param("userId") Long userId,@Param("couponId") Long couponId);
 }

+ 17 - 0
fs-service/src/main/java/com/fs/his/param/CourseFinishRewardParam.java

@@ -0,0 +1,17 @@
+package com.fs.his.param;
+
+import lombok.Data;
+
+@Data
+public class CourseFinishRewardParam {
+
+    private Long companyId;
+
+    private Long periodId;
+
+    private Long userId;
+
+    private Long videoId;
+
+
+}

+ 9 - 0
fs-service/src/main/java/com/fs/his/service/IFsCourseCouponService.java

@@ -2,6 +2,7 @@ package com.fs.his.service;
 
 import java.util.List;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.fs.common.core.domain.R;
 import com.fs.his.domain.FsCourseCoupon;
 
 /**
@@ -60,4 +61,12 @@ public interface IFsCourseCouponService extends IService<FsCourseCoupon>{
     int deleteFsCourseCouponById(Long id);
 
     List<FsCourseCoupon> selectFsCourseCouponOptions();
+
+    /**
+     * 发送课程优惠券
+     * @param videoId 小节Id
+     * @param userId 用户Id
+     */
+    R sendAutoCourseCoupon(Long videoId, Long userId,Long companyId);
+    R sendCourseCoupon(Long videoId, Long userId,Long companyId,Long periodId);
 }

+ 3 - 0
fs-service/src/main/java/com/fs/his/service/IFsCourseCouponUserService.java

@@ -2,6 +2,7 @@ package com.fs.his.service;
 
 import java.util.List;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.fs.common.core.domain.R;
 import com.fs.his.domain.FsCourseCouponUser;
 
 /**
@@ -58,4 +59,6 @@ public interface IFsCourseCouponUserService extends IService<FsCourseCouponUser>
      * @return 结果
      */
     int deleteFsCourseCouponUserById(Long id);
+
+    R useCoupon(Long userId,Long couponUserId);
 }

+ 178 - 0
fs-service/src/main/java/com/fs/his/service/impl/FsCourseCouponServiceImpl.java

@@ -1,14 +1,29 @@
 package com.fs.his.service.impl;
 
+import java.math.BigDecimal;
 import java.util.Collections;
+import java.util.Date;
 import java.util.List;
+
+import cn.hutool.json.JSONUtil;
+import com.fs.common.core.domain.R;
 import com.fs.common.utils.DateUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fs.course.config.CourseConfig;
+import com.fs.course.domain.FsUserCourseVideo;
+import com.fs.course.domain.FsUserCourseVideoRedPackage;
+import com.fs.course.mapper.FsUserCourseVideoMapper;
+import com.fs.course.mapper.FsUserCourseVideoRedPackageMapper;
+import com.fs.his.domain.FsCourseCouponUser;
+import com.fs.his.mapper.FsCourseCouponUserMapper;
+import com.fs.his.vo.CourseFinishRewardVO;
+import com.fs.system.service.ISysConfigService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.fs.his.mapper.FsCourseCouponMapper;
 import com.fs.his.domain.FsCourseCoupon;
 import com.fs.his.service.IFsCourseCouponService;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * 课程优惠券Service业务层处理
@@ -19,6 +34,17 @@ import com.fs.his.service.IFsCourseCouponService;
 @Service
 public class FsCourseCouponServiceImpl extends ServiceImpl<FsCourseCouponMapper, FsCourseCoupon> implements IFsCourseCouponService {
 
+    @Autowired
+    private FsCourseCouponUserMapper courseCouponUserMapper;
+
+    @Autowired
+    private FsUserCourseVideoMapper userCourseVideoMapper;
+
+    @Autowired
+    private ISysConfigService configService;
+
+    @Autowired
+    private FsUserCourseVideoRedPackageMapper fsUserCourseVideoRedPackageMapper;
     /**
      * 查询课程优惠券
      * 
@@ -98,4 +124,156 @@ public class FsCourseCouponServiceImpl extends ServiceImpl<FsCourseCouponMapper,
     public List<FsCourseCoupon> selectFsCourseCouponOptions() {
         return baseMapper.selectFsCourseCouponOptions();
     }
+
+    //自动
+    @Override
+    @Transactional
+    public R sendAutoCourseCoupon(Long videoId, Long userId,Long companyId) {
+        CourseFinishRewardVO rewardVO = new CourseFinishRewardVO();
+
+        Integer integral = null;
+        BigDecimal amount = null;
+        String couponName = null;
+
+        boolean isSend = true;
+        //课程小节信息
+        FsUserCourseVideo courseVideo = userCourseVideoMapper.selectFsUserCourseVideoByVideoId(videoId);
+        if (courseVideo == null) {
+            return R.error("课程小节不存在");
+        }
+
+        //优惠券
+        FsCourseCoupon fsCourseCoupon = baseMapper.selectFsCourseCouponById(courseVideo.getCourseCouponId());
+        if (fsCourseCoupon != null) {
+            if (fsCourseCoupon.getStatus() != 1) {
+                isSend = false;
+            }
+            if (fsCourseCoupon.getRemainNumber() <= 0) {
+                isSend = false;
+            }
+            //用户领取优惠券总数
+            Integer count = courseCouponUserMapper.selectCountByUserIdAndCouponId(userId, courseVideo.getCourseCouponId());
+            if (count >= fsCourseCoupon.getLimitCount()) {
+                isSend = false;
+            }
+        } else {
+            isSend = false;
+        }
+
+        if (isSend) {
+            //发放优惠券
+            FsCourseCouponUser couponUser = new FsCourseCouponUser();
+            couponUser.setCouponId(courseVideo.getCourseCouponId());
+            couponUser.setUserId(userId);
+            couponUser.setStartTime(new Date());
+            couponUser.setLimitTime(fsCourseCoupon.getLimitTime());
+            couponUser.setCreateTime(new Date());
+            int i = courseCouponUserMapper.insertFsCourseCouponUser(couponUser);
+
+            //返回的优惠券名称
+            couponName = fsCourseCoupon.getTitle();
+            if (i > 0) {
+                FsCourseCoupon coupon = new FsCourseCoupon();
+                coupon.setId(courseVideo.getCourseCouponId());
+                coupon.setRemainNumber(fsCourseCoupon.getRemainNumber() - 1);
+                baseMapper.updateFsCourseCoupon(coupon);
+            }
+        }
+        // 获取配置信息
+        String json = configService.selectConfigByKey("course.config");
+        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
+        Integer rewardType = config.getRewardType();
+
+        if (rewardType == 1) {
+            FsUserCourseVideoRedPackage redPackage = fsUserCourseVideoRedPackageMapper.selectRedPacketByVideoIdAndType(videoId, companyId, 1);
+            if (redPackage != null && redPackage.getRedPacketMoney() != null) {
+                amount = redPackage.getRedPacketMoney();
+            } else if (courseVideo.getRedPacketMoney() != null) {
+                amount = courseVideo.getRedPacketMoney();
+            }
+        }
+        if (rewardType == 2) {
+            integral = config.getAnswerIntegral();
+        }
+        rewardVO.setCouponName(couponName);
+        rewardVO.setIntegral(integral);
+        rewardVO.setRedPacketMoney(amount);
+        return R.ok().put("data",rewardVO);
+    }
+
+    //手动
+    @Transactional
+    @Override
+    public R sendCourseCoupon(Long videoId, Long userId,Long companyId,Long periodId) {
+        CourseFinishRewardVO rewardVO = new CourseFinishRewardVO();
+
+        Integer integral = null;
+        BigDecimal amount = null;
+        String couponName = null;
+
+        boolean isSend = true;
+        //课程小节信息
+        FsUserCourseVideo courseVideo = userCourseVideoMapper.selectFsUserCourseVideoByVideoId(videoId);
+        if (courseVideo == null) {
+            return R.error("课程小节不存在");
+        }
+
+        //优惠券
+        FsCourseCoupon fsCourseCoupon = baseMapper.selectFsCourseCouponById(courseVideo.getCourseCouponId());
+        if (fsCourseCoupon != null) {
+            if (fsCourseCoupon.getStatus() != 1) {
+                isSend = false;
+            }
+            if (fsCourseCoupon.getRemainNumber() <= 0) {
+                isSend = false;
+            }
+            //用户领取优惠券总数
+            Integer count = courseCouponUserMapper.selectCountByUserIdAndCouponId(userId, courseVideo.getCourseCouponId());
+            if (count >= fsCourseCoupon.getLimitCount()) {
+                isSend = false;
+            }
+        } else {
+            isSend = false;
+        }
+
+        if (isSend) {
+            //发放优惠券
+            FsCourseCouponUser couponUser = new FsCourseCouponUser();
+            couponUser.setCouponId(courseVideo.getCourseCouponId());
+            couponUser.setUserId(userId);
+            couponUser.setStartTime(new Date());
+            couponUser.setLimitTime(fsCourseCoupon.getLimitTime());
+            couponUser.setCreateTime(new Date());
+            int i = courseCouponUserMapper.insertFsCourseCouponUser(couponUser);
+
+            //返回的优惠券名称
+            couponName = fsCourseCoupon.getTitle();
+            if (i > 0) {
+                FsCourseCoupon coupon = new FsCourseCoupon();
+                coupon.setId(courseVideo.getCourseCouponId());
+                coupon.setRemainNumber(fsCourseCoupon.getRemainNumber() - 1);
+                baseMapper.updateFsCourseCoupon(coupon);
+            }
+        }
+        // 获取配置信息
+        String json = configService.selectConfigByKey("course.config");
+        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
+        Integer rewardType = config.getRewardType();
+
+        if (rewardType == 1) {
+            FsUserCourseVideoRedPackage redPackage = fsUserCourseVideoRedPackageMapper.selectRedPacketByCompanyId(videoId,companyId,periodId);
+            if (redPackage != null && redPackage.getRedPacketMoney() != null) {
+                amount = redPackage.getRedPacketMoney();
+            } else if (courseVideo.getRedPacketMoney() != null) {
+                amount = courseVideo.getRedPacketMoney();
+            }
+        }
+        if (rewardType == 2) {
+            integral = config.getAnswerIntegral();
+        }
+        rewardVO.setCouponName(couponName);
+        rewardVO.setIntegral(integral);
+        rewardVO.setRedPacketMoney(amount);
+        return R.ok().put("data",rewardVO);
+    }
 }

+ 30 - 0
fs-service/src/main/java/com/fs/his/service/impl/FsCourseCouponUserServiceImpl.java

@@ -1,6 +1,10 @@
 package com.fs.his.service.impl;
 
+import java.util.Date;
 import java.util.List;
+import java.util.Objects;
+
+import com.fs.common.core.domain.R;
 import com.fs.common.utils.DateUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -91,4 +95,30 @@ public class FsCourseCouponUserServiceImpl extends ServiceImpl<FsCourseCouponUse
     {
         return baseMapper.deleteFsCourseCouponUserById(id);
     }
+
+    @Override
+    public R useCoupon(Long userId, Long couponUserId) {
+        FsCourseCouponUser couponUser = baseMapper.selectFsCourseCouponUserById(couponUserId);
+        if (couponUser == null) {
+            return R.error("优惠券不存在");
+        }
+        if (couponUser.getStatus() != 0) {
+            return R.error("优惠券已使用");
+        }
+        if (!Objects.equals(couponUser.getUserId(), userId)) {
+            return R.error("优惠券不属于当前用户");
+        }
+        Date now = new Date();
+        if (now.after(couponUser.getLimitTime())) {
+            return R.error("优惠券已过期");
+        }
+        FsCourseCouponUser map = new FsCourseCouponUser();
+        map.setId(couponUser.getCouponId());
+        map.setStatus(1);
+        int i = baseMapper.updateById(map);
+        if (i > 0) {
+            return R.ok("优惠券使用成功");
+        }
+        return R.error("优惠券使用失败");
+    }
 }

+ 15 - 0
fs-service/src/main/java/com/fs/his/vo/CourseFinishRewardVO.java

@@ -0,0 +1,15 @@
+package com.fs.his.vo;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+@Data
+public class CourseFinishRewardVO {
+
+    private String couponName;
+
+    private BigDecimal redPacketMoney;
+
+    private Integer integral;
+}

+ 5 - 1
fs-service/src/main/resources/mapper/his/FsCourseCouponUserMapper.xml

@@ -13,10 +13,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="createTime"    column="create_time"    />
         <result property="updateTime"    column="update_time"    />
         <result property="status"    column="status"    />
+        <result property="videoId"    column="video_id"    />
     </resultMap>
 
     <sql id="selectFsCourseCouponUserVo">
-        select id, coupon_id, user_id, limit_time, start_time, create_time, update_time, status from fs_course_coupon_user
+        select id, coupon_id, user_id, limit_time, start_time, create_time, update_time, status, video_id from fs_course_coupon_user
     </sql>
 
     <select id="selectFsCourseCouponUserList" parameterType="FsCourseCouponUser" resultMap="FsCourseCouponUserResult">
@@ -45,6 +46,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">create_time,</if>
             <if test="updateTime != null">update_time,</if>
             <if test="status != null">status,</if>
+            <if test="videoId != null">video_id,</if>
          </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="couponId != null">#{couponId},</if>
@@ -54,6 +56,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">#{createTime},</if>
             <if test="updateTime != null">#{updateTime},</if>
             <if test="status != null">#{status},</if>
+            <if test="videoId != null">#{videoId},</if>
          </trim>
     </insert>
 
@@ -67,6 +70,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">create_time = #{createTime},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
             <if test="status != null">status = #{status},</if>
+            <if test="videoId != null">video_id = #{videoId},</if>
         </trim>
         where id = #{id}
     </update>

+ 43 - 0
fs-user-app/src/main/java/com/fs/app/controller/FsCourseCouponController.java

@@ -0,0 +1,43 @@
+package com.fs.app.controller;
+
+import com.fs.app.annotation.Login;
+import com.fs.common.core.domain.R;
+import com.fs.his.param.CourseFinishRewardParam;
+import com.fs.his.service.IFsCourseCouponService;
+import io.swagger.annotations.Api;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+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;
+
+@Api("课程优惠券接口")
+@RestController
+@RequestMapping(value="/app/courseCoupon")
+public class FsCourseCouponController extends AppBaseController {
+
+    Logger logger= LoggerFactory.getLogger(getClass());
+    @Autowired
+    private IFsCourseCouponService fsCourseCouponService;
+
+
+    @Login
+    @PostMapping("/sendAutoReward")
+    public R sendAutoCourseCoupon(@RequestBody CourseFinishRewardParam param){
+        long userId = Long.parseLong(getUserId());
+        param.setUserId(userId);
+        logger.info("发送自动课程优惠券参数:{}",param);
+        return fsCourseCouponService.sendAutoCourseCoupon(param.getVideoId(),param.getUserId(),param.getCompanyId());
+    }
+
+    @Login
+    @PostMapping("/sendReward")
+    public R sendCourseCoupon(@RequestBody CourseFinishRewardParam param){
+        long userId = Long.parseLong(getUserId());
+        param.setUserId(userId);
+        logger.info("发送课程优惠券参数:{}",param);
+        return fsCourseCouponService.sendCourseCoupon(param.getVideoId(),param.getUserId(),param.getCompanyId(),param.getPeriodId());
+    }
+}