其他分享
首页 > 其他分享> > 模拟下单模块开发

模拟下单模块开发

作者:互联网

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>


大家学习加油!




标签:下单,newVideoOrder,userId,videoId,int,video,模块,id,模拟
来源: https://www.cnblogs.com/xmcode/p/15640274.html