<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zt.plat.module.qms.resource.device.dal.mapper.DevicePeriodCheckMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <resultMap id="periodCheckMap" type="com.zt.plat.module.qms.resource.device.dal.dataobject.DevicePeriodCheckDO">
        <result property="id" column="ID"/>
        <result property="planId" column="PLN_ID"/>
        <result property="effectiveFlag" column="EFCT_FLG"/>
        <result property="deviceId" column="DEV_ID"/>
        <result property="checkPersonRemark" column="CHK_PSN_RMK"/>
        <result property="checkPersonName" column="CHK_PSN_NAME"/>
        <result property="checkPersonId" column="CHK_PSN_ID"/>
        <result property="recheckPersonName" column="RCHK_PSN_NAME"/>
        <result property="recheckPersonId" column="RCHK_PSN_ID"/>
        <result property="checkAccording" column="CHK_ACRD"/>
        <result property="checkAccordingRemark" column="CHK_ACRD_RMK"/>
        <result property="checkContent" column="CHK_CNTT"/>
        <result property="checkResult" column="CHK_RSLT"/>
        <result property="checkRemark" column="CHK_RMK"/>
        <result property="checkDate" column="CHK_DT"/>
        <result property="lastCheckDate" column="LST_CHK_DT"/>
        <result property="planCheckDateStart" column="PLN_CHK_DT_STRT"/>
        <result property="planCheckDateEnd" column="PLN_CHK_DT_END"/>
        <result property="frequencyRemark" column="FREQ_RMK"/>
        <result property="flowStatus" column="FLW_STS"/>
        <result property="flowInstanceId" column="FLW_INSC_ID"/>
        <result property="systemDepartmentCode" column="SYS_DEPT_CD"/>
        <result property="remark" column="RMK"/>
        <result property="companyId" column="COMPANY_ID" />
        <result property="companyName" column="COMPANY_NAME" />
        <result property="deptId" column="DEPT_ID" />
        <result property="deptName" column="DEPT_NAME" />
        <result property="postId" column="POST_ID" />
        <result property="creatorName" column="CREATOR_NAME" />
        <result property="creator" column="CREATOR" />
        <result property="updaterName" column="UPDATER_NAME" />
        <result property="updater" column="UPDATER" />
        <result property="tenantId" column="TENANT_ID" />
        <result property="createTime" column="CREATE_TIME" />
        <result property="updateTime" column="UPDATE_TIME" />
    </resultMap>

    <select id="getLastDataCheckList" resultMap="periodCheckMap">
        SELECT
            c.*,
            ROW_NUMBER () OVER (PARTITION BY c.DEV_ID ORDER BY c.PLN_CHK_DT_STRT DESC) AS rn
        FROM
            T_DEV_PRD_CHK c
                LEFT JOIN T_DEV_PRD_CHK_PLN p on c.PLN_ID = p.id
        <where>
            AND DEV_ID IN
            <foreach collection="param.deviceIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            AND p.FLW_STS = #{param.flowStatus}
        </where>

    </select>

</mapper>