guyue
2 天以前 d6011fd37ef2ff794d8efa93932bdf98d8f76dda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.linghu.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
 
/**
 * 订单管理接口
 */
@RestController
@RequestMapping("/orders")
@Api(value = "订单管理接口", tags = "订单管理")
public class OrderController {
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private KeywordService keywordService;
 
    /**
     * 新增订单
     */
    @PostMapping
    @ApiOperation(value = "新增订单")
    public ResponseResult<Orders> add(@RequestBody OrderDto orderDto) {
        // 将dto转entity
        Orders order = new Orders();
        BeanUtils.copyProperties(orderDto, order);
 
        if (order.getClient_name() == null || order.getClient_name().trim().isEmpty()) {
            return ResponseResult.error("客户名称不能为空");
        }
 
        // 生成订单ID:日期+当天的订单数量(如:202507060001)
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateStr = dateFormat.format(new Date());
 
        // 查询当天订单数量
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(Orders::getOrder_id, dateStr);
        long count = orderService.count(queryWrapper);
 
        // 生成订单ID
        String orderId = String.format("%s%04d", dateStr, count + 1);
        order.setOrder_id(orderId);
 
        // 设置初始状态
        order.setStatus(1); // 待处理
        order.setDel_flag(0); // 未删除
        order.setCreate_time(LocalDateTime.now());
        boolean save = orderService.save(order);
        // 保存关键词
        boolean saveOrderWithKeywords = orderService.saveOrderWithKeywords(orderDto, order.getOrder_id());
        if (!saveOrderWithKeywords) {
            return ResponseResult.error("添加关键词失败");
        }
        //更新关键词数量
        LambdaQueryWrapper<Keyword> queryKeywordsQueryWrapper =  new LambdaQueryWrapper<>();
        queryKeywordsQueryWrapper.eq(Keyword::getOrder_id, order.getOrder_id());
        int count1 = keywordService.count(queryKeywordsQueryWrapper);
        order.setKeyword_num(count1);
        order.setUpdate_time(LocalDateTime.now());
        orderService.updateById(order);
 
        if (save) {
            return ResponseResult.success(order);
        }
        return ResponseResult.error("添加订单失败");
    }
 
    /**
     * 删除订单(逻辑删除)
     */
    @DeleteMapping("/{orderId}")
    @ApiOperation(value = "删除订单")
    public ResponseResult<Void> delete(@PathVariable String orderId) {
        Orders order = orderService.getById(orderId);
        if (order == null) {
            return ResponseResult.error("订单不存在");
        }
 
        order.setDel_flag(1);
        order.setUpdate_time(LocalDateTime.now());
        boolean success = orderService.updateById(order);
 
        return success ? ResponseResult.success() : ResponseResult.error("删除订单失败");
    }
 
    /**
     * 更新订单
     */
    @PutMapping
    @ApiOperation(value = "更新订单")
    public ResponseResult<Void> update(@RequestBody OrderDto orderDto) {
        if (orderDto.getOrder_id() == null) {
            return ResponseResult.error("订单ID不能为空");
        }
        if (orderDto.getClient_name() == null || orderDto.getClient_name().trim().isEmpty()) {
            return ResponseResult.error("客户名称不能为空");
        }
 
        Orders existingOrder = orderService.getById(orderDto.getOrder_id());
        if (existingOrder == null) {
            return ResponseResult.error("订单不存在");
        }
 
        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("添加关键词失败");
            }
        }
        //更新关键词数量
        LambdaQueryWrapper<Keyword> queryKeywordsQueryWrapper =  new LambdaQueryWrapper<>();
        queryKeywordsQueryWrapper.eq(Keyword::getOrder_id, orderDto.getOrder_id());
        int count1 = keywordService.count(queryKeywordsQueryWrapper);
        orderDto.setKeyword_num(count1);
 
 
 
        if (orderService.updateById(orderDto)) {
            return ResponseResult.success();
        }
        return ResponseResult.error("更新订单失败");
    }
 
    /**
     * 根据ID查询订单
     */
    @GetMapping("/{orderId}")
    @ApiOperation("根据ID查询订单")
    public ResponseResult<Orders> getById(@PathVariable String orderId) {
        Orders order = orderService.getById(orderId);
        if (order == null || order.getDel_flag() == 1) {
            return ResponseResult.error("订单不存在");
        }
        return ResponseResult.success(order);
    }
 
    /**
     * 查询订单列表
     */
    @GetMapping
    @ApiOperation("查询订单列表")
    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 timeRange) {
 
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getDel_flag, 0);
 
        // 添加查询条件
        if (clientName != null && !clientName.trim().isEmpty()) {
            queryWrapper.like(Orders::getClient_name, clientName);
        }
        if (status != null) {
            queryWrapper.eq(Orders::getStatus, status);
        }
        // 改造时间筛选逻辑
        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);
            }
        }
        // 分页查询
        if (pageNum != null && pageSize != null) {
            Page<Orders> pageInfo = new Page<>(pageNum, pageSize);
            Page<Orders> result = orderService.page(pageInfo, queryWrapper);
            return ResponseResult.success(new CustomPage<>(result));
        }
 
        // 不分页查询:手动创建 CustomPage
        List<Orders> list = orderService.list(queryWrapper);
        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 = "10") 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){
        List<KeywordDto> result = orderService.getKeywordListByOrderId(orderId);
        return ResponseResult.success(result);
    }
}