diff --git a/src/main/java/com/smart/module/car/util/CostUtils.java b/src/main/java/com/smart/module/car/util/CostUtils.java new file mode 100644 index 0000000..09605f7 --- /dev/null +++ b/src/main/java/com/smart/module/car/util/CostUtils.java @@ -0,0 +1,53 @@ +package com.smart.module.car.util; + +import cn.hutool.core.date.DateUnit; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.NumberUtil; +import com.smart.common.util.DateUtils; +import com.smart.module.car.entity.CarParkManage; +import com.smart.module.car.entity.CarParkingRecord; + +import java.math.BigDecimal; +import java.math.BigInteger; + +/** + * 计算费用的工具类 + */ +public class CostUtils { + + /** + * 计算停车费用 + * @param record 停车记录实体对象 + * @param carParkManage 停车场管理实体对象 + * @return 停车费用 + */ + public static BigDecimal calculate(CarParkingRecord record, CarParkManage carParkManage){ + // 计算停车时长(分钟) + long minutes = DateUtil.between(record.getGmtInto(),// 获取停车记录的入场时间 + DateUtils.getTimestamp(), DateUnit.MINUTE);// 获取当前时间 + // 减去免费时长 + minutes = minutes - carParkManage.getFreeTime(); + if(minutes > 0){ + // 计算收费次数(单位时间内的次数) + BigDecimal count = NumberUtil.div(new BigDecimal(minutes), carParkManage.getTimeUnit()); + // 四舍五入取整 + count = NumberUtil.round(count, 0); + // 计算停车费用 + BigDecimal cost = NumberUtil.mul(count, carParkManage.getUnitCost()); + // 四舍五入取整 + cost = NumberUtil.round(cost, 0); + if(cost.compareTo(carParkManage.getMaxMoney()) > 1){ + // 如果费用超过最大费用,则返回最大费用 + return carParkManage.getMaxMoney(); + } + return cost; + }else{ + // 如果停车时长小于等于免费时长,则返回费用为0 + return new BigDecimal(BigInteger.ZERO); + } + } + + public static void main(String[] args) { + System.out.println(NumberUtil.div(4, 2)); + } +} diff --git a/src/main/java/com/smart/module/car/web/ApiController.java b/src/main/java/com/smart/module/car/web/ApiController.java new file mode 100644 index 0000000..71cd27d --- /dev/null +++ b/src/main/java/com/smart/module/car/web/ApiController.java @@ -0,0 +1,152 @@ +package com.smart.module.car.web; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.IdUtil; +import com.smart.common.constant.SystemConstant; +import com.smart.common.model.Result; +import com.smart.common.util.CommonUtils; +import com.smart.common.util.DateUtils; +import com.smart.common.util.OrderUtils; +import com.smart.module.car.entity.CarManage; +import com.smart.module.car.entity.CarParkManage; +import com.smart.module.car.entity.CarParkingRecord; +import com.smart.module.car.repository.CarParkingRecordRepository; +import com.smart.module.car.repository.ParkManageRepository; +import com.smart.module.car.service.CarManageService; +import com.smart.module.car.service.CarParkingRecordService; +import com.smart.module.car.util.BaiDuUtils; +import com.smart.module.car.util.CostUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * 接口管理 + */ +@RestController +@RequestMapping("/api") +public class ApiController { + // 文件保存路径 + @Value("${file.path}") + private String filePath; + + // 百度工具类 + @Autowired + private BaiDuUtils baiDuUtils; + + @Autowired + private ParkManageRepository parkManageRepository; + + @Autowired + private CarManageService carManageService; + + @Autowired + private CarParkingRecordRepository carParkingRecordRepository; + + @Autowired + private CarParkingRecordService carParkingRecordService; + + /** + * 识别车牌接口 + */ + @RequestMapping("distinguish") + public Result distinguish(MultipartFile file, Long id) { + try { + if(file!=null){ + // 通过id查询停车场信息 + CarParkManage carParkManage = + parkManageRepository.findById(id).orElse(new CarParkManage()); + if(id==null||carParkManage==null){ + return Result.error("请选择停车场"); + } + // 创建文件夹和文件,保存上传的图片 + File parentFile = CommonUtils.createParentFile(filePath); + String fileName = file.getOriginalFilename(); + String suffix = fileName.substring(fileName.lastIndexOf(".")); + String uuid = IdUtil.simpleUUID(); + fileName = uuid + suffix; + File imageFile = new File(parentFile,fileName); + FileUtil.writeFromStream(file.getInputStream(), imageFile); + String fileDay = DateUtil.thisYear()+"/"+(DateUtil.thisMonth()+1)+"/"+DateUtil.thisDayOfMonth(); + String imagePath = SystemConstant.FILE + "/" + fileDay+"/"+fileName; + // 调用百度接口识别车牌号码 + String plateNumber = baiDuUtils.plateLicense(imageFile.getAbsolutePath()); + if(StringUtils.isBlank(plateNumber)){ + return Result.error("识别失败"); + } + Map map = new HashMap<>(); + map.put("plateNumber",plateNumber); + map.put("imagePath",imagePath); + + // 根据车牌号查询停车记录 + CarParkingRecord record = + carParkingRecordService.getByPlateNumber(plateNumber,id); + /** + * 出场操作 + */ + CarManage carManage = + carManageService.getByPlateNumber(plateNumber,id); + if(record!=null){ + // 计算停车费用 + record.setCost(CostUtils.calculate(record,carParkManage)); + // 设置出场时间 + record.setGmtOut(DateUtils.getTimestamp()); + map.put("msg","出厂成功"); + /** + * 如果是临时车辆,生成支付订单 + */ + if(carManage.getType().shortValue() == SystemConstant.CAR_TYPE_TEMP){ + String orderNo = record.getOrderNo(); + System.out.println("生成订单,并返回二维码"+orderNo); + } + }else{ + // 新增停车记录 + record = new CarParkingRecord(); + record.setOrgId(carParkManage.getOrgId()); + record.setOrgName(carParkManage.getOrgName()); + record.setParkManageId(carParkManage.getId()); + record.setParkManageName(carParkManage.getName()); + record.setGmtInto(DateUtils.getTimestamp()); + record.setPlateNumber(plateNumber); + + if(carManage!=null){ + record.setType(carManage.getType()); + record.setOrderNo(""); + }else{ + record.setOrderNo(OrderUtils.getOrderNo(id)); + record.setType(SystemConstant.CAR_TYPE_TEMP); + } + map.put("msg","进厂成功"); + } + // 保存停车记录 + carParkingRecordRepository.saveAndFlush(record); + return Result.ok(map); + }else{ + return Result.error(); + } + } catch (Exception e) { + e.printStackTrace(); + return Result.error(); + } + } + + /** + * 支付回调接口 + */ + @RequestMapping("/callBack") + public String callBack() { + /** + * 生成订单、调用支付接口 + */ + return ""; + } +} +