package com.ruoyi.syzy.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.syzy.domain.BBusiHollandQuestionOptions;
import com.ruoyi.syzy.mapper.BBusiHollandQuestionsMapper;
import com.ruoyi.syzy.domain.BBusiHollandQuestions;
import com.ruoyi.syzy.service.IBBusiHollandQuestionsService;

/**
 * 霍兰德测评题目Service业务层处理
 * 
 * @author mingxue
 * @date 2021-08-15
 */
@Service
public class BBusiHollandQuestionsServiceImpl implements IBBusiHollandQuestionsService {
    @Autowired
    private BBusiHollandQuestionsMapper bBusiHollandQuestionsMapper;

    /**
     * 查询霍兰德测评题目
     * 
     * @param id 霍兰德测评题目ID
     * @return 霍兰德测评题目
     */
    @Override
    public BBusiHollandQuestions selectBBusiHollandQuestionsById(Long id) {
        return bBusiHollandQuestionsMapper.selectBBusiHollandQuestionsById(id);
    }

    /**
     * 根据步骤Id查询霍兰德测评题目
     * 
     * @param stepId 步骤Id
     * @return 霍兰德测评题目
     */
    @Override
    public List<BBusiHollandQuestions> selectQuestionsByStepId(Long stepId) {
        return bBusiHollandQuestionsMapper.selectQuestionsByStepId(stepId);
    }

    /**
     * 查询霍兰德测评题目列表
     * 
     * @param bBusiHollandQuestions 霍兰德测评题目
     * @return 霍兰德测评题目
     */
    @Override
    public List<BBusiHollandQuestions> selectBBusiHollandQuestionsList(BBusiHollandQuestions bBusiHollandQuestions) {
        return bBusiHollandQuestionsMapper.selectBBusiHollandQuestionsList(bBusiHollandQuestions);
    }

    /**
     * 新增霍兰德测评题目
     * 
     * @param bBusiHollandQuestions 霍兰德测评题目
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBBusiHollandQuestions(BBusiHollandQuestions bBusiHollandQuestions) {
        int rows = bBusiHollandQuestionsMapper.insertBBusiHollandQuestions(bBusiHollandQuestions);
        insertBBusiHollandQuestionOptions(bBusiHollandQuestions);
        return rows;
    }

    /**
     * 修改霍兰德测评题目
     * 
     * @param bBusiHollandQuestions 霍兰德测评题目
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBBusiHollandQuestions(BBusiHollandQuestions bBusiHollandQuestions) {
        bBusiHollandQuestionsMapper.deleteBBusiHollandQuestionOptionsByQuestionId(bBusiHollandQuestions.getId());
        insertBBusiHollandQuestionOptions(bBusiHollandQuestions);
        return bBusiHollandQuestionsMapper.updateBBusiHollandQuestions(bBusiHollandQuestions);
    }

    /**
     * 批量删除霍兰德测评题目
     * 
     * @param ids 需要删除的霍兰德测评题目ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBBusiHollandQuestionsByIds(Long[] ids) {
        bBusiHollandQuestionsMapper.deleteBBusiHollandQuestionOptionsByQuestionIds(ids);
        return bBusiHollandQuestionsMapper.deleteBBusiHollandQuestionsByIds(ids);
    }

    /**
     * 删除霍兰德测评题目信息
     * 
     * @param id 霍兰德测评题目ID
     * @return 结果
     */
    @Override
    public int deleteBBusiHollandQuestionsById(Long id) {
        bBusiHollandQuestionsMapper.deleteBBusiHollandQuestionOptionsByQuestionId(id);
        return bBusiHollandQuestionsMapper.deleteBBusiHollandQuestionsById(id);
    }

    /**
     * 新增霍兰德测评题目选项信息
     * 
     * @param bBusiHollandQuestions 霍兰德测评题目对象
     */
    public void insertBBusiHollandQuestionOptions(BBusiHollandQuestions bBusiHollandQuestions) {
        List<BBusiHollandQuestionOptions> bBusiHollandQuestionOptionsList = bBusiHollandQuestions.getOptions();
        Long id = bBusiHollandQuestions.getId();
        if (StringUtils.isNotNull(bBusiHollandQuestionOptionsList)) {
            List<BBusiHollandQuestionOptions> list = new ArrayList<BBusiHollandQuestionOptions>();
            for (BBusiHollandQuestionOptions bBusiHollandQuestionOptions : bBusiHollandQuestionOptionsList) {
                bBusiHollandQuestionOptions.setQuestionId(id);
                list.add(bBusiHollandQuestionOptions);
            }
            if (list.size() > 0) {
                bBusiHollandQuestionsMapper.batchBBusiHollandQuestionOptions(list);
            }
        }
    }
}
