package com.xinelu.manage.service.schedule.impl;

import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.manage.domain.schedule.Schedule;
import com.xinelu.manage.mapper.schedule.ScheduleMapper;
import com.xinelu.manage.service.schedule.IScheduleService;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author haown
* @description 针对表【schedule(排班班次表)】的数据库操作Service实现
* @createDate 2023-09-21 10:12:22
*/
@Service
public class ScheduleServiceImpl implements IScheduleService {

	@Resource
	private ScheduleMapper scheduleMapper;

	@Override
	public int save(Schedule schedule) {
		try {
			verify(schedule);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		schedule.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
		schedule.setCreateTime(new Date());
		return scheduleMapper.insertSelective(schedule);
	}

	@Override public int update(Schedule schedule) {
		try {
			verify(schedule);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		schedule.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
		schedule.setUpdateTime(new Date());
		return scheduleMapper.updateByPrimaryKeySelective(schedule);
	}

	@Override public int delete(Long id) {
		Schedule schedule = new Schedule();
		schedule.setId(id);
		schedule.setDelFlag("1");
		schedule.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
		schedule.setUpdateTime(new Date());
		return scheduleMapper.updateByPrimaryKeySelective(schedule);
	}

	@Override public List<Schedule> getList(Schedule schedule) {
		return scheduleMapper.getList(schedule);
	}

	@Override public Schedule getById(Long id) {
		return scheduleMapper.selectByPrimaryKey(id);
	}

	private void verify(Schedule schedule) throws Exception {
		if (schedule.getForenoonStartTime() == null && schedule.getForenoonEndTime() == null && schedule.getAfternoonStartTime() == null && schedule.getAfternoonEndTime() == null) {
			throw new Exception("请选择开始时间和结束时间！");
		}
		if (schedule.getForenoonStartTime() == null) {
			if (schedule.getForenoonEndTime() != null) {
				throw new Exception("请选择上午开始时间！");
			}
		} else {
			if (schedule.getForenoonEndTime() == null) {
				throw new Exception("请选择上午结束时间！");
			} else {
				// 判断时间的先后顺序
				if (schedule.getForenoonEndTime().isBefore(schedule.getForenoonStartTime())) {
					throw new Exception("上午开始时间应在上午结束时间之前！");
				}
			}
		}

		if (schedule.getAfternoonStartTime() == null) {
			if (schedule.getAfternoonEndTime() != null) {
				throw new Exception("请选择下午开始时间！");
			}
		} else {
			if (schedule.getAfternoonEndTime() == null) {
				throw new Exception("请选择下午结束时间！");
			} else {
				// 判断时间的先后顺序
				if (schedule.getAfternoonEndTime().isBefore(schedule.getAfternoonStartTime())) {
					throw new Exception("下午开始时间应在下午结束时间之前！");
				}
			}
		}
	}
}




