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

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

    <resultMap id="deviceMap" type="com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationWithProductVO">
        <!--设备字段-->
        <id property="id" column="ID" />
        <result property="productId" column="PDT_ID" />
        <result property="deviceName" column="DEV_NAME" />
        <result property="alias" column="ALS" />
        <result property="deviceStatus" column="DEV_STS" />
        <result property="inUseFlag" column="IN_USE_FLG" />
        <result property="gradeCategory" column="GRD_CTGR" />
        <result property="deviceNumber" column="DEV_NUM" />
        <result property="deviceCode" column="DEV_CD" />
        <result property="assetCode" column="AST_CD" />
        <result property="factoryCode" column="FACT_CD" />
        <result property="position" column="POS" />
        <result property="purchasePrice" column="PRCH_PRC" />
        <result property="purchaseDate" column="PRCH_DT" />
        <result property="productiveDate" column="PDTV_DT" />
        <result property="deployDate" column="DPLY_DT" />
        <result property="deployEngineer" column="DPLY_ENGR" />
        <result property="deployLocation" column="DPLY_LOC" />
        <result property="acceptanceUserId" column="ACPT_USER_ID" />
        <result property="acceptanceUserName" column="ACPT_USER_NAME" />
        <result property="managerUserId" column="MNGR_USER_ID" />
        <result property="managerUserName" column="MNGR_USER_NAME" />
        <result property="customFormData" column="CST_FORM_DAT" />
        <result property="photo" column="PHTO" />
        <result property="deviceParameter" column="DEV_PRM" />
        <result property="stateShow" column="STA_SHW" />
        <result property="stateStack" column="STA_STK" />
        <result property="systemDepartmentCode" column="SYS_DEPT_CD" />
        <result property="remark" column="RMK" />
        <result property="isConnect" column="IS_CONN" />
        <result property="lastConnectTime" column="LST_CONN_TM" />
        <result property="collectDeviceType" column="COLT_DEV_TP" />
        <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="SPEC"/>
        <result property="modelNo" column="MDL_NO"/>
        <result property="customConfig" column="CST_CFG"/>
    </resultMap>

    <select id="selectPage" resultMap="deviceMap">
        SELECT d.*,
               p.MFR,
               p.SPEC,
               p.MDL_NO,
               p.CST_CFG
        from
            T_DEV_INF d
        LEFT JOIN T_DEV_PDT p ON d.PDT_ID = p.ID
        <where>
            <if test="param.id != null">
                and d.ID = #{param.id}
            </if>
            <if test="param.productId != null">
                and d.PDT_ID = #{param.productId}
            </if>
            <if test="param.productIds != null and param.productIds.size > 0">
                and d.PDT_ID in (
                    <foreach collection="param.productIds" item="productId" separator=",">
                        #{productId}
                    </foreach>
                )
            </if>
            <if test="param.deviceName != null and param.deviceName != ''">
                and d.DEV_NAME like concat('%',#{param.deviceName},'%')
            </if>
            <if test="param.alias != null and param.alias != ''">
                and d.ALIAS like concat('%',#{param.alias},'%')
            </if>
            <if test="param.deviceCode != null and param.deviceCode != ''">
                and d.DEV_CD like concat('%',#{param.deviceCode},'%')
            </if>
            <if test="param.position != null and param.position != ''">
                and d.POS like concat('%',#{param.position},'%')
            </if>
            <if test="param.deployLocation != null and param.deployLocation != ''">
                and d.DPLY_LOC like concat('%',#{param.deployLocation},'%')
            </if>
            <if test="param.deviceStatusJson != null and param.deviceStatusJson.Accept != null">
                and JSON_VALUE(d.DEV_STS, '$.Accept') = #{param.deviceStatusJson.Accept}
            </if>
            <if test="param.deviceStatusJson != null and param.deviceStatusJson.Repair != null">
                and JSON_VALUE(d.DEV_STS, '$.Repair') = #{param.deviceStatusJson.Repair}
            </if>
            <if test="param.deviceStatusJson != null and param.deviceStatusJson.Demote != null">
                and JSON_VALUE(d.DEV_STS, '$.Demote') = #{param.deviceStatusJson.Demote}
            </if>
            <if test="param.deviceStatusJson != null and param.deviceStatusJson.Scrap != null">
                and JSON_VALUE(d.DEV_STS, '$.Scrap') = #{param.deviceStatusJson.Scrap}
            </if>
            <if test="param.deviceStatusJson != null and param.deviceStatusJson.Enable != null">
                and JSON_VALUE(d.DEV_STS, '$.Enable') = #{param.deviceStatusJson.Enable}
            </if>
            <if test="param.deviceStatusJson != null and param.deviceStatusJson.Borrow != null">
                and JSON_VALUE(d.DEV_STS, '$.Borrow') = #{param.deviceStatusJson.Borrow}
            </if>
            and d.DELETED = 0
        </where>
    </select>

</mapper>