课程名称:多端全栈项目实战:商业级代驾全流程落地
课程章节: 华夏代驾全栈小程序实战
课程讲师: 神思者
课程内容:
后算计算 预估里程
课程收获:
避免postman 模拟客户端 随便输入起点终点里程和时长

首先我们需要编写form类 对传输得数据进行校验
@Data@Schema(description = "预估里程和时间的表单")public class EstimateOrderMileageAndMinuteForm {
@NotBlank(message = "mode不能为空")
@Pattern(regexp = "^driving$|^walking$|^bicycling$")
@Schema(description = "计算方式")
private String mode;
@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 = "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;}之后需要在fegin 调用相应得接口
预估订单金额 每一笔代驾都需要记录订单得预估金额,所以需要根据里程和计费规则,预估出代驾得费用 所以我们需要用到规则引擎
规则引擎可以做到把算法剥离出程序,你可以保存到TXT文件或者数据库里面,用得时候再加载回程序,虽然加载回来得算法是字符串,但是规则引擎可以处理这些字符串,
比如遇到雨雪天气,代驾费用就得上调,如果是淡季 代驾费用就可以下调,既然这些算法要经常得变动,我们肯定不能把算法写死到程序里面。要把算法从程序中抽离,保存再数据库里面,将来要改动计费算法时,只需要新增一条记录,原有得记录也不需要删除。
课程内选择了QLExpress 作为一个嵌入式规则引擎再业务中使用,让规则定义简单且不失灵活,QLExpress 支持标准得java语法,还可以支持自定义得操作符号,操作符重载,函数定义,宏定义,数据延迟加载等。至于其他得规则引擎,由于都不支持复杂语法 所以QLExpress 是不错的选择
首先需要再数据库中创建一个新的表 记录计费规则

因为课程中直接提供了算法 所以我们可以直接调用api
@RestController@RequestMapping("/charge")@Tag(name = "ChargeRuleController", description = "代驾费用的Web接口")public class ChargeRuleController{
@PostMapping("/estimateOrderCharge")
public R estimateOrderCharge(@RequestBody @Valid EstimateOrderChargeForm form) {
……
HashMap map = chargeRuleService.calculateOrderCharge(form.getMileage(), form.getTime(), 0, key);
return R.ok().put("result", map);
}}
之后是使用postman 进行调用 返回数据如下
{
"msg": "success",
"result": {
"amount": "115.00", //总金额
"chargeRuleId": "714601916034166785", //使用的规则ID
"baseMileage": "8", //代驾基础里程
"baseMileagePrice": "85", // 基础里程费
"exceedMileagePrice": "3.5", //超出规定里程后每公里3.5元
"mileageFee": "102.50", //本订单里程费
"baseMinute": "10", //免费等时10分钟
"exceedMinutePrice": "1.0", //超出10分钟后,每分钟1元
"waitingFee": "0.00", //本订单等时费
"baseReturnMileage": "8", //总里程超过8公里后,要加收返程费
"exceedReturnPrice": "1.0", //返程里程是每公里1元
"returnMileage": "12.5", //本订单的返程里程
"returnFee": "12.50", //本订单返程费
},
"code": 200}
随时随地看视频