package com.cyl.manager.staff.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyl.manager.staff.domain.StaffAnswerRecord;
import com.cyl.manager.staff.domain.TrainCheckAnswer;
import com.cyl.manager.staff.domain.dto.StaffAnswerAuditDTO;
import com.cyl.manager.staff.mapper.StaffAnswerRecordMapper;
import com.cyl.manager.staff.service.IStaffAnswerRecordService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 答题记录表 服务实现类
 * </p>
 *
 * @author 谷瑶
 * @since 2024-12-18
 */
@Service
public class StaffAnswerRecordServiceImpl extends ServiceImpl<StaffAnswerRecordMapper, StaffAnswerRecord> implements IStaffAnswerRecordService {

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean audit(StaffAnswerAuditDTO auditDTO) {
        StaffAnswerRecord record = this.getById(auditDTO.getId());
        if (record == null) {
            throw new ServiceException("未找到答题记录!");
        }
        List<TrainCheckAnswer> trainCheckAnswers = BeanUtil.copyToList(auditDTO.getList(), TrainCheckAnswer.class);
        record.setAnswer(trainCheckAnswers);
        record.setRemark(auditDTO.getRemark());
        record.setUpdateTime(LocalDateTime.now());
        if (auditDTO.getPassed()) {
            //通过
            record.setStatus(2);
        }else {
            //未通过
            record.setStatus(3);
        }


        return true;
    }
}
