package com.yanzhu.manage.service.impl;

import java.util.List;

import com.yanzhu.common.core.constant.CacheConstants;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.core.utils.StringUtils;
import com.yanzhu.common.redis.service.RedisService;
import com.yanzhu.common.security.utils.SecurityUtils;
import com.yanzhu.manage.domain.ProProjectInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.yanzhu.manage.domain.BusExamQuestionResult;
import com.yanzhu.manage.mapper.BusExamQuestionMapper;
import com.yanzhu.manage.domain.BusExamQuestion;
import com.yanzhu.manage.service.IBusExamQuestionService;

/**
 * 基础题库Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2024-08-25
 */
@Service
public class BusExamQuestionServiceImpl implements IBusExamQuestionService 
{
    @Autowired
    private RedisService redisService;

    @Autowired
    private BusExamQuestionMapper busExamQuestionMapper;

    /**
     * 查询基础题库
     * 
     * @param id 基础题库主键
     * @return 基础题库
     */
    @Override
    public BusExamQuestion selectBusExamQuestionById(Long id)
    {
        return busExamQuestionMapper.selectBusExamQuestionById(id);
    }

    /**
     * 查询基础题库列表
     * 
     * @param busExamQuestion 基础题库
     * @return 基础题库
     */
    @Override
    public List<BusExamQuestion> selectBusExamQuestionList(BusExamQuestion busExamQuestion)
    {
        busExamQuestion.setActiveComId(SecurityUtils.getLoginUser().getProjectDeptId());
        busExamQuestion.setActiveProjectId(SecurityUtils.getLoginUser().getProjectId());
        return busExamQuestionMapper.selectBusExamQuestionList(busExamQuestion);
    }

    /**
     * 新增基础题库
     * 
     * @param busExamQuestion 基础题库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBusExamQuestion(BusExamQuestion busExamQuestion)
    {
        /**ProProjectInfo info = redisService.getCacheObject(CacheConstants.PRO_PROJECT+busExamQuestion.getProjectId());
        busExamQuestion.setComId(info.getComId());*/
        busExamQuestion.setCreateBy(SecurityUtils.getUsername());
        busExamQuestion.setCreateTime(DateUtils.getNowDate());
        busExamQuestion.setUpdateTime(DateUtils.getNowDate());
        int rows = busExamQuestionMapper.insertBusExamQuestion(busExamQuestion);
        insertBusExamQuestionResult(busExamQuestion);
        return rows;
    }

    /**
     * 修改基础题库
     * 
     * @param busExamQuestion 基础题库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBusExamQuestion(BusExamQuestion busExamQuestion)
    {
        busExamQuestion.setUpdateTime(DateUtils.getNowDate());
        busExamQuestionMapper.deleteBusExamQuestionResultByQuestionId(busExamQuestion.getId());
        insertBusExamQuestionResult(busExamQuestion);
        return busExamQuestionMapper.updateBusExamQuestion(busExamQuestion);
    }

    /**
     * 批量删除基础题库
     * 
     * @param ids 需要删除的基础题库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusExamQuestionByIds(Long[] ids)
    {
        busExamQuestionMapper.deleteBusExamQuestionResultByQuestionIds(ids);
        return busExamQuestionMapper.deleteBusExamQuestionByIds(ids);
    }

    /**
     * 删除基础题库信息
     * 
     * @param id 基础题库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusExamQuestionById(Long id)
    {
        busExamQuestionMapper.deleteBusExamQuestionResultByQuestionId(id);
        return busExamQuestionMapper.deleteBusExamQuestionById(id);
    }

    /**
     * 新增题库问题信息
     * 
     * @param busExamQuestion 基础题库对象
     */
    public void insertBusExamQuestionResult(BusExamQuestion busExamQuestion)
    {
        List<BusExamQuestionResult> busExamQuestionResultList = busExamQuestion.getBusExamQuestionResultList();
        Long id = busExamQuestion.getId();
        if (StringUtils.isNotNull(busExamQuestionResultList))
        {
            List<BusExamQuestionResult> list = new ArrayList<BusExamQuestionResult>();
            for (BusExamQuestionResult busExamQuestionResult : busExamQuestionResultList)
            {
                busExamQuestionResult.setQuestionId(id);
                list.add(busExamQuestionResult);
            }
            if (list.size() > 0)
            {
                busExamQuestionMapper.batchBusExamQuestionResult(list);
            }
        }
    }
}
