【九月打卡】第12天 多端全栈项目实战
标签:
SpringBoot
课程名称:多端全栈项目实战:商业级代驾全流程落地
课程章节: 华夏代驾全栈小程序实战
课程讲师: 神思者
课程内容:
创建代驾订单,保存到mysql
课程收获:
接下来需要编写 insertOrder service类
int rows = orderDao.insert(orderEntity);
将传入的数据进行插入
之后判断rows的长度 如果是1则表示插入成功
这时候就要调用第二个sql 去查找刚才插入的订单的uuid
String id = orderDao.searchOrderIdByUUID(orderEntity.getUuid());
之后插入账单数据 (同时 需要把刚才订单的id一同传入)
billEntity.setOrderId(Long.parseLong(id));
rows = orderBillDao.insert(billEntity);
同样也是判断rows 长度为1 表示成功
之后只需要往redis里面插入缓存,配合redis事务用于司机抢单,避免多个司机同时抢单成功(也可以降低数据库的读写压力)
在redis 缓存 未接单的订单id 当订单有司机抢到后 就把司机id写入
如果没有人抢就用none
抢完订单后 需要把缓存销毁 同时更新sql 缓存设置超时15分钟 如果超过15分钟 订单则销毁
防止早上下单 司机晚上才抢(人都走了 hhh~~)
public String insertOrder(OrderEntity orderEntity, OrderBillEntity billEntity) { //插入订单记录 int rows = orderDao.insert(orderEntity); if (rows == 1) { String id = orderDao.searchOrderIdByUUID(orderEntity.getUuid()); //插入订单费用记录 billEntity.setOrderId(Long.parseLong(id)); rows = orderBillDao.insert(billEntity); if (rows == 1) { //往Redis里面插入缓存,配合Redis事务用于司机抢单,避免多个司机同时抢单成功 redisTemplate.opsForValue().set("order#" + id, "none"); redisTemplate.expire("order#" + id, 15, TimeUnit.MINUTES); //缓存15分钟 return id; } else { throw new HxdsException("保存新订单费用失败"); } } else { throw new HxdsException("保存新订单失败"); } }
附上刚才说的代码
接下来需要编写form类 因为三个sql要传ing多参数的
@Data@Schema(description = "顾客下单的表单")public class InsertOrderForm { @NotBlank(message = "uuid不能为空") @Pattern(regexp = "^[0-9A-Za-z]{32}$", message = "uuid内容不正确") @Schema(description = "uuid") private String uuid; @NotNull(message = "customerId不能为空") @Min(value = 1, message = "customerId不能小于1") @Schema(description = "客户ID") private Long customerId; @NotBlank(message = "startPlace不能为空") @Pattern(regexp = "[\\(\\)0-9A-Z#\\-_\\u4e00-\\u9fa5]{2,50}", message = "startPlace内容不正确") @Schema(description = "订单起点") private String startPlace; @NotBlank(message = "startPlaceLatitude不能为空") @Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "startPlaceLatitude内容不正确") @Schema(description = "订单起点的纬度") private String startPlaceLatitude; @NotBlank(message = "startPlaceLongitude不能为空") @Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "startPlaceLongitude内容不正确") @Schema(description = "订单起点的经度") private String startPlaceLongitude; @NotBlank(message = "endPlace不能为空") @Pattern(regexp = "[\\(\\)0-9A-Z#\\-_\\u4e00-\\u9fa5]{2,50}", message = "endPlace内容不正确") @Schema(description = "订单终点") private String endPlace; @NotBlank(message = "endPlaceLatitude不能为空") @Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "endPlaceLatitude内容不正确") @Schema(description = "订单终点的纬度") private String endPlaceLatitude; @NotBlank(message = "endPlaceLongitude不能为空") @Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "endPlaceLongitude内容不正确") @Schema(description = "订单终点的经度") private String endPlaceLongitude; @NotBlank(message = "expectsMileage不能为空") @Pattern(regexp = "^[1-9]\\d*\\.\\d+$|^0\\.\\d*[1-9]\\d*$|^[1-9]\\d*$", message = "expectsMileage内容不正确") @Schema(description = "预估代驾公里数") private String expectsMileage; @NotBlank(message = "expectsFee不能为空") @Pattern(regexp = "^[1-9]\\d*\\.\\d{1,2}$|^0\\.\\d{1,2}$|^[1-9]\\d*$", message = "expectsFee内容不正确") @Schema(description = "预估代驾费用") private String expectsFee; @NotBlank(message = "favourFee不能为空") @Pattern(regexp = "^[1-9]\\d*\\.\\d{1,2}$|^0\\.\\d{1,2}$|^[1-9]\\d*$", message = "favourFee内容不正确") @Schema(description = "顾客好处费") private String favourFee; @NotNull(message = "chargeRuleId不能为空") @Min(value = 1, message = "chargeRuleId不能小于1") @Schema(description = "规则ID") private Long chargeRuleId; @NotBlank(message = "carPlate不能为空") @Pattern(regexp = "^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})$", message = "carPlate内容不正确") @Schema(description = "车牌号") private String carPlate; @NotBlank(message = "carType不能为空") @Pattern(regexp = "^[\\u4e00-\\u9fa5A-Za-z0-9\\-\\_\\s]{2,20}$", message = "carType内容不正确") @Schema(description = "车型") private String carType; @NotBlank(message = "date不能为空") @Pattern(regexp = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$", message = "date内容不正确") @Schema(description = "日期") private String date; @NotNull(message = "baseMileage不能为空") @Min(value = 1, message = "baseMileage不能小于1") @Schema(description = "基础里程(公里)") private Short baseMileage; @NotBlank(message = "baseMileagePrice不能为空") @Pattern(regexp = "^[1-9]\\d*\\.\\d{1,2}$|^0\\.\\d{1,2}$|^[1-9]\\d*$", message = "baseMileagePrice内容不正确") @Schema(description = "基础里程价格") private String baseMileagePrice; @NotBlank(message = "exceedMileagePrice不能为空") @Pattern(regexp = "^[1-9]\\d*\\.\\d{1,2}$|^0\\.\\d{1,2}$|^[1-9]\\d*$", message = "exceedMileagePrice内容不正确") @Schema(description = "超出基础里程的价格") private String exceedMileagePrice; @NotNull(message = "baseMinute不能为空") @Min(value = 1, message = "baseMinute不能小于1") @Schema(description = "基础分钟") private Short baseMinute; @NotBlank(message = "exceedMinutePrice不能为空") @Pattern(regexp = "^[1-9]\\d*\\.\\d{1,2}$|^0\\.\\d{1,2}$|^[1-9]\\d*$", message = "exceedMinutePrice内容不正确") @Schema(description = "超出基础分钟的价格") private String exceedMinutePrice; @NotNull(message = "baseReturnMileage不能为空") @Min(value = 1, message = "baseReturnMileage不能小于1") @Schema(description = "基础返程里程(公里)") private Short baseReturnMileage; @NotBlank(message = "exceedReturnPrice不能为空") @Pattern(regexp = "^[1-9]\\d*\\.\\d{1,2}$|^0\\.\\d{1,2}$|^[1-9]\\d*$", message = "exceedReturnPrice内容不正确") @Schema(description = "超出基础返程里程的价格") private String exceedReturnPrice;}
经纬度在数据库里面是string
需要进行转换
JSONObject json = new JSONObject(); json.set("latitude", form.getStartPlaceLatitude()); json.set("longitude", form.getStartPlaceLongitude()); orderEntity.setStartPlaceLocation(json.toString());
java里面浮点数不够精确所以也需要进行处理
orderEntity.setExpectsMileage(new BigDecimal(form.getExpectsMileage())); orderEntity.setExpectsFee(new BigDecimal(form.getExpectsFee())); orderEntity.setFavourFee(new BigDecimal(form.getFavourFee()));
接下来 就可以进行测试拉~~
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦