| | |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | * 新增订单 |
| | | */ |
| | | @PostMapping |
| | | @Transactional // 开启事务 |
| | | @ApiOperation(value = "新增订单") |
| | | public ResponseResult<Orders> add(@RequestBody OrderDto orderDto) { |
| | | // 将dto转entity |
| | |
| | | // 生成订单ID:日期+当天的订单数量(如:202507060001) |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); |
| | | String dateStr = dateFormat.format(new Date()); |
| | | |
| | | // 查询当天订单数量 |
| | | // 1. 查询当天最大的订单号(包含已删除的,适应硬删除场景) |
| | | LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.likeRight(Orders::getOrder_id, dateStr); |
| | | long count = orderService.count(queryWrapper); |
| | | queryWrapper.likeRight(Orders::getOrder_id, dateStr) |
| | | .select(Orders::getOrder_id) |
| | | .orderByDesc(Orders::getOrder_id) |
| | | .last("LIMIT 1"); // 只取最大的一条 |
| | | Orders maxOrder = orderService.getOne(queryWrapper); |
| | | int sequence = 1; // 默认序号 |
| | | |
| | | // 生成订单ID |
| | | String orderId = String.format("%s%04d", dateStr, count + 1); |
| | | if (maxOrder != null && maxOrder.getOrder_id() != null) { |
| | | // 2. 从最大订单号中提取序号(如"202507250005"提取"0005") |
| | | String maxId = maxOrder.getOrder_id(); |
| | | if (maxId.length() == dateStr.length() + 4) { // 校验格式 |
| | | String seqStr = maxId.substring(dateStr.length()); |
| | | sequence = Integer.parseInt(seqStr) + 1; // 序号+1 |
| | | } |
| | | } |
| | | |
| | | // 3. 生成新订单号(补全4位,如1→0001) |
| | | String orderId = String.format("%s%04d", dateStr, sequence); |
| | | order.setOrder_id(orderId); |
| | | |
| | | // 设置初始状态 |
| | |
| | | //更新关键词数量 |
| | | LambdaQueryWrapper<Keyword> queryKeywordsQueryWrapper = new LambdaQueryWrapper<>(); |
| | | queryKeywordsQueryWrapper.eq(Keyword::getOrder_id, order.getOrder_id()); |
| | | int count1 = keywordService.count(queryKeywordsQueryWrapper); |
| | | int count1 = (int) keywordService.count(queryKeywordsQueryWrapper); |
| | | order.setKeyword_num(count1); |
| | | order.setUpdate_time(LocalDateTime.now()); |
| | | orderService.updateById(order); |
| | |
| | | //更新关键词数量 |
| | | LambdaQueryWrapper<Keyword> queryKeywordsQueryWrapper = new LambdaQueryWrapper<>(); |
| | | queryKeywordsQueryWrapper.eq(Keyword::getOrder_id, orderDto.getOrder_id()); |
| | | int count1 = keywordService.count(queryKeywordsQueryWrapper); |
| | | int count1 = (int) keywordService.count(queryKeywordsQueryWrapper); |
| | | orderDto.setKeyword_num(count1); |
| | | |
| | | |