package com.fs.task;

import com.fs.company.service.ICompanyService;
import com.fs.company.vo.RedPacketMoneyVO;
import com.fs.course.mapper.FsCourseRedPacketLogMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@AllArgsConstructor
@Component("companyTask")
public class FsCompanyTask {

    private FsCourseRedPacketLogMapper fsCourseRedPacketLogMapper;
    private ICompanyService companyService;

    public void refreshCompanyMoney() {
        LocalDateTime now = LocalDateTime.now();
        // 获取上一个小时的开始时间
        LocalDateTime startTime = now.minusHours(1)
                .withMinute(0)
                .withSecond(0);
        // 获取上一个小时的结束时间
        LocalDateTime endTime = startTime
                .withMinute(59)
                .withSecond(59);
        List<RedPacketMoneyVO> redPacketMoneyVOS = fsCourseRedPacketLogMapper.selectFsCourseRedPacketLogHourseByCompany(startTime, endTime);
        for (RedPacketMoneyVO redPacketMoneyVO : redPacketMoneyVOS) {
            companyService.subtractCompanyMoneyHourse(redPacketMoneyVO.getMoney(), redPacketMoneyVO.getCompanyId(), startTime.toLocalTime(), endTime.toLocalTime());
        }
    }
}