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

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.module.pms.controller.admin.iotoperationmeetingdetail.vo.IotOperationMeetingDetailPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoperationmeetingdetail.IotOperationMeetingDetailDO;
import org.apache.ibatis.annotations.Mapper;

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

/**
 * 生产运营会明细 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotOperationMeetingDetailMapper extends BaseMapperX<IotOperationMeetingDetailDO> {

    default PageResult<IotOperationMeetingDetailDO> selectPage(IotOperationMeetingDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotOperationMeetingDetailDO>()
                .eqIfPresent(IotOperationMeetingDetailDO::getMeetingId, reqVO.getMeetingId())
                .inIfPresent(IotOperationMeetingDetailDO::getMeetingId, reqVO.getMeetingIds())
                .eqIfPresent(IotOperationMeetingDetailDO::getCompanyId, reqVO.getCompanyId())
                .eqIfPresent(IotOperationMeetingDetailDO::getDeptId, reqVO.getDeptId())
                .likeIfPresent(IotOperationMeetingDetailDO::getProjectName, reqVO.getProjectName())
                .betweenIfPresent(IotOperationMeetingDetailDO::getMeetingDate, reqVO.getMeetingDate())
                .eqIfPresent(IotOperationMeetingDetailDO::getMeetingSeries, reqVO.getMeetingSeries())
                .eqIfPresent(IotOperationMeetingDetailDO::getCurrentRevenue, reqVO.getCurrentRevenue())
                .eqIfPresent(IotOperationMeetingDetailDO::getCumulativeRevenue, reqVO.getCumulativeRevenue())
                .eqIfPresent(IotOperationMeetingDetailDO::getCurrentOnAccount, reqVO.getCurrentOnAccount())
                .eqIfPresent(IotOperationMeetingDetailDO::getCumulativeOnAccount, reqVO.getCumulativeOnAccount())
                .eqIfPresent(IotOperationMeetingDetailDO::getCurrentPayment, reqVO.getCurrentPayment())
                .eqIfPresent(IotOperationMeetingDetailDO::getCumulativePayment, reqVO.getCumulativePayment())
                .eqIfPresent(IotOperationMeetingDetailDO::getPlannedWorkload, reqVO.getPlannedWorkload())
                .eqIfPresent(IotOperationMeetingDetailDO::getActualCompletion, reqVO.getActualCompletion())
                .eqIfPresent(IotOperationMeetingDetailDO::getEquipmentUtilizationRate, reqVO.getEquipmentUtilizationRate())
                .eqIfPresent(IotOperationMeetingDetailDO::getKeyWorkCompletion, reqVO.getKeyWorkCompletion())
                .eqIfPresent(IotOperationMeetingDetailDO::getProblemsAnalysis, reqVO.getProblemsAnalysis())
                .eqIfPresent(IotOperationMeetingDetailDO::getNextPlannedWorkload, reqVO.getNextPlannedWorkload())
                .eqIfPresent(IotOperationMeetingDetailDO::getPriorityTasks, reqVO.getPriorityTasks())
                .eqIfPresent(IotOperationMeetingDetailDO::getSupport, reqVO.getSupport())
                .eqIfPresent(IotOperationMeetingDetailDO::getSort, reqVO.getSort())
                .eqIfPresent(IotOperationMeetingDetailDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotOperationMeetingDetailDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(IotOperationMeetingDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotOperationMeetingDetailDO::getId));
    }

    /**
     * 查询：createTime 所在年份 + projectName 模糊匹配 + sort 最大的记录
     */
    default List<IotOperationMeetingDetailDO> selectMaxSortByYearAndProject(IotOperationMeetingDetailPageReqVO reqVO) {
        LocalDateTime[] createTimeArr = reqVO.getCreateTime();
        if (createTimeArr == null || createTimeArr.length == 0) {
            return null;
        }
        // 取年份（用数组第一个时间即可）
        int year = createTimeArr[0].getYear();

        return selectList(new LambdaQueryWrapperX<IotOperationMeetingDetailDO>()
                // 1. 匹配 createTime 所在年份
                .apply("YEAR(create_time) = {0}", year)
                // 2. projectName 模糊查询
                .eq(IotOperationMeetingDetailDO::getProjectName, reqVO.getProjectName())
                // 3. 按 sort 倒序 → 取第一条就是最大值
                .orderByDesc(IotOperationMeetingDetailDO::getSort)
        );
    }

    /**
     * 查询：createTime 所在年份 + projectName 模糊匹配 + sort 最大的记录
     */
    default List<IotOperationMeetingDetailDO> summarizedProjectDetails(IotOperationMeetingDetailPageReqVO reqVO) {

        return selectList(new LambdaQueryWrapperX<IotOperationMeetingDetailDO>()
                // 1. 匹配 createTime 所在年份
                .apply("YEAR(create_time) = {0}", reqVO.getYear())
                // 2. sort 期次 精确查询
                .eq(IotOperationMeetingDetailDO::getSort, reqVO.getSort())
                // 3. 按 sort 倒序 → 取第一条就是最大值
                .orderByDesc(IotOperationMeetingDetailDO::getId)
        );
    }

}