package com.yanzhu.manage.service.impl;

import java.util.List;

import com.yanzhu.common.core.context.SecurityContextHolder;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.core.utils.StringUtils;
import com.yanzhu.common.security.utils.SecurityUtils;
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.BusExamInfoQues;
import com.yanzhu.manage.mapper.BusExamInfoMapper;
import com.yanzhu.manage.domain.BusExamInfo;
import com.yanzhu.manage.service.IBusExamInfoService;

/**
 * 基础试卷Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2024-08-25
 */
@Service
public class BusExamInfoServiceImpl implements IBusExamInfoService 
{
    @Autowired
    private BusExamInfoMapper busExamInfoMapper;

    /**
     * 查询基础试卷
     * 
     * @param id 基础试卷主键
     * @return 基础试卷
     */
    @Override
    public BusExamInfo selectBusExamInfoById(Long id)
    {
        return busExamInfoMapper.selectBusExamInfoById(id);
    }

    /**
     * 查询基础试卷列表
     * 
     * @param busExamInfo 基础试卷
     * @return 基础试卷
     */
    @Override
    public List<BusExamInfo> selectBusExamInfoList(BusExamInfo busExamInfo)
    {
        busExamInfo.setActiveComId(SecurityUtils.getLoginUser().getProjectDeptId());
        busExamInfo.setActiveProjectId(SecurityUtils.getLoginUser().getProjectId());
        return busExamInfoMapper.selectBusExamInfoList(busExamInfo);
    }

    /**
     * 新增基础试卷
     * 
     * @param busExamInfo 基础试卷
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBusExamInfo(BusExamInfo busExamInfo)
    {
        busExamInfo.setCreateTime(DateUtils.getNowDate());
        busExamInfo.setCreateBy(SecurityContextHolder.getUserName());
        int rows = busExamInfoMapper.insertBusExamInfo(busExamInfo);
        insertBusExamInfoQues(busExamInfo);
        return rows;
    }

    /**
     * 修改基础试卷
     * 
     * @param busExamInfo 基础试卷
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBusExamInfo(BusExamInfo busExamInfo)
    {
        busExamInfo.setUpdateTime(DateUtils.getNowDate());
        busExamInfo.setUpdateBy(SecurityContextHolder.getUserName());
        busExamInfoMapper.deleteBusExamInfoQuesByExamId(busExamInfo.getId());
        insertBusExamInfoQues(busExamInfo);
        return busExamInfoMapper.updateBusExamInfo(busExamInfo);
    }

    /**
     * 批量删除基础试卷
     * 
     * @param ids 需要删除的基础试卷主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusExamInfoByIds(Long[] ids)
    {
        busExamInfoMapper.deleteBusExamInfoQuesByExamIds(ids);
        return busExamInfoMapper.deleteBusExamInfoByIds(ids);
    }

    /**
     * 删除基础试卷信息
     * 
     * @param id 基础试卷主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusExamInfoById(Long id)
    {
        busExamInfoMapper.deleteBusExamInfoQuesByExamId(id);
        return busExamInfoMapper.deleteBusExamInfoById(id);
    }

    /**
     * 新增试卷问题信息
     * 
     * @param busExamInfo 基础试卷对象
     */
    public void insertBusExamInfoQues(BusExamInfo busExamInfo)
    {
        List<BusExamInfoQues> busExamInfoQuesList = busExamInfo.getBusExamInfoQuesList();
        Long id = busExamInfo.getId();
        if (StringUtils.isNotNull(busExamInfoQuesList))
        {
            List<BusExamInfoQues> list = new ArrayList<BusExamInfoQues>();
            for (BusExamInfoQues busExamInfoQues : busExamInfoQuesList)
            {
                busExamInfoQues.setExamId(id);
                list.add(busExamInfoQues);
            }
            if (list.size() > 0)
            {
                busExamInfoMapper.batchBusExamInfoQues(list);
            }
        }
    }
}
