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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoperationmeeting.IotOperationMeetingDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.pms.controller.admin.iotoperationmeeting.vo.*;

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

/**
 * 生产运营会 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotOperationMeetingMapper extends BaseMapperX<IotOperationMeetingDO> {

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

    /**
     * 查询：校验当前公司 当前日期所属年 当前期次是否有重复的记录
     */
    default List<IotOperationMeetingDO> selectExistMeetingByYearAndSort(IotOperationMeetingPageReqVO reqVO) {
        LocalDateTime[] createTimeArr = reqVO.getCreateTime();
        if (createTimeArr == null || createTimeArr.length == 0) {
            return null;
        }
        // 取年份（用数组第一个时间即可）
        int year = createTimeArr[0].getYear();

        return selectList(new LambdaQueryWrapperX<IotOperationMeetingDO>()
                // 1. 匹配 createTime 所在年份
                .apply("YEAR(create_time) = {0}", year)
                // 2. sort 查询
                .eq(IotOperationMeetingDO::getSort, reqVO.getSort())
        );
    }

}