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

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.system.controller.admin.biz.vo.elderlyitemsround.ElderlyItemsRoundPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyitemsround.ElderlyItemsRoundPageRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyItemsRoundDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

@Mapper
public interface ElderlyItemsRoundMapper extends BaseMapperX<ElderlyItemsRoundDO> {

    List<ElderlyItemsRoundPageRespVO> selectPageWithElderInfo(Page<ElderlyItemsRoundPageRespVO> page,
                                                      @Param("reqVO") ElderlyItemsRoundPageReqVO reqVO);

    ElderlyItemsRoundDO selectByIdWithElderInfo(@Param("id") Long id);

    List<Map<String, Object>> selectMonthDayMinStatus(@Param("tenantId") Long tenantId,
                                                      @Param("startTime") LocalDateTime startTime,
                                                      @Param("endTime") LocalDateTime endTime);

    default PageResult<ElderlyItemsRoundPageRespVO> selectPageWithElderInfo(ElderlyItemsRoundPageReqVO reqVO) {
        Page<ElderlyItemsRoundPageRespVO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        List<ElderlyItemsRoundPageRespVO> list = selectPageWithElderInfo(page, reqVO);
        return new PageResult<>(list, page.getTotal());
    }

    default PageResult<ElderlyItemsRoundDO> selectDetailPage(ElderlyItemsRoundPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ElderlyItemsRoundDO>()
                .eq(ElderlyItemsRoundDO::getElderId, reqVO.getElderId())
                .eqIfPresent(ElderlyItemsRoundDO::getRole, reqVO.getRole())
                .geIfPresent(ElderlyItemsRoundDO::getRoundTime, reqVO.getRoundTime() != null && reqVO.getRoundTime().length >= 1
                        ? reqVO.getRoundTime()[0].atStartOfDay() : null)
                .ltIfPresent(ElderlyItemsRoundDO::getRoundTime, reqVO.getRoundTime() != null && reqVO.getRoundTime().length == 2
                        ? reqVO.getRoundTime()[1].plusDays(1).atStartOfDay() : null)
                .orderByDesc(ElderlyItemsRoundDO::getRoundTime, ElderlyItemsRoundDO::getId));
    }
}
