com.ssm.dao.user OrderDetailDao.xml/OrderMasterDao.xml
<!--新增所有列 OrderMasterDao.xml-->
<insert id="insert" keyProperty="billcode" >
insert into dgq.order_master(billCode, createdBy, creationDate, totalPrice, isPayment)
values (#{billCode}, #{createdby}, #{creationdate}, #{totalprice}, #{ispayment})
</insert>
com.ssm.dao OrderDetailDao.java/OrderMasterDao.java
com.ssm.entity OrderDetail.java/OrderMaster.java
Usercount.usercountMoney改为Float类型
// ProviderController
@Autowired
private UserSerivce userSerivce;
@Autowired
private UsercountService usercountService;
@Autowired
private OrderDetailService orderDetailService;
@Autowired
private OrderMasterService orderMasterService;
// 购买=============================================================================================================
@RequestMapping("/buyproduct")
@ResponseBody
public String buyProduct(HttpServletRequest req, HttpServletResponse resp, @RequestParam("proid") Integer id) throws JsonProcessingException {
Map<String, Object> resultMap = new HashMap<>();
Integer userId = ((User) req.getSession().getAttribute(Constant.USER_SESSION)).getId();
// 查商品
Provider provider = providerService.queryProviderById(id);
// 扣余额
Usercount usercountQuery = new Usercount();
usercountQuery.setUserId(userId);
Usercount usercount = usercountService.queryAll(usercountQuery).get(0);
if ( provider.getProductPrice() > usercount.getUsercountMoney() ) {
resultMap.put("buyResult", "noMoney");
return new ObjectMapper().writeValueAsString(resultMap);
}
// 插入订单 + 扣款
try {
OrderMaster orderMaster = new OrderMaster();
orderMaster.setBillcode(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + (int) ((Math.random() * 9 + 1) * Math.pow(10, 8 - 1)));
orderMaster.setCreatedby(userId);
orderMaster.setCreationdate(new Date());
orderMaster.setIspayment(0);
orderMaster.setTotalprice(new Double(provider.getProductPrice().toString()));
OrderMaster one = orderMasterService.insert(orderMaster);
OrderDetail orderDetail = new OrderDetail();
orderDetail.setBillcode(orderMaster.getBillcode());
orderDetail.setProductid(id);
orderDetail.setProductcount(1);
OrderDetail two = orderDetailService.insert(orderDetail);
usercount.setUsercountMoney(usercount.getUsercountMoney() - provider.getProductPrice());
usercountService.update(usercount);
} catch (NumberFormatException e) {
e.printStackTrace();
resultMap.put("buyResult", "false");
return new ObjectMapper().writeValueAsString(resultMap);
}
resultMap.put("buyResult", "true");
return new ObjectMapper().writeValueAsString(resultMap);
}