<?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="org.springblade.modules.business.equipment.inspection.base.mapper.EquipmentInspectionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="EquipmentInspectionInfoResultMap"
               type="org.springblade.modules.business.equipment.inspection.base.entity.EquipmentInspectionInfoEntity">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="inspection_title" property="inspectionTitle"/>
        <result column="inspection_date" property="inspectionDate"/>
    </resultMap>


    <resultMap id="equipmentInspectionStatisticsVOResultMap"
               type="org.springblade.modules.business.equipment.inspection.base.vo.EquipmentInspectionStatisticsVO">
        <result column="id" property="id"/>
        <result column="real_name" property="servicePersonName"/>
        <result column="complete_count" property="completeCount"/>
    </resultMap>

    <select id="inspectionReportCount" resultMap="equipmentInspectionStatisticsVOResultMap">
        SELECT u.real_name,u.id,s.complete_count FROM blade_user u LEFT JOIN
        (SELECT create_user,count(create_user) AS complete_count FROM equipment_inspection_report_info WHERE is_deleted
        =0
        <if test="inspectionInfoDTO.planId!=null">
            AND plan_id= #{inspectionInfoDTO.planId}
        </if>
        GROUP BY create_user) s
        on s.create_user = u.id
        WHERE u.is_deleted=0
        <if test="inspectionInfoDTO.orgId!=null">
            AND u.dept_id = #{inspectionInfoDTO.orgId}
        </if>
    </select>

    <select id="inspectionCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        equipment_inspection_report_info o
        LEFT JOIN rtu_info i ON i.rtu_code = o.rtu_code
        WHERE
        o.is_deleted = 0
        <if test="inspectionInfoDTO.orgId!=null">
            and i.org_id = #{inspectionInfoDTO.orgId}
        </if>
        <if test="inspectionInfoDTO.planId!=null">
            AND o.plan_id= #{inspectionInfoDTO.planId}
        </if>
    </select>
</mapper>
