package com.storlead.sales.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.storlead.sales.pojo.entity.customer.StorleadCalendarEntity;
import com.storlead.sales.mapper.customer.StorleadCalendarMapper;
import com.storlead.sales.service.customer.StorleadCalendarService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 日历表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2025-03-21
 */
@Service
public class StorleadCalendarServiceImpl extends ServiceImpl<StorleadCalendarMapper, StorleadCalendarEntity> implements StorleadCalendarService {

    @Override
    public Date dateOff(Date begainTransactionTimeOld, Integer days, List<StorleadCalendarEntity> calendarList) {
        // 1. 过滤出工作日且日期小于等于begainTransactionTimeOld的记录
        List<StorleadCalendarEntity> filteredList = calendarList.stream()
                .filter(calendar -> calendar.getWorkday() == 1)
                .filter(calendar -> !calendar.getDateStd().after(begainTransactionTimeOld))
                .sorted(Comparator.comparing(StorleadCalendarEntity::getDateStd).reversed())
                .collect(Collectors.toList());

        // 2. 检查是否有足够的数据
        if (filteredList.size() <= days) {
            // 可根据业务需求返回null或最早日期
            return filteredList.isEmpty() ? null : filteredList.get(filteredList.size() - 1).getDateStd();
        }

        // 3. 获取指定偏移量的日期
        return filteredList.get(days).getDateStd();
    }

//    @Override
    public Date dateOffOld(Date begainTransactionTimeOld,Integer days) {
        LambdaQueryWrapper<StorleadCalendarEntity> begainTransactionTimeWrapper =  new LambdaQueryWrapper();

        begainTransactionTimeWrapper.le(StorleadCalendarEntity::getDateStd, begainTransactionTimeOld);

//        begainTransactionTimeWrapper.ge(StorleadCalendarEntity::getDateStd, begainTransactionTimeOld);
        begainTransactionTimeWrapper.eq(StorleadCalendarEntity::getWorkday, 1);
        begainTransactionTimeWrapper.orderByDesc(StorleadCalendarEntity::getDateStd);

        begainTransactionTimeWrapper.last("LIMIT 1 OFFSET "+days);
        StorleadCalendarEntity storleadCalendarB = getOne(begainTransactionTimeWrapper);
        Date begainTransactionTime = storleadCalendarB.getDateStd();
        return begainTransactionTime;
    }

    @Override
    public boolean isWorkDay(LocalDate date) {
//        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        String dateStr = date.format(fmt);
        //有先查询公司设置，是否是工作日

        //查询法定节假日，是否是工作日
        LambdaQueryWrapper<StorleadCalendarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StorleadCalendarEntity::getDateStd,date);
        StorleadCalendarEntity calendarEntity = getOne(wrapper);
        if(Objects.isNull(calendarEntity)){
            return false;
        }
        if(calendarEntity.getWorkday() == 1){
            return true;
        }else {
            return false;
        }
    }

}
