package cn.iocoder.yudao.module.member.service.holiday;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.iocoder.yudao.module.member.controller.admin.holiday.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.holiday.HolidayDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.holiday.HolidayMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 假期 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class HolidayServiceImpl implements HolidayService {

    @Resource
    private HolidayMapper holidayMapper;

    @Override
    public Long createHoliday(HolidaySaveReqVO createReqVO) {
        // 插入
        HolidayDO holiday = BeanUtils.toBean(createReqVO, HolidayDO.class);
        holidayMapper.insert(holiday);
        // 返回
        return holiday.getId();
    }

    @Override
    public void updateHoliday(HolidaySaveReqVO updateReqVO) {
        // 校验存在
        validateHolidayExists(updateReqVO.getId());
        // 更新
        HolidayDO updateObj = BeanUtils.toBean(updateReqVO, HolidayDO.class);
        holidayMapper.updateById(updateObj);
    }

    @Override
    public void deleteHoliday(Long id) {
        // 校验存在
        validateHolidayExists(id);
        // 删除
        holidayMapper.deleteById(id);
    }

    private void validateHolidayExists(Long id) {
        if (holidayMapper.selectById(id) == null) {
            throw exception(HOLIDAY_NOT_EXISTS);
        }
    }

    @Override
    public HolidayDO getHoliday(Long id) {
        return holidayMapper.selectById(id);
    }

    @Override
    public PageResult<HolidayDO> getHolidayPage(HolidayPageReqVO pageReqVO) {
        return holidayMapper.selectPage(pageReqVO);
    }

    @Override
    public void getNextYearData(HolidayVO vo) {
        if (vo.getCode() == 0) {
            Map<String, HolidayDataVO> holiday = vo.getHoliday();
            Collection<HolidayDataVO> values = holiday.values();
            ArrayList<HolidayDO> add = new ArrayList<>();
            for (HolidayDataVO value : values) {
                HolidayDO holidayDO = new HolidayDO();
                holidayDO.setDate(value.getDate());
                holidayDO.setName(value.getName());
                holidayDO.setHoliday(value.getHoliday());
                add.add(holidayDO);
            }
            holidayMapper.insertBatch(add);
        }
    }

    @Override
    public Boolean checkHoliday() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
        // 判断当天是否周末
        boolean isHoliday =  dayOfWeek== DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;

        //获取假期
        // 创建 DateTimeFormatter 对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 将 LocalDate 对象转换为字符串
        String formattedDate = currentDate.format(formatter);

        HolidayDO holidayDO = holidayMapper.selectOne(new QueryWrapper<HolidayDO>().eq("date", formattedDate).last("LIMIT 1"));
        if (holidayDO != null) {
            isHoliday = holidayDO.getHoliday();
        }
        return isHoliday;
    }

    @Override
    public Boolean checkHoliday(String time) {
        // 创建 DateTimeFormatter 对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.parse(time, formatter);
        DayOfWeek dayOfWeek =currentDate.getDayOfWeek();
        // 判断当天是否周末
        boolean isHoliday =  dayOfWeek== DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;
        //获取假期
        HolidayDO holidayDO = holidayMapper.selectOne(new QueryWrapper<HolidayDO>().eq("date", time).last("LIMIT 1"));
        if (holidayDO != null) {
            isHoliday = holidayDO.getHoliday();
        }
        return isHoliday;
    }
}