guyue
7 天以前 9daf1936174685bad526ed41a026d2f2c421f6b0
src/main/java/com/linghu/controller/OrderController.java
@@ -4,17 +4,22 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.linghu.model.common.ResponseResult;
import com.linghu.model.entity.Keyword;
import com.linghu.model.entity.Orders;
import com.linghu.model.dto.KeywordDto;
import com.linghu.model.dto.OrderDto;
import com.linghu.model.page.CustomPage;
import com.linghu.service.KeywordService;
import com.linghu.service.OrderService;
import io.swagger.annotations.Api;
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;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
@@ -31,10 +36,14 @@
    @Autowired
    private OrderService orderService;
    @Autowired
    private KeywordService keywordService;
    /**
     * 新增订单
     */
    @PostMapping
    @Transactional // 开启事务
    @ApiOperation(value = "新增订单")
    public ResponseResult<Orders> add(@RequestBody OrderDto orderDto) {
        // 将dto转entity
@@ -48,14 +57,26 @@
        // 生成订单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);
        // 设置初始状态
@@ -68,6 +89,13 @@
        if (!saveOrderWithKeywords) {
            return ResponseResult.error("添加关键词失败");
        }
        //更新关键词数量
        LambdaQueryWrapper<Keyword> queryKeywordsQueryWrapper =  new LambdaQueryWrapper<>();
        queryKeywordsQueryWrapper.eq(Keyword::getOrder_id, order.getOrder_id());
        int count1 = (int) keywordService.count(queryKeywordsQueryWrapper);
        order.setKeyword_num(count1);
        order.setUpdate_time(LocalDateTime.now());
        orderService.updateById(order);
        if (save) {
            return ResponseResult.success(order);
@@ -98,22 +126,38 @@
     */
    @PutMapping
    @ApiOperation(value = "更新订单")
    public ResponseResult<Void> update(@RequestBody Orders order) {
        if (order.getOrder_id() == null) {
    public ResponseResult<Void> update(@RequestBody OrderDto orderDto) {
        if (orderDto.getOrder_id() == null) {
            return ResponseResult.error("订单ID不能为空");
        }
        if (order.getClient_name() == null || order.getClient_name().trim().isEmpty()) {
        if (orderDto.getClient_name() == null || orderDto.getClient_name().trim().isEmpty()) {
            return ResponseResult.error("客户名称不能为空");
        }
        Orders existingOrder = orderService.getById(order.getOrder_id());
        Orders existingOrder = orderService.getById(orderDto.getOrder_id());
        if (existingOrder == null) {
            return ResponseResult.error("订单不存在");
        }
        order.setUpdate_time(LocalDateTime.now());
        orderDto.setUpdate_time(LocalDateTime.now());
        if (orderDto.getKeywords()!= null&&orderDto.getKeywords().trim().length() > 0){
            // 保存关键词
            boolean saveOrderWithKeywords = orderService.saveOrderWithKeywords(orderDto, orderDto.getOrder_id());
            if (!saveOrderWithKeywords) {
                return ResponseResult.error("添加关键词失败");
            }
            //更新订单状态,新增关键词
            orderDto.setStatus(1);
        }
        //更新关键词数量
        LambdaQueryWrapper<Keyword> queryKeywordsQueryWrapper =  new LambdaQueryWrapper<>();
        queryKeywordsQueryWrapper.eq(Keyword::getOrder_id, orderDto.getOrder_id());
        int count1 = (int) keywordService.count(queryKeywordsQueryWrapper);
        orderDto.setKeyword_num(count1);
        if (orderService.updateById(order)) {
        if (orderService.updateById(orderDto)) {
            return ResponseResult.success();
        }
        return ResponseResult.error("更新订单失败");
@@ -137,42 +181,88 @@
     */
    @GetMapping
    @ApiOperation("查询订单列表")
    public ResponseResult<List<Orders>> list(
    public ResponseResult<CustomPage<Orders>> list(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String clientName,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String createTime) {
            @RequestParam(required = false) String timeRange) {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getDel_flag, 0); // 只查询未删除的订单
        queryWrapper.eq(Orders::getDel_flag, 0);
        // 添加查询条件
        if (clientName != null && !clientName.trim().isEmpty()) {
            queryWrapper.like(Orders::getClient_name, clientName);
            queryWrapper.eq(Orders::getClient_name, clientName);
        }
        if (status != null) {
            queryWrapper.eq(Orders::getStatus, status);
        }
        if (createTime != null) {
            queryWrapper.like(Orders::getCreate_time, createTime);
        }
        // 改造时间筛选逻辑
        if (timeRange != null && !timeRange.trim().isEmpty()) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime startTime = null;
            switch (timeRange.trim()) {
                case "week":
                    startTime = now.minusWeeks(1);
                    break;
                case "month":
                    startTime = now.minusMonths(1);
                    break;
                case "threeMonths":
                    startTime = now.minusMonths(3);
                    break;
                case "year":
                    startTime = now.minusYears(1);
                    break;
                default:
                    // 可添加日志记录无效参数
                    break;
            }
            if (startTime != null) {
                queryWrapper.ge(Orders::getCreate_time, startTime);
            }
        }
        // 排序
        queryWrapper.orderByDesc(Orders::getCreate_time);
        // 分页查询
        if (pageNum != null && pageSize != null) {
            Page<Orders> pageInfo = new Page<>(pageNum, pageSize);
            Page<Orders> result = orderService.page(pageInfo, queryWrapper);
            return ResponseResult.success(result.getRecords());
            return ResponseResult.success(new CustomPage<>(result));
        }
        // 不分页
        // 不分页查询:手动创建 CustomPage
        List<Orders> list = orderService.list(queryWrapper);
        return ResponseResult.success(list);
        CustomPage<Orders> page = new CustomPage<>(new Page<>());
        page.setRecords(list);
        page.setTotal(list.size());
        return ResponseResult.success(page);
    }
    /**
     * 获取客户列表
     * @param
     * @return
     */
    @GetMapping("/clientList")
    @ApiOperation("获取客户列表")
    public ResponseResult<CustomPage<String>> getClientList(@RequestParam(required = false) String clientName,
                                                      @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "100") Integer pageSize) {
        Page<String> result = orderService.getClientList(clientName,pageNum, pageSize);
        return ResponseResult.success(new CustomPage<>( result));
    }
    @GetMapping("/{orderId}/keywordList")
    @ApiOperation("获取订单关联的关键词及提问词")
    public ResponseResult<List<KeywordDto>> getKeywordList(@PathVariable String orderId) {
    public ResponseResult<List<KeywordDto>> getKeywordList(@PathVariable String orderId){
        List<KeywordDto> result = orderService.getKeywordListByOrderId(orderId);
        return ResponseResult.success(result);
    }