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

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.elderly.vo.ElderlySafeCheckElderPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlySafeCheckElderPageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlySafeCheckPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.elderly.ElderlySafeCheckDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

@Mapper
public interface ElderlySafeCheckMapper extends BaseMapperX<ElderlySafeCheckDO> {

    default PageResult<ElderlySafeCheckDO> selectRecordPage(ElderlySafeCheckPageReqVO reqVO,
                                                           LocalDateTime startTime,
                                                           LocalDateTime endTimeExclusive) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ElderlySafeCheckDO>()
                .eqIfPresent(ElderlySafeCheckDO::getElderId, reqVO.getElderId())
                .geIfPresent(ElderlySafeCheckDO::getCreateTime, startTime)
                .ltIfPresent(ElderlySafeCheckDO::getCreateTime, endTimeExclusive)
                .inIfPresent(ElderlySafeCheckDO::getTenantId, reqVO.getTenantIds())
                .orderByDesc(ElderlySafeCheckDO::getCreateTime)
                .orderByDesc(ElderlySafeCheckDO::getId));
    }

    @Select({"<script>",
            "SELECT",
            "  esc.elder_id AS elderId,",
            "  ei.elder_name AS elderName,",
            "  ei.room_name AS roomName,",
            "  ei.bed_name AS bedName",
            "FROM elderly_safe_check esc",
            "LEFT JOIN elderly_info ei ON ei.id = esc.elder_id",
            "LEFT JOIN org_build_room r ON ei.room_id = r.id",
            "LEFT JOIN org_build_bed b ON ei.bed_id = b.id",
            "WHERE 1 = 1",
            "  <if test='pageVO.elderId != null'>",
            "    AND esc.elder_id = #{pageVO.elderId}",
            "  </if>",
            "  <if test='pageVO.elderName != null and pageVO.elderName != \"\"'>",
            "    AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')",
            "  </if>",
            "  <if test='pageVO.checkDate != null and pageVO.checkDate.length &gt; 0 and pageVO.checkDate[0] != null'>",
            "    AND esc.create_time &gt;= TIMESTAMP(#{pageVO.checkDate[0]}, '00:00:00')",
            "  </if>",
            "  <if test='pageVO.checkDate != null and pageVO.checkDate.length &gt; 1 and pageVO.checkDate[1] != null'>",
            "    AND esc.create_time &lt; TIMESTAMP(DATE_ADD(#{pageVO.checkDate[1]}, INTERVAL 1 DAY), '00:00:00')",
            "  </if>",
            "  <if test='pageVO.tenantIds != null and pageVO.tenantIds.length &gt; 0'>",
            "    AND esc.tenant_id IN",
            "    <foreach item='id' collection='pageVO.tenantIds' open='(' separator=',' close=')'>",
            "      #{id}",
            "    </foreach>",
            "  </if>",
            "GROUP BY esc.elder_id, ei.elder_name, ei.room_name, ei.bed_name",
            "ORDER BY ei.elder_name ASC, esc.elder_id DESC",
            "</script>"})
    List<ElderlySafeCheckElderPageRespVO> selectElderPage(@Param("page") Page<?> page,
                                                         @Param("pageVO") ElderlySafeCheckElderPageReqVO pageReqVO);
}

