package com.leromro.core.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leromro.common.core.domain.AjaxResult;
import com.leromro.common.core.domain.R;
import com.leromro.common.exception.ServiceException;
import com.leromro.common.utils.SecurityUtils;
import com.leromro.core.mapper.VolunteerWorkDateMapper;
import com.leromro.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.leromro.core.mapper.VolunteerWorkDateMapper;
import com.leromro.core.domain.VolunteerWorkDate;
import com.leromro.core.service.IVolunteerWorkDateService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 志愿者排班日期Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class VolunteerWorkDateServiceImpl extends ServiceImpl<VolunteerWorkDateMapper, VolunteerWorkDate> implements IVolunteerWorkDateService
{
    @Autowired
    private VolunteerWorkDateMapper volunteerWorkDateMapper;

    /**
     * 查询志愿者排班日期
     * 
     * @param volunteerWorkDateId 志愿者排班日期主键
     * @return 志愿者排班日期
     */
    @Override
    public VolunteerWorkDate selectVolunteerWorkDateByVolunteerWorkDateId(Long volunteerWorkDateId)
    {
        return volunteerWorkDateMapper.selectVolunteerWorkDateByVolunteerWorkDateId(volunteerWorkDateId);
    }

    /**
     * 查询志愿者排班日期列表
     * 
     * @param volunteerWorkDate 志愿者排班日期
     * @return 志愿者排班日期
     */
    @Override
    public List<VolunteerWorkDate> selectVolunteerWorkDateList(VolunteerWorkDate volunteerWorkDate)
    {
        return volunteerWorkDateMapper.selectVolunteerWorkDateList(volunteerWorkDate);
    }

    /**
     * 新增志愿者排班日期
     * 
     * @param volunteerWorkDate 志愿者排班日期
     * @return 结果
     */
    @Override
    public Boolean insertVolunteerWorkDate(VolunteerWorkDate volunteerWorkDate)
    {
        return this.save(volunteerWorkDate);
    }

    /**
     * 修改志愿者排班日期
     * 
     * @param list 志愿者排班日期集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateVolunteerWorkDate(List<VolunteerWorkDate> list)
    {
        // 获取当前用户信息
        Long userId = SecurityUtils.getUserId();
        // 查询出所有已经预约的日期
        List<VolunteerWorkDate> reservationList = this.list(new LambdaQueryWrapper<VolunteerWorkDate>()
                .eq(VolunteerWorkDate::getVolunteerId, userId)
                .eq(VolunteerWorkDate::getHasReservation, 1));
        // 如果存在则判断已经预约的日期是否被删除，如果被删除则提示不能修改
        if (CollectionUtil.isNotEmpty(reservationList)){
            List<LocalDate> dates = list.stream().map(VolunteerWorkDate::getWorkDate).collect(Collectors.toList());
            List<LocalDate> reservations = reservationList.stream().map(VolunteerWorkDate::getWorkDate).collect(Collectors.toList());
            for (LocalDate date : reservations) {
                if (!dates.contains(date)){
                    //把reservation转换为月日
                    return AjaxResult.error(date.format( DateTimeFormatter.ofPattern("M月d日"))+"已被预约，无法取消，请联系客户取消预约");
                }
            }
        }
        list.forEach(l->l.setVolunteerId(userId));
        this.remove(new LambdaQueryWrapper<VolunteerWorkDate>().eq(VolunteerWorkDate::getVolunteerId, userId));
        this.saveBatch(list);
        return AjaxResult.success();
    }

    /**
     * 批量删除志愿者排班日期
     * 
     * @param volunteerWorkDateIds 需要删除的志愿者排班日期主键
     * @return 结果
     */
    @Override
    public int deleteVolunteerWorkDateByVolunteerWorkDateIds(Long[] volunteerWorkDateIds)
    {
        return volunteerWorkDateMapper.deleteVolunteerWorkDateByVolunteerWorkDateIds(volunteerWorkDateIds);
    }

    /**
     * 删除志愿者排班日期信息
     * 
     * @param volunteerWorkDateId 志愿者排班日期主键
     * @return 结果
     */
    @Override
    public int deleteVolunteerWorkDateByVolunteerWorkDateId(Long volunteerWorkDateId)
    {
        return volunteerWorkDateMapper.deleteVolunteerWorkDateByVolunteerWorkDateId(volunteerWorkDateId);
    }

    /**
     * 查看志愿者排班日期及时间"
     */
    @Override
    public R<List<VolunteerWorkDate>> getWorkDateAndTime() {
        // 获取当前用户信息
        Long userId = SecurityUtils.getUserId();
        List<VolunteerWorkDate> list = this.list(new LambdaQueryWrapper<VolunteerWorkDate>()
                .eq(VolunteerWorkDate::getVolunteerId, userId));
        return R.ok(list);
    }
}
