package com.njcn.rdms.module.project.dal.mysql.workreport.monthly;

import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.project.dal.dataobject.workreport.monthly.MonthlyReportApprovalRecordDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface MonthlyReportApprovalRecordMapper extends BaseMapperX<MonthlyReportApprovalRecordDO> {

    default List<MonthlyReportApprovalRecordDO> selectListByMonthlyReportId(Long monthlyReportId) {
        return selectList(new LambdaQueryWrapperX<MonthlyReportApprovalRecordDO>()
                .eq(MonthlyReportApprovalRecordDO::getMonthlyReportId, monthlyReportId)
                .orderByDesc(MonthlyReportApprovalRecordDO::getApprovalRound)
                .orderByDesc(MonthlyReportApprovalRecordDO::getId));
    }

    default int countByMonthlyReportId(Long monthlyReportId) {
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<MonthlyReportApprovalRecordDO>()
                .eq(MonthlyReportApprovalRecordDO::getMonthlyReportId, monthlyReportId)));
    }

    default int deleteByMonthlyReportId(Long monthlyReportId) {
        return delete(new LambdaQueryWrapperX<MonthlyReportApprovalRecordDO>()
                .eq(MonthlyReportApprovalRecordDO::getMonthlyReportId, monthlyReportId));
    }

    default MonthlyReportApprovalRecordDO selectLatestApprovedByMonthlyReportId(Long monthlyReportId) {
        return selectOne(new LambdaQueryWrapperX<MonthlyReportApprovalRecordDO>()
                .eq(MonthlyReportApprovalRecordDO::getMonthlyReportId, monthlyReportId)
                .eq(MonthlyReportApprovalRecordDO::getConclusion, "approved")
                .orderByDesc(MonthlyReportApprovalRecordDO::getApprovalRound)
                .orderByDesc(MonthlyReportApprovalRecordDO::getId)
                .last("LIMIT 1"));
    }
}
