package cn.iocoder.yudao.module.pms.dal.mysql.iotrhdailyreport;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pms.controller.admin.iotrhdailyreport.vo.IotRhDailyReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.vo.IotMaintainPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.stat.vo.AllOrderResp;
import cn.iocoder.yudao.module.pms.controller.admin.stat.vo.YearTotalGas;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotrhdailyreport.IotRhDailyReportDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 瑞恒日报 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotRhDailyReportMapper extends BaseMapperX<IotRhDailyReportDO> {

    List<YearTotalGas> selectYearTotalGas(IotRhDailyReportPageReqVO reqVO);

    /**
     * 查询 瑞恒日报列表
     * @param page
     * @param reqVO
     * @param projectIds
     * @return
     */
    IPage<IotRhDailyReportDO> rhDailyReports(IPage<IotRhDailyReportDO> page, @Param("reqVO") IotRhDailyReportPageReqVO reqVO,
                                             @Param("taskIds") Collection<Long> taskIds,
                                             @Param("projectIds") Collection<Long> projectIds, @Param("deptIds") Collection<Long> deptIds);

    /**
     * 根据 井号 查询 瑞恒日报 按照队伍分组 小计
     * @param page
     * @param reqVO
     * @param projectIds
     * @return
     */
    IPage<IotRhDailyReportDO> rhWellNameDailyReports(IPage<IotRhDailyReportDO> page, @Param("reqVO") IotRhDailyReportPageReqVO reqVO,
                                             @Param("taskIds") Collection<Long> taskIds,
                                             @Param("projectIds") Collection<Long> projectIds, @Param("deptIds") Collection<Long> deptIds);

    /**
     * 根据 井号 查询 瑞恒日报 按照队伍分组 小计
     * @param page
     * @param reqVO
     * @param projectIds
     * @return
     */
    IPage<IotRhDailyReportDO> rhDeptDailyReports(IPage<IotRhDailyReportDO> page, @Param("reqVO") IotRhDailyReportPageReqVO reqVO,
                                                     @Param("taskIds") Collection<Long> taskIds,
                                                     @Param("projectIds") Collection<Long> projectIds, @Param("deptIds") Collection<Long> deptIds);

    /**
     * 根据 井号 查询 瑞恒日报 按照队伍分组 小计 查询小组内最后1条记录id
     * @param page
     * @param reqVO
     * @param projectIds
     * @return
     */
    @TenantIgnore
    List<IotRhDailyReportDO> rhWellNameDailyReportsGroupIds(IPage<IotRhDailyReportDO> page, @Param("reqVO") IotRhDailyReportPageReqVO reqVO,
                                                     @Param("taskIds") Collection<Long> taskIds,
                                                     @Param("projectIds") Collection<Long> projectIds, @Param("deptIds") Collection<Long> deptIds);

    /**
     * 以队伍为维度 查询 井号 分组后的 瑞恒日报 按照任务井分组 小计 查询小组内最后1条记录id
     * @param page
     * @param reqVO
     * @param projectIds
     * @return
     */
    @TenantIgnore
    List<IotRhDailyReportDO> rhDeptDailyReportsGroupIds(IPage<IotRhDailyReportDO> page, @Param("reqVO") IotRhDailyReportPageReqVO reqVO,
                                                            @Param("taskIds") Collection<Long> taskIds,
                                                            @Param("projectIds") Collection<Long> projectIds, @Param("deptIds") Collection<Long> deptIds);

    /**
     * 瑞恒日报汇总统计
     * @param page
     * @param reqVO
     * @param projectIds
     * @return
     */
    IPage<IotRhDailyReportDO> rhReportStatistics(IPage<IotRhDailyReportDO> page, @Param("reqVO") IotRhDailyReportPageReqVO reqVO,
                                             @Param("taskIds") Collection<Long> taskIds,
                                             @Param("projectIds") Collection<Long> projectIds, @Param("deptIds") Collection<Long> deptIds);

    default PageResult<IotRhDailyReportDO> selectPage(IotRhDailyReportPageReqVO reqVO) {
        // 检查contractName不为空但projectIds为空的情况
        if (StrUtil.isNotBlank(reqVO.getContractName()) && (CollUtil.isEmpty(reqVO.getProjectIds()))) {
            return new PageResult<>(Collections.emptyList(), 0L);
        }

        // 检查taskName不为空但taskIds为空的情况
        if (StrUtil.isNotBlank(reqVO.getTaskName()) && (CollUtil.isEmpty(reqVO.getTaskIds()))) {
            return new PageResult<>(Collections.emptyList(), 0L);
        }

        // 正常查询逻辑
        return selectPage(reqVO, new LambdaQueryWrapperX<IotRhDailyReportDO>()
                // .eqIfPresent(IotRhDailyReportDO::getDeptId, reqVO.getDeptId())
                .inIfPresent(IotRhDailyReportDO::getDeptId, reqVO.getDeptIds())
                .eqIfPresent(IotRhDailyReportDO::getProjectId, reqVO.getProjectId())
                .inIfPresent(IotRhDailyReportDO::getProjectId, reqVO.getProjectIds())
                .eqIfPresent(IotRhDailyReportDO::getTaskId, reqVO.getTaskId())
                .inIfPresent(IotRhDailyReportDO::getTaskId, reqVO.getTaskIds())
                .eqIfPresent(IotRhDailyReportDO::getProjectClassification, reqVO.getProjectClassification())
                .eqIfPresent(IotRhDailyReportDO::getRelocationDays, reqVO.getRelocationDays())
                .betweenIfPresent(IotRhDailyReportDO::getTransitTime, reqVO.getTransitTime())
                .eqIfPresent(IotRhDailyReportDO::getDailyGasInjection, reqVO.getDailyGasInjection())
                .eqIfPresent(IotRhDailyReportDO::getDailyWaterInjection, reqVO.getDailyWaterInjection())
                .betweenIfPresent(IotRhDailyReportDO::getDailyInjectGasTime, reqVO.getDailyInjectGasTime())
                .betweenIfPresent(IotRhDailyReportDO::getDailyInjectWaterTime, reqVO.getDailyInjectWaterTime())
                .betweenIfPresent(IotRhDailyReportDO::getNonProductionTime, reqVO.getNonProductionTime())
                .eqIfPresent(IotRhDailyReportDO::getNptReason, reqVO.getNptReason())
                .betweenIfPresent(IotRhDailyReportDO::getConstructionStartDate, reqVO.getConstructionStartDate())
                .betweenIfPresent(IotRhDailyReportDO::getConstructionEndDate, reqVO.getConstructionEndDate())
                .eqIfPresent(IotRhDailyReportDO::getProductionStatus, reqVO.getProductionStatus())
                .eqIfPresent(IotRhDailyReportDO::getNextPlan, reqVO.getNextPlan())
                .eqIfPresent(IotRhDailyReportDO::getConstructionStatus, reqVO.getConstructionStatus())
                .eqIfPresent(IotRhDailyReportDO::getPersonnel, reqVO.getPersonnel())
                .eqIfPresent(IotRhDailyReportDO::getTotalGasInjection, reqVO.getTotalGasInjection())
                .eqIfPresent(IotRhDailyReportDO::getTotalWaterInjection, reqVO.getTotalWaterInjection())
                .eqIfPresent(IotRhDailyReportDO::getCumulativeCompletion, reqVO.getCumulativeCompletion())
                .eqIfPresent(IotRhDailyReportDO::getExtProperty, reqVO.getExtProperty())
                .eqIfPresent(IotRhDailyReportDO::getSort, reqVO.getSort())
                .eqIfPresent(IotRhDailyReportDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotRhDailyReportDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotRhDailyReportDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(IotRhDailyReportDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(IotRhDailyReportDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotRhDailyReportDO::getCreateTime)
                .orderByAsc(IotRhDailyReportDO::getDeptId));
    }

    default List<IotRhDailyReportDO> dailyReports(IotRhDailyReportPageReqVO reqVO) {
        // 获取查询参数中的 statisticFlag 值
        String statisticFlag = reqVO.getStatisticFlag();
        LambdaQueryWrapperX<IotRhDailyReportDO> queryWrapper = new LambdaQueryWrapperX<IotRhDailyReportDO>();
        queryWrapper
                .inIfPresent(IotRhDailyReportDO::getDeptId, reqVO.getDeptIds())
                .eqIfPresent(IotRhDailyReportDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(IotRhDailyReportDO::getTaskId, reqVO.getTaskId())
                .eqIfPresent(IotRhDailyReportDO::getProjectClassification, reqVO.getProjectClassification())
                .eqIfPresent(IotRhDailyReportDO::getRelocationDays, reqVO.getRelocationDays())
                .betweenIfPresent(IotRhDailyReportDO::getTransitTime, reqVO.getTransitTime())
                .eqIfPresent(IotRhDailyReportDO::getDailyGasInjection, reqVO.getDailyGasInjection())
                .eqIfPresent(IotRhDailyReportDO::getDailyWaterInjection, reqVO.getDailyWaterInjection())
                .betweenIfPresent(IotRhDailyReportDO::getDailyInjectGasTime, reqVO.getDailyInjectGasTime())
                .betweenIfPresent(IotRhDailyReportDO::getDailyInjectWaterTime, reqVO.getDailyInjectWaterTime())
                .betweenIfPresent(IotRhDailyReportDO::getNonProductionTime, reqVO.getNonProductionTime())
                .eqIfPresent(IotRhDailyReportDO::getNptReason, reqVO.getNptReason())
                .betweenIfPresent(IotRhDailyReportDO::getConstructionStartDate, reqVO.getConstructionStartDate())
                .betweenIfPresent(IotRhDailyReportDO::getConstructionEndDate, reqVO.getConstructionEndDate())
                .eqIfPresent(IotRhDailyReportDO::getProductionStatus, reqVO.getProductionStatus())
                .eqIfPresent(IotRhDailyReportDO::getNextPlan, reqVO.getNextPlan())
                .eqIfPresent(IotRhDailyReportDO::getConstructionStatus, reqVO.getConstructionStatus())
                .eqIfPresent(IotRhDailyReportDO::getPersonnel, reqVO.getPersonnel())
                .eqIfPresent(IotRhDailyReportDO::getTotalGasInjection, reqVO.getTotalGasInjection())
                .eqIfPresent(IotRhDailyReportDO::getTotalWaterInjection, reqVO.getTotalWaterInjection())
                .eqIfPresent(IotRhDailyReportDO::getCumulativeCompletion, reqVO.getCumulativeCompletion())
                .eqIfPresent(IotRhDailyReportDO::getExtProperty, reqVO.getExtProperty())
                .eqIfPresent(IotRhDailyReportDO::getSort, reqVO.getSort())
                .eqIfPresent(IotRhDailyReportDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotRhDailyReportDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotRhDailyReportDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(IotRhDailyReportDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(IotRhDailyReportDO::getCreateTime, reqVO.getCreateTime());
        if ("Y".equals(statisticFlag)) {
            queryWrapper.and(wrapper -> wrapper
                    .gt(IotRhDailyReportDO::getDailyInjectGasTime, 0)
                    .or()
                    .gt(IotRhDailyReportDO::getDailyInjectWaterTime, 0)
            );
        }
        if (StrUtil.isNotBlank(reqVO.getYear())) {
            queryWrapper.apply("YEAR(create_time) = {0}", reqVO.getYear());
        }
        return selectList(queryWrapper);
    }

    // 修改selectList方法，使用XML实现
    List<IotRhDailyReportDO> selectListGrouped(@Param("reqVO") IotRhDailyReportPageReqVO reqVO);

    /**
     * 查询每个任务的搬迁安装天数
     * @param reqVO
     * @return
     */
    List<IotRhDailyReportDO> relocationDays(@Param("reqVO") IotRhDailyReportPageReqVO reqVO);

    /**
     * 根据条件查询瑞恒日报记录
     *
     * @param deptId     施工队伍id
     * @param taskId     任务ID，可选参数，用于筛选任务ID匹配的记录
     * @param dateTime   当前时间
     * @return 返回符合条件的瑞恒日报记录
     */
    default IotRhDailyReportDO selectExistReport(Long deptId, Long taskId, LocalDateTime dateTime) {
        String dateStr = ObjUtil.isNotEmpty(dateTime) ? dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) : StrUtil.EMPTY;
        // 使用LambdaQueryWrapperX构建查询条件，根据传入的参数动态添加查询条件
        return selectOne(new LambdaQueryWrapperX<IotRhDailyReportDO>()
                .eqIfPresent(IotRhDailyReportDO::getDeptId, deptId)
                .eqIfPresent(IotRhDailyReportDO::getTaskId, taskId)
                .apply(StrUtil.isNotBlank(dateStr), "DATE(create_time) = {0}", dateStr)
                .last("limit 1"));
    }

    /**
     * 瑞恒日报状态统计
     * @param reqVO
     * @return
     */
    List<AllOrderResp> selectStatusNumber(@Param("reqVO") IotMaintainPageReqVO reqVO);
}
