package com.vetti.hotake.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.hotake.domain.HotakeAiInterviewQuestionsInfo;
import com.vetti.hotake.domain.dto.AiQuestionDto;
import com.vetti.hotake.mapper.HotakeAiInterviewQuestionsInfoMapper;
import com.vetti.hotake.service.IHotakeAiInterviewQuestionsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * AI面试问题信息Service业务层处理
 * 
 * @author wangxiangshun
 * @date 2025-12-14
 */
@SuppressWarnings("all")
@Service
public class HotakeAiInterviewQuestionsInfoServiceImpl extends BaseServiceImpl implements IHotakeAiInterviewQuestionsInfoService
{
    @Autowired
    private HotakeAiInterviewQuestionsInfoMapper hotakeAiInterviewQuestionsInfoMapper;

    /**
     * 查询AI面试问题信息
     * 
     * @param id AI面试问题信息主键
     * @return AI面试问题信息
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeAiInterviewQuestionsInfo selectHotakeAiInterviewQuestionsInfoById(Long id)
    {
        HotakeAiInterviewQuestionsInfo questionsInfo = hotakeAiInterviewQuestionsInfoMapper.selectHotakeAiInterviewQuestionsInfoById(id);
        if(questionsInfo != null){
            Integer questionNum = 0;
            String times = "0";
            if(StrUtil.isNotEmpty(questionsInfo.getQuestionJson())){
                List<AiQuestionDto> aiQuestionList  = JSONUtil.toList(questionsInfo.getQuestionJson(), AiQuestionDto.class);
                questionsInfo.setAiQuestionList(aiQuestionList);
                questionNum = aiQuestionList.size();
                times = String.valueOf(aiQuestionList.size() * 1.5);
            }
            questionsInfo.setQuestionNum(questionNum);
            questionsInfo.setTimes(times);
        }
        return questionsInfo;
    }

    /**
     * 查询AI面试问题信息列表
     * 
     * @param hotakeAiInterviewQuestionsInfo AI面试问题信息
     * @return AI面试问题信息
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeAiInterviewQuestionsInfo> selectHotakeAiInterviewQuestionsInfoList(HotakeAiInterviewQuestionsInfo hotakeAiInterviewQuestionsInfo)
    {
        List<HotakeAiInterviewQuestionsInfo> questionsInfoList = hotakeAiInterviewQuestionsInfoMapper.selectHotakeAiInterviewQuestionsInfoList(hotakeAiInterviewQuestionsInfo);
        if(CollectionUtil.isNotEmpty(questionsInfoList)){
            for (HotakeAiInterviewQuestionsInfo questionsInfo : questionsInfoList) {
                if(questionsInfo != null){
                    Integer questionNum = 0;
                    String times = "0";
                    if(StrUtil.isNotEmpty(questionsInfo.getQuestionJson())){
                        List<AiQuestionDto> aiQuestionList  = JSONUtil.toList(questionsInfo.getQuestionJson(), AiQuestionDto.class);
                        questionsInfo.setAiQuestionList(aiQuestionList);
                        questionNum = aiQuestionList.size();
                        times = String.valueOf(aiQuestionList.size() * 1.5);
                    }
                    questionsInfo.setQuestionNum(questionNum);
                    questionsInfo.setTimes(times);
                }
            }
        }
        return questionsInfoList;
    }

    /**
     * 新增AI面试问题信息
     * 
     * @param hotakeAiInterviewQuestionsInfo AI面试问题信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int insertHotakeAiInterviewQuestionsInfo(HotakeAiInterviewQuestionsInfo hotakeAiInterviewQuestionsInfo)
    {
        fill(FillTypeEnum.INSERT.getCode(), hotakeAiInterviewQuestionsInfo);
        if(CollectionUtil.isNotEmpty(hotakeAiInterviewQuestionsInfo.getAiQuestionList())){
            hotakeAiInterviewQuestionsInfo.setQuestionJson(JSONUtil.toJsonStr(hotakeAiInterviewQuestionsInfo.getAiQuestionList()));
        }else{
            hotakeAiInterviewQuestionsInfo.setQuestionJson("");
        }
        return hotakeAiInterviewQuestionsInfoMapper.insertHotakeAiInterviewQuestionsInfo(hotakeAiInterviewQuestionsInfo);
    }

    /**
     * 修改AI面试问题信息
     * 
     * @param hotakeAiInterviewQuestionsInfo AI面试问题信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updateHotakeAiInterviewQuestionsInfo(HotakeAiInterviewQuestionsInfo hotakeAiInterviewQuestionsInfo)
    {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeAiInterviewQuestionsInfo);
        if(CollectionUtil.isNotEmpty(hotakeAiInterviewQuestionsInfo.getAiQuestionList())){
            hotakeAiInterviewQuestionsInfo.setQuestionJson(JSONUtil.toJsonStr(hotakeAiInterviewQuestionsInfo.getAiQuestionList()));
        }else{
            hotakeAiInterviewQuestionsInfo.setQuestionJson("");
        }
        return hotakeAiInterviewQuestionsInfoMapper.updateHotakeAiInterviewQuestionsInfo(hotakeAiInterviewQuestionsInfo);
    }

    /**
     * 批量删除AI面试问题信息
     * 
     * @param ids 需要删除的AI面试问题信息主键
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int deleteHotakeAiInterviewQuestionsInfoByIds(Long[] ids)
    {
        return hotakeAiInterviewQuestionsInfoMapper.deleteHotakeAiInterviewQuestionsInfoByIds(ids);
    }

    /**
     * 删除AI面试问题信息信息
     * 
     * @param id AI面试问题信息主键
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int deleteHotakeAiInterviewQuestionsInfoById(Long id)
    {
        return hotakeAiInterviewQuestionsInfoMapper.deleteHotakeAiInterviewQuestionsInfoById(id);
    }
    /**
     * 批量新增AI面试问题信息
     *
     * @param hotakeAiInterviewQuestionsInfoList AI面试问题信息列表
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int batchInsertHotakeAiInterviewQuestionsInfo(List<HotakeAiInterviewQuestionsInfo> hotakeAiInterviewQuestionsInfoList){
        return hotakeAiInterviewQuestionsInfoMapper.batchInsertHotakeAiInterviewQuestionsInfo(hotakeAiInterviewQuestionsInfoList);
    }
}
