package com.ruoyi.hywz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.hywz.entity.AdsPrjHywzqlcPrjFunInfoDf;
import com.ruoyi.hywz.mapper.AdsPrjHywzqlcPrjFunInfoDfMapper;
import com.ruoyi.hywz.service.IAdsPrjHywzqlcPrjFunInfoDfService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 应用表_项目域_物资全流程_项目财务基本信息_按日全量更新 服务实现类
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-04-17 09:40:18
 */
@Service
public class AdsPrjHywzqlcPrjFunInfoDfServiceImpl extends ServiceImpl<AdsPrjHywzqlcPrjFunInfoDfMapper,AdsPrjHywzqlcPrjFunInfoDf> implements IAdsPrjHywzqlcPrjFunInfoDfService {

    @Override
    public AdsPrjHywzqlcPrjFunInfoDf getByWbsNo(String wbsNo) {
        QueryWrapper<AdsPrjHywzqlcPrjFunInfoDf> queryWrapper = Wrappers.query();
        queryWrapper.eq("wbs_no", wbsNo);

        // 13:10前查昨天数据，之后查今天数据
        if (isBefore(13, 10)) {
            // 昨天
            queryWrapper.eq("DATE_FORMAT(etl_tm,'%Y-%m-%d')", this.getYesterdayDate());
        } else {
            // 今天
            queryWrapper.eq("DATE_FORMAT(etl_tm,'%Y-%m-%d')", this.getTodayDate());
        }
        List<AdsPrjHywzqlcPrjFunInfoDf> list = baseMapper.selectList(queryWrapper);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    private boolean isBefore(Integer h, Integer m) {
        LocalTime now = LocalTime.now();
        LocalTime of = LocalTime.of(h, m);
        return now.isBefore(of);
    }

    private String getTodayDate() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return today.format(formatter);
    }

    private String getYesterdayDate() {
        LocalDate yesterday = LocalDate.now().plusDays(-1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return yesterday.format(formatter);
    }

}
