| | |
| | | package cc.mrbird.febs.ai.service.impl; |
| | | |
| | | import cc.mrbird.febs.ai.entity.AiProductQuestion; |
| | | import cc.mrbird.febs.ai.entity.AiProductQuestionLink; |
| | | import cc.mrbird.febs.ai.mapper.AiProductQuestionMapper; |
| | | import cc.mrbird.febs.ai.service.AiProductQuestionLinkService; |
| | | import cc.mrbird.febs.ai.service.AiProductQuestionService; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * AI产品题目 Service实现类 |
| | |
| | | public class AiProductQuestionServiceImpl extends ServiceImpl<AiProductQuestionMapper, AiProductQuestion> implements AiProductQuestionService { |
| | | |
| | | private final AiProductQuestionMapper aiProductQuestionMapper; |
| | | private final AiProductQuestionLinkService aiProductQuestionLinkService; |
| | | |
| | | |
| | | @Override |
| | | public AiProductQuestion getById(String id) { |
| | | return this.getById(id); |
| | | return aiProductQuestionMapper.selectById( id); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByCompanyId(String companyId) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getCompanyId, companyId); |
| | | queryWrapper.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | public List<AiProductQuestion> getListByQuery(LambdaQueryWrapper<AiProductQuestion> questionLambdaQueryWrapper) { |
| | | return aiProductQuestionMapper.selectList(questionLambdaQueryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByProductCategoryId(String productCategoryId) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getProductCategoryId, productCategoryId); |
| | | queryWrapper.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | public AiProductQuestion createQuestion(String productId) { |
| | | AiProductQuestion aiProductQuestion = new AiProductQuestion(); |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByTitle(String title) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.like(AiProductQuestion::getTitle, title); |
| | | queryWrapper.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByParentId(String parentId) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getParentId, parentId); |
| | | queryWrapper.orderByAsc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByDifficulty(Integer difficulty) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getDifficulty, difficulty); |
| | | queryWrapper.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByState(Integer state) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getState, state); |
| | | queryWrapper.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByCorrectAnswer(Integer correctAnswer) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getCorrectAnswer, correctAnswer); |
| | | queryWrapper.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByCompanyIdAndState(String companyId, Integer state) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getCompanyId, companyId); |
| | | queryWrapper.eq(AiProductQuestion::getState, state); |
| | | queryWrapper.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> getByParentIdAndState(String parentId, Integer state) { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getParentId, parentId); |
| | | queryWrapper.eq(AiProductQuestion::getState, state); |
| | | queryWrapper.orderByAsc(AiProductQuestion::getCreatedTime); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean saveProductQuestion(AiProductQuestion aiProductQuestion) { |
| | | try { |
| | | return this.save(aiProductQuestion); |
| | | } catch (Exception e) { |
| | | log.error("保存AI产品题目失败: ", e); |
| | | return false; |
| | | LambdaQueryWrapper<AiProductQuestionLink> aiProductQuestionLinkLambdaQueryWrapper = Wrappers.lambdaQuery(AiProductQuestionLink.class); |
| | | aiProductQuestionLinkLambdaQueryWrapper.eq(AiProductQuestionLink::getProductId, productId); |
| | | List<AiProductQuestionLink> linkList = aiProductQuestionLinkService.getListByQuery(aiProductQuestionLinkLambdaQueryWrapper); |
| | | if(CollUtil.isNotEmpty(linkList)){ |
| | | List<String> collect = linkList.stream().map(AiProductQuestionLink::getProductQuestionId).collect(Collectors.toList()); |
| | | if(CollUtil.isNotEmpty( collect)){ |
| | | String questionId = RandomUtil.randomEle(collect); |
| | | aiProductQuestion = this.getById(questionId); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean saveBatchProductQuestions(List<AiProductQuestion> productQuestions) { |
| | | try { |
| | | return this.saveBatch(productQuestions); |
| | | } catch (Exception e) { |
| | | log.error("批量保存AI产品题目失败: ", e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean updateProductQuestion(AiProductQuestion aiProductQuestion) { |
| | | try { |
| | | return this.updateById(aiProductQuestion); |
| | | } catch (Exception e) { |
| | | log.error("更新AI产品题目失败: ", e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean deleteById(String id) { |
| | | try { |
| | | return this.removeById(id); |
| | | } catch (Exception e) { |
| | | log.error("删除AI产品题目失败: ", e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean deleteByParentId(String parentId) { |
| | | try { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getParentId, parentId); |
| | | return this.remove(queryWrapper); |
| | | } catch (Exception e) { |
| | | log.error("根据父ID删除AI产品题目失败: ", e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean deleteByCompanyId(String companyId) { |
| | | try { |
| | | LambdaQueryWrapper<AiProductQuestion> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(AiProductQuestion::getCompanyId, companyId); |
| | | return this.remove(queryWrapper); |
| | | } catch (Exception e) { |
| | | log.error("根据公司ID删除AI产品题目失败: ", e); |
| | | return false; |
| | | } |
| | | return aiProductQuestion; |
| | | } |
| | | } |