package com.qmth.exam.reserve.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.exam.reserve.bean.applytask.TimePeriodReq;
import com.qmth.exam.reserve.dao.TimePeriodDao;
import com.qmth.exam.reserve.entity.ApplyTaskEntity;
import com.qmth.exam.reserve.entity.TimePeriodEntity;
import com.qmth.exam.reserve.enums.EventType;
import com.qmth.exam.reserve.service.ApplyTaskService;
import com.qmth.exam.reserve.service.OperateLogService;
import com.qmth.exam.reserve.service.TimePeriodService;

@Service
public class TimePeriodServiceImpl extends ServiceImpl<TimePeriodDao, TimePeriodEntity> implements TimePeriodService {

    @Autowired
    private ApplyTaskService taskService;

    @Autowired
    private OperateLogService logService;

    @Override
    public void saveTimePeriod(Long userId, Long id, List<TimePeriodReq> timeList) {
        ApplyTaskEntity task = taskService.getById(id);
        if (task == null)
            throw new StatusException("预约任务不存在！");
        for (TimePeriodReq time : timeList) {
            TimePeriodEntity timePeriod = new TimePeriodEntity();
            BeanUtils.copyProperties(time, timePeriod);
            timePeriod.setApplyTaskId(id);
            this.saveOrUpdate(timePeriod);
        }
        logService.insertOperateLog(userId, EventType.SAVE_TIME_PERIOD, null);
    }

}
