huliguo
3 天以前 1cb09e4cde9fb97b8369478a9fc16418ac4e29a4
src/main/java/com/linghu/service/impl/OrderServiceImpl.java
@@ -1,22 +1,117 @@
package com.linghu.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linghu.model.entity.Order;
import com.linghu.model.dto.KeywordDto;
import com.linghu.model.dto.OrderDto;
import com.linghu.model.entity.Keyword;
import com.linghu.model.entity.Orders;
import com.linghu.model.entity.Question;
import com.linghu.service.KeywordService;
import com.linghu.service.OrderService;
import com.linghu.mapper.OrderMapper;
import java.util.ArrayList;
import java.util.List;
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-02 16:32:19
*/
 * @author xy
 * @description 针对表【order】的数据库操作Service实现
 * @createDate 2025-07-04 20:17:33
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders>
        implements OrderService {
    @Autowired
    private KeywordService keywordService;
    @Autowired
    private QuestionService questionService;
    @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("notSubmitted");
                    // 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;
        }
        // 删除旧关键词(当状态为1时允许修改关键词)
        keywordService.lambdaUpdate()
                .eq(Keyword::getOrder_id, orderDto.getOrder_id())
                .eq(Keyword::getStatus, "notSubmitted")
                .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("notSubmitted");
                    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);
            // 查询该关键词下的所有提问词
            List<Question> questions = questionService.lambdaQuery()
                    .eq(Question::getKeyword_id, keyword.getKeyword_id())
                    .list();
            dto.setQuestionList(questions);
            keywordDtos.add(dto);
        }
        return keywordDtos;
    }
}