<?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.DeviceUseRecordMapper">
    <resultMap id="DeviceInfomationUseRecordVO" type="com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationUseRecordVO">
        <!-- 主键 -->
        <id column="ID" property="id" javaType="java.lang.Long"/>
        <result column="PDT_ID" property="productId" javaType="java.lang.Long"/>
        <result column="DEV_NAME" property="deviceName" javaType="java.lang.String"/>
        <result column="ALS" property="alias" javaType="java.lang.String"/>
        <result column="DEV_STS" property="deviceStatus" javaType="java.lang.String"/>
        <result column="IN_USE_FLG" property="inUseFlag" javaType="java.lang.String"/>
        <result column="GRD_CTGR" property="gradeCategory" javaType="java.lang.String"/>
        <result column="DEV_NUM" property="deviceNumber" javaType="java.lang.String"/>
        <result column="DEV_CD" property="deviceCode" javaType="java.lang.String"/>
        <result column="AST_CD" property="assetCode" javaType="java.lang.String"/>
        <result column="FACT_CD" property="factoryCode" javaType="java.lang.String"/>
        <result column="POS" property="position" javaType="java.lang.String"/>
        <result column="PRCH_PRC" property="purchasePrice" javaType="java.math.BigDecimal"/>
        <result column="PRCH_DT" property="purchaseDate" javaType="java.time.LocalDateTime"/>
        <result column="PDTV_DT" property="productiveDate" javaType="java.time.LocalDateTime"/>
        <result column="DPLY_DT" property="deployDate" javaType="java.time.LocalDateTime"/>
        <result column="DPLY_ENGR" property="deployEngineer" javaType="java.lang.String"/>
        <result column="DPLY_LOC" property="deployLocation" javaType="java.lang.String"/>
        <result column="ACPT_USER_ID" property="acceptanceUserId" javaType="java.lang.String"/>
        <result column="ACPT_USER_NAME" property="acceptanceUserName" javaType="java.lang.String"/>
        <result column="MNGR_USER_ID" property="managerUserId" javaType="java.lang.String"/>
        <result column="MNGR_USER_NAME" property="managerUserName" javaType="java.lang.String"/>
        <result column="CST_FORM_DAT" property="customFormData" javaType="java.lang.String"/>
        <result column="PHTO" property="photo" javaType="java.lang.String"/>
        <result column="DEV_PRM" property="deviceParameter" javaType="java.lang.String"/>
        <result column="STA_SHW" property="stateShow" javaType="java.lang.String"/>
        <result column="STA_STK" property="stateStack" javaType="java.lang.String"/>
        <result column="SYS_DEPT_CD" property="systemDepartmentCode" javaType="java.lang.String"/>
        <result column="RMK" property="remark" javaType="java.lang.String"/>
        <result column="IS_CONN" property="isConnect" javaType="java.lang.String"/>
        <result column="LST_CONN_TM" property="lastConnectTime" javaType="java.time.LocalDateTime"/>
        <result column="COLT_DEV_TP" property="collectDeviceType" javaType="java.lang.String"/>
        <!--大类字段-->
        <result property="deptName" column="DEPT_NAME"/>
        <result property="manufacturer" column="MFR"/>
        <result property="specification" column="SPEC"/>
        <result property="modelNo" column="MDL_NO"/>
        <!--使用记录字段-->
        <result property="finishedCount" column="finished_count"/>
        <result property="runningCount" column="running_count"/>

    </resultMap>
    <!--
        <sql id="fieldDAndP">
            d.*,
            p.MFR,
            p.SPEC,
            p.MDL_NO
        </sql>

        <select id="queryVoById" resultMap="DeviceInfomationUseRecordVO">
            select m.*,
            <include refid="fieldDAndP"/>
            from T_DEV_USE_RCD m
            left join T_DEV_INF d on m.DEV_ID = d.id
            left join T_DEV_PDT p on d.PDT_ID = p.id
            <where>
                and m.ID = #{id}
            </where>
        </select>
    -->

    <select id="queryPageListWithCount" resultMap="DeviceInfomationUseRecordVO">
        select
        d.*,
        p.MFR,
        p.SPEC,
        p.MDL_NO
        ,(
        select count(*) from T_DEV_USE_RCD m
        where 1=1
        and m.DEV_ID = d.ID
        <if test="param.useTimeStart != null and param.useTimeStart.length>0">
            and m.USE_TM_STRT between #{param.useTimeStart[0]} and #{param.useTimeStart[1]}
        </if>
        and m.USE_TM_END is not null
        and m.DELETED = 0
        ) as finished_count,
        (
        select count(*) from T_DEV_USE_RCD m
        where 1=1
        and m.DEV_ID = d.ID
        <if test="param.useTimeStart != null and param.useTimeStart.length>0">
            and m.USE_TM_STRT between #{param.useTimeStart[0]} and #{param.useTimeStart[1]}
        </if>
        and m.USE_TM_END is null
        and m.DELETED = 0
        ) as running_count
        from T_DEV_INF d
        left join T_DEV_PDT p on d.PDT_ID = p.ID
        <where>
            <if test="param.deviceId != null and param.deviceId != ''">
                and d.id = #{param.deviceId}
            </if>
            <if test="param.deviceIdList!=null and param.deviceIdList.size>0">
                and d.id in
                <foreach collection="param.deviceIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </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.deviceName != null and param.deviceName != ''">
                and d.DEV_NAME like concat(concat('%', #{param.deviceName}), '%')
            </if>
            <if test="param.deviceCode != null and param.deviceCode != ''">
                and d.DEV_CD like concat(concat('%', #{param.deviceCode}), '%')
            </if>
            <if test="param.deptName != null and param.deptName != ''">
                and d.DEPT_NAME = #{param.deptName}
            </if>
            and d.DELETED = 0
        </where>
    </select>
    

</mapper>