package cn.iocoder.yudao.module.system.dal.mysql.elderly;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyNursingLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanLogRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.elderly.ElderlyNursingLogDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * 长者护理记录 Mapper
 */
@Mapper
public interface ElderlyNursingLogMapper extends BaseMapperX<ElderlyNursingLogDO> {

    default List<ElderlyNursingLogDO> selectList(String nurse, Long tenantId, LocalDate startDate, LocalDate endDate) {
        return selectList(new LambdaQueryWrapperX<ElderlyNursingLogDO>()
                .eqIfPresent(ElderlyNursingLogDO::getTenantId, tenantId)
                .likeIfPresent(ElderlyNursingLogDO::getNurse, nurse)
                .leIfPresent(ElderlyNursingLogDO::getNurseDate, endDate)
                .ge(ElderlyNursingLogDO::getNurseDate, startDate)
                .orderByDesc(ElderlyNursingLogDO::getNurseDate)
                .orderByDesc(ElderlyNursingLogDO::getId));
    }

    List<ElderlyNursingLogRespVO> getFinishList(@Param("nurse") String nurse,
                                          @Param("elderName") String elderName,
                                          @Param("tenantId") Long tenantId,
                                          @Param("frequencyCategoryType") Integer frequencyCategoryType,
                                          @Param("startDate") LocalDateTime startDate,
                                          @Param("endDate") LocalDateTime endDate);

    List<ElderlyNursingLogRespVO> selectPageByElderIdWithItemName(@Param("page") Page<ElderlyNursingLogRespVO> page,
                                                                  @Param("elderId") Long elderId,
                                                                  @Param("startDate") LocalDateTime startDate,
                                                                  @Param("endDate") LocalDateTime endDate);

    List<NursingPlanLogRespVO> selectPlanLogByElderIdWithItemName(@Param("elderId") Long elderId);

    List<LatestCreateTimeDTO> selectLatestCreateTimeByPlanItemIds(@Param("tenantId") Long tenantId,
                                                                  @Param("nursingPlanItemIds") List<Long> nursingPlanItemIds);

    /**
     * 批量查询指定护理明细（nursingPlanItemId）下最近一次生成日志的 createTime。
     * 用于定时器按“频率间隔”判断是否需要继续生成新的日志。
     */
    @Data
    class LatestCreateTimeDTO {
        private Long nursingPlanItemId;
        private LocalDateTime createTime;
    }
}
