模拟下单模块开发

controller类:

/**
* 下单接口
*/
@PostMapping("save")
public JsonData save(@RequestBody VideoOrderRequest videoOrderRequest, HttpServletRequest request){

Integer userId = (Integer) request.getAttribute("user_id");

if (userId == null){
return JsonData.buildError("");
}
videoOrderService.save(userId,videoOrderRequest.getVideoId());

return JsonData.buildSuccess();
}
接口实现类:

@Autowired
private VideoOrderMapper videoOrderMapper;

@Autowired
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.setVideoId(videoId);
newVideoOrder.setVideoImg(video.getCoverImg());
newVideoOrder.setVideoTitle(video.getTitle());
newVideoOrder.setUserId(userId);
newVideoOrder.setTotalFee(video.getPrice());
newVideoOrder.setOutTradeNo(UUID.randomUUID().toString());
newVideoOrder.setState(1);
newVideoOrder.setCreateTime(new Date());

int row = videoOrderMapper.saveOrder(newVideoOrder);

return row;
}

mapper:
/**
* 查询用户是否购买此商品
* @param userId
* @param videoId
* @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);

xml文件:
<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>


大家学习加油!




上一篇:完整版EXCEL导出 (大框架SpringCloud 业务还是Springboot一套)


下一篇:用maven方式创建web项目