package com.linghu.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.linghu.config.FinalStatus;
|
import com.linghu.model.common.ResponseResult;
|
import com.linghu.model.dto.KeywordDto;
|
import com.linghu.model.dto.OrderDto;
|
import com.linghu.model.entity.Keyword;
|
import com.linghu.model.entity.KeywordTask;
|
import com.linghu.model.entity.Orders;
|
import com.linghu.model.entity.Question;
|
import com.linghu.model.page.CustomPage;
|
import com.linghu.service.KeywordService;
|
import com.linghu.service.KeywordTaskService;
|
import com.linghu.service.OrderService;
|
import com.linghu.mapper.OrderMapper;
|
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
import com.linghu.service.QuestionService;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.StringUtils;
|
|
/**
|
* @author xy
|
* @description 针对表【order】的数据库操作Service实现
|
* @createDate 2025-07-04 20:17:33
|
*/
|
@Service
|
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders>
|
implements OrderService {
|
|
@Autowired
|
private KeywordService keywordService;
|
@Autowired
|
private QuestionService questionService;
|
@Autowired
|
private OrderMapper orderMapper;
|
@Autowired
|
private KeywordTaskService keywordTaskService;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public boolean saveOrderWithKeywords(OrderDto orderDto, String order_id) {
|
|
// 如果有关键词,则保存关键词
|
if (StringUtils.hasText(orderDto.getKeywords())) {
|
String[] keywordArray = orderDto.getKeywords().split("\\n");
|
for (String keywordName : keywordArray) {
|
if (StringUtils.hasText(keywordName)) {
|
Keyword keyword = new Keyword();
|
keyword.setOrder_id(order_id);
|
keyword.setKeyword_name(keywordName.trim());
|
keyword.setStatus(FinalStatus.NOTSUBMITTED.getValue());
|
// keyword.setNum(1); // 默认采集轮数为1
|
keywordService.save(keyword);
|
}
|
}
|
|
}
|
|
return true;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public boolean updateOrderWithKeywords(OrderDto orderDto, Integer currentStatus) {
|
// 状态为2或3时禁止修改
|
if (currentStatus >= 2) {
|
throw new RuntimeException("执行中和已完成状态的订单不可修改");
|
}
|
|
// 更新订单基本信息
|
if (!this.updateById(orderDto)) {
|
return false;
|
}
|
|
// 删除旧关键词(当状态为未提交时允许修改关键词)
|
keywordService.lambdaUpdate()
|
.eq(Keyword::getOrder_id, orderDto.getOrder_id())
|
.eq(Keyword::getStatus, FinalStatus.NOTSUBMITTED.getValue())
|
.remove();
|
|
// 保存新关键词
|
if (StringUtils.hasText(orderDto.getKeywords())) {
|
String[] keywordArray = orderDto.getKeywords().split("\\n");
|
for (String keywordName : keywordArray) {
|
if (StringUtils.hasText(keywordName)) {
|
Keyword keyword = new Keyword();
|
keyword.setOrder_id(orderDto.getOrder_id());
|
keyword.setKeyword_name(keywordName.trim());
|
keyword.setStatus(FinalStatus.NOTSUBMITTED.getValue());
|
keywordService.save(keyword);
|
}
|
}
|
}
|
|
return true;
|
}
|
|
@Override
|
public List<KeywordDto> getKeywordListByOrderId(String orderId) {
|
List<Keyword> keywords = keywordService.lambdaQuery()
|
.eq(Keyword::getOrder_id, orderId)
|
.list();
|
|
// 遍历关键词,获取每个关键词对应的提问词
|
List<KeywordDto> keywordDtos = new ArrayList<>();
|
for (Keyword keyword : keywords) {
|
KeywordDto dto = new KeywordDto();
|
BeanUtils.copyProperties(keyword, dto);
|
LambdaQueryWrapper<KeywordTask> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(KeywordTask::getKeyword_id, keyword.getKeyword_id());
|
List<KeywordTask> keywordTasks = keywordTaskService.list(queryWrapper);
|
|
// 提取 task_id 列表
|
List<String> taskIdList = keywordTasks.stream()
|
.map(KeywordTask::getTask_id)
|
.filter(Objects::nonNull)
|
.collect(Collectors.toList());
|
|
dto.setTaskIdList(taskIdList); // 设置 task_id 列表
|
// 查询该关键词下的所有提问词
|
List<Question> questions = questionService.lambdaQuery()
|
.eq(Question::getKeyword_id, keyword.getKeyword_id())
|
.list();
|
dto.setQuestionList(questions);
|
keywordDtos.add(dto);
|
}
|
return keywordDtos;
|
}
|
@Override
|
public Page<String> getClientList(String clientName, Integer pageNum, Integer pageSize) {
|
Page<Orders> page = new Page<>(pageNum, pageSize);
|
|
// 构建查询条件(根据客户名称模糊搜索)
|
LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.select(Orders::getClient_name) // 只查询客户名称字段
|
.eq(Orders::getDel_flag, 0) // 只查询未删除的订单
|
.groupBy(Orders::getClient_name); // 按客户名称分组去重
|
if (clientName != null && !clientName.isEmpty()) {
|
queryWrapper.like(Orders::getClient_name, clientName);
|
}
|
List<Orders> orders = orderMapper.selectList(queryWrapper);
|
|
// 转换为客户选项列表(统计每个客户的订单数量)
|
List<String> clientOptions = orders.stream()
|
.map(Orders::getClient_name)
|
.collect(Collectors.toList());
|
|
// 构建结果分页对象
|
Page<String> resultPage = new Page<>();
|
resultPage.setRecords(clientOptions);
|
|
return resultPage;
|
|
|
}
|
|
@Override
|
public ResponseResult<CustomPage<Orders>> getCustomPageResponseResult(Integer pageNum, Integer pageSize, String clientName, Integer status, String timeRange) {
|
LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(Orders::getDel_flag, 0);
|
|
// 添加查询条件
|
if (clientName != null && !clientName.trim().isEmpty()) {
|
queryWrapper.eq(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);
|
}
|
}
|
// 排序
|
queryWrapper.orderByDesc(Orders::getCreate_time);
|
// 分页查询
|
if (pageNum != null && pageSize != null) {
|
Page<Orders> pageInfo = new Page<>(pageNum, pageSize);
|
Page<Orders> result = this.page(pageInfo, queryWrapper);
|
return ResponseResult.success(new CustomPage<>(result));
|
}
|
|
// 不分页查询:手动创建 CustomPage
|
List<Orders> list = this.list(queryWrapper);
|
CustomPage<Orders> page = new CustomPage<>(new Page<>());
|
page.setRecords(list);
|
page.setTotal(list.size());
|
|
return ResponseResult.success(page);
|
}
|
|
@Override
|
|
public ResponseResult<Orders> getOrdersResponseResult(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());
|
// 1. 查询当天最大的订单号(包含已删除的,适应硬删除场景)
|
LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.likeRight(Orders::getOrder_id, dateStr)
|
.select(Orders::getOrder_id)
|
.orderByDesc(Orders::getOrder_id)
|
.last("LIMIT 1"); // 只取最大的一条
|
Orders maxOrder = this.getOne(queryWrapper);
|
int sequence = 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);
|
|
// 设置初始状态
|
order.setStatus(1); // 待处理
|
order.setDel_flag(0); // 未删除
|
order.setCreate_time(LocalDateTime.now());
|
boolean save = this.save(order);
|
// 保存关键词
|
boolean saveOrderWithKeywords = this.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 = (int) keywordService.count(queryKeywordsQueryWrapper);
|
order.setKeyword_num(count1);
|
order.setUpdate_time(LocalDateTime.now());
|
this.updateById(order);
|
|
if (save) {
|
return ResponseResult.success(order);
|
}
|
return ResponseResult.error("添加订单失败");
|
}
|
|
@Override
|
public ResponseResult<Void> getVoidResponseResult(String orderId) {
|
Orders order = this.getById(orderId);
|
if (order == null) {
|
return ResponseResult.error("订单不存在");
|
}
|
|
order.setDel_flag(1);
|
order.setUpdate_time(LocalDateTime.now());
|
boolean success = this.updateById(order);
|
|
return success ? ResponseResult.success() : ResponseResult.error("删除订单失败");
|
}
|
|
@Override
|
public ResponseResult<Void> updateOrder(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 = this.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 = 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 (this.updateById(orderDto)) {
|
return ResponseResult.success();
|
}
|
return ResponseResult.error("更新订单失败");
|
}
|
|
@Override
|
public ResponseResult<Orders> getOrderById(String orderId) {
|
Orders order = this.getById(orderId);
|
if (order == null || order.getDel_flag() == 1) {
|
return ResponseResult.error("订单不存在");
|
}
|
return ResponseResult.success(order);
|
}
|
|
|
}
|