开发用户下单购买视频接口
VideoOrder模块下单接口开发
VideoOrderController.java
package net.ybclass.online_ybclass.controller;
import net.ybclass.online_ybclass.model.request.VideoOrderRequest;
import net.ybclass.online_ybclass.service.VideoOrderService;
import net.ybclass.online_ybclass.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/api/v1/pri/order")
public class VideoOrderController {
@Autowired(required = false)
private VideoOrderService videoOrderService;
/**
* 下单接口
*
* @return
*/
@RequestMapping("save")
public JsonData saveOrder(@RequestBody VideoOrderRequest videoOrderRequest, HttpServletRequest request) {
Integer userId = (Integer) request.getAttribute("user_id");
int rows = videoOrderService.save(userId, videoOrderRequest.getVideoId());
return rows == 0 ? JsonData.buildError("下单失败") : JsonData.buildSuccess();
}
}
VideoOrderService.java
package net.ybclass.online_ybclass.service;
public interface VideoOrderService {
int save(int userId, int videoId);
}
VideoOrderServiceImpl.java
package net.ybclass.online_ybclass.service.impl;
import net.ybclass.online_ybclass.mapper.UserMapper;
import net.ybclass.online_ybclass.mapper.VideoMapper;
import net.ybclass.online_ybclass.mapper.VideoOrderMapper;
import net.ybclass.online_ybclass.model.entity.Video;
import net.ybclass.online_ybclass.model.entity.VideoOrder;
import net.ybclass.online_ybclass.service.VideoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;
@Service
public class VideoOrderServiceImpl implements VideoOrderService {
@Autowired(required = false)
private VideoOrderMapper videoOrderMapper;
@Autowired(required = false)
private VideoMapper videoMapper;
/**
* 下单操作
* 未来版本:优惠卷抵扣,风控用户检测,生成订单基础信息,生成支付信息
*
* @param userId
* @param videoId
* @return
*/
@Override
public int save(int userId, int videoId) {
//判断是否已经购买
VideoOrder videoOrder = videoOrderMapper.findByUserIdAndVideoIdAndState(userId, videoId, 1);
if (videoOrder != null) return 0;
Video video = videoMapper.findById(videoId);
VideoOrder newVideoOrder = new VideoOrder();
newVideoOrder.setCreateTime(new Date());
newVideoOrder.setOutTradeNo(UUID.randomUUID().toString());
newVideoOrder.setState(1);
newVideoOrder.setTotalFee(video.getPrice());
newVideoOrder.setUserId(userId);
newVideoOrder.setVideoId(videoId);
newVideoOrder.setVideoImg(video.getConverImg());
newVideoOrder.setVideoTitle(video.getTitle());
int rows = videoOrderMapper.saveOrder(newVideoOrder);
return rows;
}
}
VideoOrderMapper.java
package net.ybclass.online_ybclass.mapper;
import net.ybclass.online_ybclass.model.entity.VideoOrder;
import org.apache.ibatis.annotations.Param;
public interface VideoOrderMapper {
/**
* 查询用户是否购买过此商品
*
* @param userId 用户id
* @param videoId 视频id
* @param state 状态
* @return
*/
VideoOrder findByUserIdAndVideoIdAndState(@Param("user_id") int userId, @Param("video_id") int videoId, @Param("state") int state);
/**
* 下单
*
* @param videoOrder
* @return
*/
int saveOrder(VideoOrder videoOrder);
}
VideoOrderMapper.xml
<?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="net.ybclass.online_ybclass.mapper.VideoOrderMapper">
<!-- 查询用户是否购买过此商品 -->
<select id="findByUserIdAndVideoIdAndState" resultType="VideoOrder">
SELECT * FROM video_order WHERE user_id=#{user_id} AND video_id=#{video_id} AND state=#{state}
</select>
<!-- 下单,并返回主键值 -->
<insert id="saveOrder" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO `video_order`( `out_trade_no`, `state`, `create_time`, `total_fee`, `video_id`, `video_title`, `video_img`, `user_id`)
VALUES (#{outTradeNo,jdbcType=VARCHAR},#{state,jdbcType=INTEGER}
,#{createTime,jdbcType=TIMESTAMP},#{totalFee,jdbcType=INTEGER}
,#{videoId,jdbcType=INTEGER},#{videoTitle,jdbcType=VARCHAR}
,#{videoImg,jdbcType=VARCHAR},#{userId,jdbcType=INTEGER});
</insert>
</mapper>
演示