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.common.utils.SecurityUtils;
import com.vetti.hotake.domain.HotakeInitialScreeningQuestionsInfo;
import com.vetti.hotake.domain.dto.AnswerOptionsDto;
import com.vetti.hotake.domain.vo.HotakeInitialScreeningQuestionsInfoVo;
import com.vetti.hotake.mapper.HotakeInitialScreeningQuestionsInfoMapper;
import com.vetti.hotake.service.IHotakeInitialScreeningQuestionsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 初步筛选问题信息Service业务层处理
 * 
 * @author wangxiangshun
 * @date 2025-12-14
 */
@SuppressWarnings("all")
@Service
public class HotakeInitialScreeningQuestionsInfoServiceImpl extends BaseServiceImpl implements IHotakeInitialScreeningQuestionsInfoService
{
    @Autowired
    private HotakeInitialScreeningQuestionsInfoMapper hotakeInitialScreeningQuestionsInfoMapper;

    /**
     * 查询初步筛选问题信息
     * 
     * @param id 初步筛选问题信息主键
     * @return 初步筛选问题信息
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeInitialScreeningQuestionsInfo selectHotakeInitialScreeningQuestionsInfoById(Long id)
    {
        HotakeInitialScreeningQuestionsInfo questionsInfo =  hotakeInitialScreeningQuestionsInfoMapper.selectHotakeInitialScreeningQuestionsInfoById(id);
        if(questionsInfo != null && StrUtil.isNotEmpty(questionsInfo.getAnswerOptions())){
            List<AnswerOptionsDto> answerOptionsDtos = JSONUtil.toList(questionsInfo.getAnswerOptions(), AnswerOptionsDto.class);
            questionsInfo.setAnswerOptionsList(answerOptionsDtos);
        }
        return questionsInfo;
    }

    /**
     * 查询初步筛选问题信息列表
     * 
     * @param hotakeInitialScreeningQuestionsInfo 初步筛选问题信息
     * @return 初步筛选问题信息
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeInitialScreeningQuestionsInfo> selectHotakeInitialScreeningQuestionsInfoList(HotakeInitialScreeningQuestionsInfo hotakeInitialScreeningQuestionsInfo)
    {
        return hotakeInitialScreeningQuestionsInfoMapper.selectHotakeInitialScreeningQuestionsInfoList(hotakeInitialScreeningQuestionsInfo);
    }

    /**
     * 新增初步筛选问题信息
     * 
     * @param hotakeInitialScreeningQuestionsInfo 初步筛选问题信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int insertHotakeInitialScreeningQuestionsInfo(HotakeInitialScreeningQuestionsInfo hotakeInitialScreeningQuestionsInfo)
    {
        hotakeInitialScreeningQuestionsInfo.setRecruiterId(SecurityUtils.getUserId());
        fill(FillTypeEnum.INSERT.getCode(), hotakeInitialScreeningQuestionsInfo);
        if(CollectionUtil.isNotEmpty(hotakeInitialScreeningQuestionsInfo.getAnswerOptionsList())){
            hotakeInitialScreeningQuestionsInfo.setAnswerOptions(JSONUtil.toJsonStr(hotakeInitialScreeningQuestionsInfo.getAnswerOptionsList()));
        }else {
            hotakeInitialScreeningQuestionsInfo.setAnswerOptions("");
        }
        return hotakeInitialScreeningQuestionsInfoMapper.insertHotakeInitialScreeningQuestionsInfo(hotakeInitialScreeningQuestionsInfo);
    }

    /**
     * 修改初步筛选问题信息
     * 
     * @param hotakeInitialScreeningQuestionsInfo 初步筛选问题信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updateHotakeInitialScreeningQuestionsInfo(HotakeInitialScreeningQuestionsInfo hotakeInitialScreeningQuestionsInfo)
    {
        hotakeInitialScreeningQuestionsInfo.setRecruiterId(SecurityUtils.getUserId());
        fill(FillTypeEnum.UPDATE.getCode(), hotakeInitialScreeningQuestionsInfo);
        if(CollectionUtil.isNotEmpty(hotakeInitialScreeningQuestionsInfo.getAnswerOptionsList())){
            hotakeInitialScreeningQuestionsInfo.setAnswerOptions(JSONUtil.toJsonStr(hotakeInitialScreeningQuestionsInfo.getAnswerOptionsList()));
        }else {
            hotakeInitialScreeningQuestionsInfo.setAnswerOptions("");
        }
        return hotakeInitialScreeningQuestionsInfoMapper.updateHotakeInitialScreeningQuestionsInfo(hotakeInitialScreeningQuestionsInfo);
    }

    /**
     * 批量删除初步筛选问题信息
     * 
     * @param ids 需要删除的初步筛选问题信息主键
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int deleteHotakeInitialScreeningQuestionsInfoByIds(Long[] ids)
    {
        return hotakeInitialScreeningQuestionsInfoMapper.deleteHotakeInitialScreeningQuestionsInfoByIds(ids);
    }

    /**
     * 删除初步筛选问题信息信息
     * 
     * @param id 初步筛选问题信息主键
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int deleteHotakeInitialScreeningQuestionsInfoById(Long id)
    {
        return hotakeInitialScreeningQuestionsInfoMapper.deleteHotakeInitialScreeningQuestionsInfoById(id);
    }
    /**
     * 批量新增初步筛选问题信息
     *
     * @param questionsInfoVo 初步筛选问题信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void batchInsertHotakeInitialScreeningQuestionsInfo(HotakeInitialScreeningQuestionsInfoVo questionsInfoVo){
        //先删除之前的初步筛选问题数据
//        hotakeInitialScreeningQuestionsInfoMapper.deleteHotakeInitialScreeningQuestionsInfoByRoleId(questionsInfoVo.getRoleId());
        if(CollectionUtil.isNotEmpty(questionsInfoVo.getQuestionsInfoList())){
            for(HotakeInitialScreeningQuestionsInfo questionsInfo : questionsInfoVo.getQuestionsInfoList()){
                fill(FillTypeEnum.INSERT.getCode(), questionsInfo);
                questionsInfo.setRecruiterId(SecurityUtils.getUserId());
                if(CollectionUtil.isNotEmpty(questionsInfo.getAnswerOptionsList())){
                    questionsInfo.setAnswerOptions(JSONUtil.toJsonStr(questionsInfo.getAnswerOptionsList()));
                }else {
                    questionsInfo.setAnswerOptions("");
                }
                questionsInfo.setRoleId(questionsInfoVo.getRoleId());
            }
            hotakeInitialScreeningQuestionsInfoMapper.batchInsertHotakeInitialScreeningQuestionsInfo(questionsInfoVo.getQuestionsInfoList());
        }
    }
}
