package com.ruoyi.sim.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.ele6.catalyzer.ruoyi.custom.BaseService;
import cn.ele6.catalyzer.ruoyi.enhance.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sim.domain.vo.RealExamCollectionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sim.mapper.RealExamCollectionMapper;
import com.ruoyi.sim.domain.RealExamCollection;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 考试集合Service业务层处理
 *
 * @author tom
 * @date 2024-12-17
 */
@Service
public class RealExamCollectionService extends BaseService {
    @Autowired
    private RealExamCollectionMapper realExamCollectionMapper;

    /**
     * 查询考试集合
     *
     * @param examCollectionId 考试集合主键
     * @return 考试集合
     */
    public RealExamCollection selectRealExamCollectionByExamCollectionId(Long examCollectionId) {
        return realExamCollectionMapper.selectRealExamCollectionByExamCollectionId(examCollectionId);
    }

    /**
     * 查询考试集合列表
     *
     * @param realExamCollection 考试集合
     * @return 考试集合
     */
    public List<RealExamCollection> selectRealExamCollectionList(RealExamCollection realExamCollection) {
        return realExamCollectionMapper.selectRealExamCollectionList(realExamCollection);
    }

    /**
     * 新增考试集合
     *
     * @param realExamCollection 考试集合
     * @return 结果
     */
    public int insertRealExamCollection(RealExamCollection realExamCollection) {
        realExamCollection.setCreateTime(DateUtils.getNowDate());
        return realExamCollectionMapper.insertRealExamCollection(realExamCollection);
    }

    /**
     * 修改考试集合
     *
     * @param realExamCollection 考试集合
     * @return 结果
     */
    public int updateRealExamCollection(RealExamCollection realExamCollection) {
        realExamCollection.setUpdateTime(DateUtils.getNowDate());
        return realExamCollectionMapper.updateRealExamCollection(realExamCollection);
    }

    /**
     * 批量删除考试集合
     *
     * @param examCollectionIds 需要删除的考试集合主键
     * @return 结果
     */
    public int deleteRealExamCollectionByExamCollectionIds(Long[] examCollectionIds) {
        return realExamCollectionMapper.deleteRealExamCollectionByExamCollectionIds(examCollectionIds);
    }

    /**
     * 删除考试集合信息
     *
     * @param examCollectionId 考试集合主键
     * @return 结果
     */
    public int deleteRealExamCollectionByExamCollectionId(Long examCollectionId) {
        return realExamCollectionMapper.deleteRealExamCollectionByExamCollectionId(examCollectionId);
    }

    // -------------------------------- tom add  --------------------------------

    /**
     * [教师][分页]查询考试集合列表
     *
     * @param p
     * @return
     */
    public TableDataInfo listExamByTeacher(RealExamCollection p) {
        startPage();
        p.setExamCollectionType(RealExamCollection.Type.EXAM);
        List<RealExamCollectionVo> list = new ArrayList<>();
        realExamCollectionMapper.selectRealExamCollectionList(p).forEach((RealExamCollection s) -> {
            RealExamCollectionVo t = new RealExamCollectionVo();
            BeanUtils.copyProperties(s, t);
            list.add(t);
        });
        return getDataTable(list);
    }

    /**
     * [教师][分页]查询练习集合列表
     *
     * @param p
     * @return
     */
    public TableDataInfo listExerciseByTeacher(RealExamCollection p) {
        startPage();
        p.setExamCollectionType(RealExamCollection.Type.EXERCISE);
        List<RealExamCollectionVo> list = new ArrayList<>();
        realExamCollectionMapper.selectRealExamCollectionList(p).forEach((RealExamCollection s) -> {
            RealExamCollectionVo t = new RealExamCollectionVo();
            BeanUtils.copyProperties(s, t);
            list.add(t);
        });
        return getDataTable(list);
    }

    /**
     * [教师]获取考试集合详细信息
     */
    public AjaxResult getExamInfoByTeacher(Long id) {
        RealExamCollection f = realExamCollectionMapper.
                selectRealExamCollectionByExamCollectionId(id);
        return AjaxResult.success(f);
    }

    /**
     * [教师]获取练习集合详细信息
     */
    public AjaxResult getExerciseInfoByTeacher(Long id) {
        RealExamCollection f = realExamCollectionMapper.
                selectRealExamCollectionByExamCollectionId(id);
        return AjaxResult.success(f);
    }

    /**
     * [教师]新增考试集合
     *
     * @param vo
     * @return
     */
    public AjaxResult addExam(@RequestBody RealExamCollectionVo vo) {
        // check
        // 日期检查
        if (StringUtils.isBlank(vo.getExamCollectionName())) {

        }
        //
        RealExamCollection p = new RealExamCollection();
        BeanUtils.copyProperties(vo, p);
        p.setExamCollectionType(RealExamCollection.Type.EXAM);
        insertRealExamCollection(p);
        return AjaxResult.success();
    }

    /**
     * [教师]新增练习集合
     *
     * @param vo
     * @return
     */
    public AjaxResult addExercise(@RequestBody RealExamCollectionVo vo) {
        // check

        //
        RealExamCollection p = new RealExamCollection();
        BeanUtils.copyProperties(vo, p);
        p.setExamCollectionType(RealExamCollection.Type.EXERCISE);
        insertRealExamCollection(p);
        return AjaxResult.success();
    }

    /**
     * [学院]新增自主练习集合
     * todo:界面入口
     *
     * @param vo
     * @return
     */
    public AjaxResult addSelfExercise(@RequestBody RealExamCollectionVo vo) {
        // check

        // do.
        RealExamCollection p = new RealExamCollection();
        BeanUtils.copyProperties(vo, p);
        p.setExamCollectionType(RealExamCollection.Type.SELF_EXERCISE);
        insertRealExamCollection(p);
        return AjaxResult.success();
    }

    /**
     * [教师]修改考试集合
     *
     * @param vo
     * @return
     */
    public AjaxResult editExam(@RequestBody RealExamCollectionVo vo) {
        // check

        //
        RealExamCollection p = new RealExamCollection();
        BeanUtils.copyProperties(vo, p);
        p.setExamCollectionType(RealExamCollection.Type.EXAM);
        updateRealExamCollection(p);
        // todo:更新关联

        return AjaxResult.success();
    }

    /**
     * [教师]修改练习集合
     *
     * @param vo
     * @return
     */
    public AjaxResult editExercise(@RequestBody RealExamCollectionVo vo) {
        // check

        // do.
        RealExamCollection p = new RealExamCollection();
        BeanUtils.copyProperties(vo, p);
        p.setExamCollectionType(RealExamCollection.Type.EXERCISE);
        updateRealExamCollection(p);
        // todo:更新关联

        return AjaxResult.success();
    }

    /**
     * [教师]删除多个考试集合
     *
     * @param ids
     * @return
     */
    public AjaxResult deleteExams(Long[] ids) {
        // check

        // do.
        deleteRealExamCollectionByExamCollectionIds(ids);
        // todo:更新关联
        return AjaxResult.success();
    }

    /**
     * [教师]删除多个练习集合
     *
     * @param ids
     * @return
     */
    public AjaxResult deleteExercises(Long[] ids) {
        // check

        // do.
        deleteRealExamCollectionByExamCollectionIds(ids);
        // todo:更新关联
        return AjaxResult.success();
    }

    /**
     * [教师]打开考试集合
     *
     * @param id
     * @return
     */
    @Transactional
    public AjaxResult openExam(Long id) {
        // check
        int countOpened = 0;
        {
            RealExamCollection q = new RealExamCollection();
            q.setExamCollectionState(RealExamCollection.State.OPENED);
            l.info("q = {}", q);
            countOpened = selectRealExamCollectionList(q).size();
        }
        l.info("countOpened = {}", countOpened);
        if (countOpened > 0) {
            return AjaxResult.error("已经有打开的考试!");
        }
        // check datetime
        // do.
        RealExamCollection f = selectRealExamCollectionByExamCollectionId(id);
        f.setExamCollectionState(RealExamCollection.State.OPENED);
        updateRealExamCollection(f);
        return AjaxResult.success();
    }

    /**
     * [教师]关闭考试集合
     *
     * @param id
     * @return
     */
    public AjaxResult closeExam(Long id) {
        RealExamCollection f = selectRealExamCollectionByExamCollectionId(id);
        f.setExamCollectionState(RealExamCollection.State.CLOSED);
        updateRealExamCollection(f);
        return AjaxResult.success();
    }
}
