<?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.DeviceCalibrationMapper">

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

    <resultMap id="calibrationMap" type="com.zt.plat.module.qms.resource.device.controller.vo.DeviceCalibrationVO">
        <id column="ID" property="id" javaType="java.lang.Long"/>
        <result column="PLN_ID" property="planId" javaType="java.lang.Long"/>
        <result column="EFCT_FLG" property="effectiveFlag" javaType="java.lang.String"/>
        <result column="DEV_ID" property="deviceId" javaType="java.lang.Long"/>
        <result column="SPLR_NAME" property="supplierName" javaType="java.lang.String"/>
        <result column="SPLR_ID" property="supplierId" javaType="java.lang.Long"/>
        <result column="SPEC" property="specification" javaType="java.lang.String"/>
        <result column="PLN_CHK_DT" property="planCheckDate" javaType="java.time.LocalDateTime"/>
        <result column="LST_CHK_DT" property="lastCheckDate" javaType="java.time.LocalDateTime"/>
        <result column="FREQ_RMK" property="frequencyRemark" javaType="java.lang.String"/>
        <result column="CHK_TP" property="checkType" javaType="java.lang.String"/>
        <result column="CHK_DT" property="checkDate" javaType="java.time.LocalDateTime"/>
        <result column="AGT" property="agent" javaType="java.lang.String"/>
        <result column="CRTF_CD" property="certificateCode" javaType="java.lang.String"/>
        <result column="VDTY_DT" property="validityDate" javaType="java.time.LocalDateTime"/>
        <result column="CHK_CNTT" property="checkContent" javaType="java.lang.String"/>
        <result column="CHK_RSLT" property="checkResult" javaType="java.lang.String"/>
        <result column="FLW_STS" property="flowStatus" javaType="java.lang.String"/>
        <result column="FLW_INSC_ID" property="flowInstanceId" javaType="java.lang.String"/>
        <result property="formId" column="FORM_ID"/>
        <result column="CNL_FLG" property="cancelFlag" javaType="java.lang.Integer"/>
        <result column="SYS_DEPT_CD" property="systemDepartmentCode" javaType="java.lang.String"/>
        <result column="RMK" property="remark" javaType="java.lang.String"/>
        <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" />
        <!--大类字段-->
        <result property="manufacturer" column="MFR"/>
        <result property="specification" column="device_specification"/>
        <result property="modelNo" column="MDL_NO"/>
        <!--设备字段-->
        <result property="deviceName" column="DEV_NAME"/>
        <result property="alias" column="ALS"/>
        <result property="deviceCode" column="DEV_CD"/>
        <result property="factoryCode" column="FACT_CD"/>
    </resultMap>

    <select id="getLastDataCheckList" resultMap="calibrationMap">
        SELECT
        c.*
        FROM
        T_DEV_CLB c
        LEFT JOIN T_DEV_CLB_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>

    <select id="queryPageList" resultMap="calibrationMap">
        SELECT
            c.*,
            d.DEV_NAME,
            d.ALS,
            d.DEV_CD,
            d.FACT_CD,
            p.MFR,
            p.SPEC as device_specification,
            p.MDL_NO
        FROM
            T_DEV_CLB c
        left join T_DEV_INF d on c.DEV_ID = d.ID
        left join T_DEV_PDT p on d.PDT_ID = p.ID
        <where>
            <if test="param.id != null">
                and c.ID = #{param.id}
            </if>
            <if test="param.deviceId != null">
                and c.DEV_ID = #{param.deviceId}
            </if>
            <if test="param.productId != null">
                and d.PDT_ID = #{param.productId}
            </if>
            <if test="param.productIdList!=null and param.productIdList.size>0">
                and d.PDT_ID in
                <foreach collection="param.productIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.planId != null">
                and c.PLN_ID = #{param.planId}
            </if>
            <if test="param.effectiveFlag != null and param.effectiveFlag != ''">
                and c.EFCT_FLG = #{param.effectiveFlag}
            </if>
            <if test="param.deptId != null">
                and d.DEPT_ID = #{param.deptId}
            </if>
            <if test="param.deptIdList!=null and param.deptIdList.size>0">
                and d.DEPT_ID in
                <foreach collection="param.deptIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.checkDate != null and param.checkDate.length > 0">
                AND c.CHK_DT BETWEEN #{checkDate[0]} AND #{checkDate[1]}
            </if>
            <if test="param.deviceName != null and param.deviceName != ''">
                and d.DEV_NAME like concat(concat('%', #{param.deviceName}), '%')
            </if>
        </where>
    </select>


</mapper>