1
0
mirror of https://gitee.com/coder-xiaomo/flashsale synced 2025-01-10 19:58:14 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

交易下单3-下单部分

This commit is contained in:
程序员小墨 2022-03-03 23:37:17 +08:00
parent e3917d1ff1
commit 49d3380c40
6 changed files with 109 additions and 8 deletions

View File

@ -62,6 +62,26 @@
>
> - 可以用 `org.springframework.beans` 包中的 `BeanUtils.copyProperties` 进行DO转Model或Model转DO要字段和类型完全一致的字段才能拷贝其他不对应字段的需要手动设置
7.
7. Service返回Controller前完成将DO转换为Model同时进行业务处理最后返回Model
8. Service返回Controller前将DO
8. 创建Controller继承BaseController在class上添加注解
```java
@Controller("/item")
@RequestMapping("/item")
@CrossOrigin(allowedHeaders = "*", allowCredentials = "true")
```
9. 创建Controller方法添加以下注释
```java
// POST
@RequestMapping(value = "/create", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
// GET
@RequestMapping(value = "/get", method = {RequestMethod.GET})
@ResponseBody
```
10.

View File

@ -43,7 +43,10 @@
<label class="control-label" id="sales">
</div>
</div>
<div>
<div class="form-actions">
<button class="btn blue" id="createorder" type="submit">
下单
</button>
<a href="listitem.html">返回</a>
</div>
</div>
@ -82,6 +85,29 @@
alert("获取信息失败,原因为" + data.responseText);
}
})
// 下单
$("#createorder").on("click", function () {
$.ajax({
type: "POST",
url: "http://localhost:8090/order/createorder",
data: {
"itemId": g_itemVO.id,
"amount": 1, // 暂时写死买1件
},
xhrFields: {withCredentials: true},
success: function (data) {
if (data.status == "success") {
// TODO
} else {
alert("下单失败,原因为" + data.data.errMsg);
}
},
error: function (data) {
alert("下单失败,原因为" + data.responseText);
}
})
});
})
function reloadDom() {

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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();
}
}

View File

@ -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>