mirror of
https://gitee.com/coder-xiaomo/flashsale
synced 2025-09-13 07:21:38 +08:00
交易下单3-下单部分
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.cxyxiaomo.flashsale.controller;
|
||||
|
||||
import com.cxyxiaomo.flashsale.error.BusinessException;
|
||||
import com.cxyxiaomo.flashsale.response.CommonReturnType;
|
||||
import com.cxyxiaomo.flashsale.service.OrderService;
|
||||
import com.cxyxiaomo.flashsale.service.model.OrderModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller("/order")
|
||||
@RequestMapping("/order")
|
||||
@CrossOrigin(allowedHeaders = "*", allowCredentials = "true")
|
||||
public class OrderController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
// 封装下单请求
|
||||
public CommonReturnType createOrder(@RequestParam(name = "itemId") Integer itemId,
|
||||
@RequestParam(name = "amount") Integer amount) throws BusinessException {
|
||||
OrderModel orderModel = orderService.createOrder(null, itemId, amount);
|
||||
|
||||
return CommonReturnType.create(null);
|
||||
}
|
||||
}
|
@@ -50,4 +50,6 @@ public interface SequenceDOMapper {
|
||||
* @mbg.generated Thu Mar 03 23:07:17 CST 2022
|
||||
*/
|
||||
int updateByPrimaryKey(SequenceDO record);
|
||||
|
||||
SequenceDO getSequenceByName(String name);
|
||||
}
|
@@ -1,7 +1,9 @@
|
||||
package com.cxyxiaomo.flashsale.service.impl;
|
||||
|
||||
import com.cxyxiaomo.flashsale.dao.OrderDOMapper;
|
||||
import com.cxyxiaomo.flashsale.dao.SequenceDOMapper;
|
||||
import com.cxyxiaomo.flashsale.dataobject.OrderDO;
|
||||
import com.cxyxiaomo.flashsale.dataobject.SequenceDO;
|
||||
import com.cxyxiaomo.flashsale.error.BusinessException;
|
||||
import com.cxyxiaomo.flashsale.error.EmBusinessError;
|
||||
import com.cxyxiaomo.flashsale.service.ItemService;
|
||||
@@ -13,6 +15,7 @@ import com.cxyxiaomo.flashsale.service.model.UserModel;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -31,6 +34,9 @@ public class OrderServiceImpl implements OrderService {
|
||||
@Autowired
|
||||
private OrderDOMapper orderDOMapper;
|
||||
|
||||
@Autowired
|
||||
private SequenceDOMapper sequenceDOMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public OrderModel createOrder(Integer userId, Integer itemId, Integer amount) throws BusinessException {
|
||||
@@ -65,13 +71,13 @@ public class OrderServiceImpl implements OrderService {
|
||||
orderModel.setOrderPrice(itemModel.getPrice().multiply(new BigDecimal(amount)));
|
||||
|
||||
// 生成交易流水号,订单号
|
||||
orderModel.setId(generateOrderNo());
|
||||
|
||||
OrderDO orderDO = convertFromOrderModel(orderModel);
|
||||
orderDOMapper.insertSelective(orderDO);
|
||||
|
||||
// 4. 返回前端
|
||||
|
||||
return null;
|
||||
return orderModel;
|
||||
}
|
||||
|
||||
private OrderDO convertFromOrderModel(OrderModel orderModel) {
|
||||
@@ -85,6 +91,7 @@ public class OrderServiceImpl implements OrderService {
|
||||
}
|
||||
|
||||
// 生成交易流水号,订单号
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW) // 即使包在事务的函数中也新开一个事务来提交,保证全局唯一性
|
||||
private String generateOrderNo() {
|
||||
// 订单号为16位
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
@@ -94,10 +101,22 @@ public class OrderServiceImpl implements OrderService {
|
||||
String nowDate = now.format(DateTimeFormatter.ISO_DATE).replace("-", "");
|
||||
stringBuilder.append(nowDate);
|
||||
|
||||
// 中间6位为自增序列
|
||||
// 中间6位为自增序列 (暂时不考虑序列超出情况,可增加init和max数据表列,当达到最大时恢复为初始值,作为循环序列使用)
|
||||
// 获取当前Sequence
|
||||
int sequence = 0;
|
||||
SequenceDO sequenceDO = sequenceDOMapper.getSequenceByName("order_info");
|
||||
sequence = sequenceDO.getCurrentValue();
|
||||
sequenceDO.setCurrentValue(sequenceDO.getCurrentValue() + sequenceDO.getStep());
|
||||
sequenceDOMapper.updateByPrimaryKeySelective(sequenceDO);
|
||||
String sequenceStr = String.valueOf(sequence);
|
||||
for (int i = 0; i < 6 - sequenceStr.length(); i++) { // 补齐前面的 0
|
||||
stringBuilder.append(0);
|
||||
}
|
||||
stringBuilder.append(sequenceStr);
|
||||
|
||||
// 最后2位为分库分表位
|
||||
stringBuilder.append("00"); // 暂时写死
|
||||
return "";
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
@@ -108,4 +108,10 @@
|
||||
step = #{step,jdbcType=INTEGER}
|
||||
where name = #{name,jdbcType=VARCHAR}
|
||||
</update>
|
||||
<select id="getSequenceByName" parameterType="java.lang.String" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from sequence_info
|
||||
where name = #{name,jdbcType=VARCHAR} for update
|
||||
</select>
|
||||
</mapper>
|
Reference in New Issue
Block a user