package org.jeecg.modules.yjComprehensiveLog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.newWarning.entity.WellDrilling;
import org.jeecg.modules.newWarning.service.*;
import org.jeecg.modules.yjComprehensiveLog.entity.YjComprehensiveLog;
import org.jeecg.modules.yjComprehensiveLog.mapper.YjComprehensiveLogMapper;
import org.jeecg.modules.yjComprehensiveLog.model.YjJhComprehensiveCountModel;
import org.jeecg.modules.yjComprehensiveLog.service.YjComprehensiveLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * 综合异常日志信息;(yj_comprehensive_log)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2025-4-9
 */
@Service
@Slf4j
public class YjComprehensiveLogServiceImpl extends ServiceImpl<YjComprehensiveLogMapper, YjComprehensiveLog> implements YjComprehensiveLogService {

    @Resource
    private HookLoadWarnLogService hookLoadWarnLogService;

    @Resource
    private PumpingPressureWarnLogService pumpingPressureWarnLogService;

    @Resource
    private TorqueWarnLogService torqueWarnLogService;

    @Resource
    private TotalPoolVolumeWarnLogService totalPoolVolumeWarnLogService;

    @Resource
    private WellDrillingService wellDrillingService;



    /**
     * 根据时间获取单独井的各类预警数据
     * @param startDate
     * @param endDate
     * @param jh
     */
    @Override
    public YjJhComprehensiveCountModel getWarnCountByJhAndDate(String startDate, String endDate, String jh){
        YjJhComprehensiveCountModel  res =new YjJhComprehensiveCountModel();
        Integer gzCount = hookLoadWarnLogService.getWarnCountByJhAndDate(jh,startDate, endDate);

        Integer byCount =  pumpingPressureWarnLogService.getWarnCountByJhAndDate(jh,startDate,endDate);

        Integer njCount = torqueWarnLogService.getWarnCountByJhAndDate(jh,startDate,endDate);

        Integer jlCount = totalPoolVolumeWarnLogService.getWarnCountByJhAndDate(jh, startDate, endDate, "井漏");

        Integer ylCount = totalPoolVolumeWarnLogService.getWarnCountByJhAndDate(jh,startDate,endDate,"溢流");
        res.setJh(jh);
        res.setGzCount(BigDecimal.valueOf(gzCount));
        res.setByCount(BigDecimal.valueOf(byCount));
        res.setNjCount(BigDecimal.valueOf(njCount));
        res.setJlCount(BigDecimal.valueOf(jlCount));
        res.setYlCount(BigDecimal.valueOf(ylCount));
        return res;
    }

    /**
     * 根据不同的时间类型查询预警日志数据
     * @param endDate
     * @param jh
     * @param dateType
     * @param warnType
     * @return
     */
    @Override
    public List<YjComprehensiveLog> getLogByJh(String endDate, String jh, String dateType, String warnType){
        QueryWrapper<YjComprehensiveLog> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(YjComprehensiveLog::getJh, jh)
                .eq(YjComprehensiveLog::getWarnType, warnType).orderByAsc(YjComprehensiveLog::getWarnTime);
        // 日期范围查询逻辑
        LocalDate endDateLocal = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String startStr ="";
        String endStr ="";
        switch (dateType) {
            case "1": // 当天数据
                String dateStr = endDateLocal.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                startStr=dateStr;
                endStr=dateStr;
                break;

            case "2": { // 本周数据
                LocalDate monday = endDateLocal.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                LocalDate sunday = endDateLocal.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                startStr = monday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                 endStr = sunday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                break;
            }

            case "3": { // 当月数据
                LocalDate firstDay = endDateLocal.with(TemporalAdjusters.firstDayOfMonth());
                LocalDate lastDay = endDateLocal.with(TemporalAdjusters.lastDayOfMonth());
                 startStr = firstDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                 endStr = lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                break;
            }
            default:
                throw new IllegalArgumentException("无效的dateType参数");
        }

        log.info("查询井号:{},预警类别:{},开始时间：{}，结束时间：{}",jh,warnType,startStr,endStr);
        wrapper.apply("TO_DATE(SUBSTR(warn_time, 1, 19), 'yyyy-MM-dd HH24:mi:ss') >= to_date({0},'yyyy-mm-dd hh24:mi:ss')",startStr+" 00:00:00").
                apply("TO_DATE(SUBSTR(warn_time, 1, 19), 'yyyy-MM-dd HH24:mi:ss') <= to_date({0},'yyyy-mm-dd hh24:mi:ss')",endStr+" 23:59:59");
        return this.list(wrapper);
    }


}
