<?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.ruoyi.manage.mapper.RecordInspectionMapper">

    <resultMap type="com.ruoyi.manage.domain.RecordInspection" id="RecordInspectionResult">
        <result property="inspectionId" column="inspection_id"/>
        <result property="inspectionTime" column="inspection_time"/>
        <result property="inspectionUser" column="inspection_user"/>
        <result property="totalNumber" column="total_number"/>
        <result property="normalNumber" column="normal_number"/>
        <result property="abnormalNumber" column="abnormal_number"/>
        <result property="skipNumber" column="skip_number"/>
    </resultMap>

    <sql id="selectRecordInspectionVo">
        select inspection_id, inspection_time, inspection_user
        from record_inspection
    </sql>

    <select id="selectRecordInspectionList" parameterType="RecordInspection" resultMap="RecordInspectionResult">
        select ri.inspection_id,
                    inspection_time,
                    inspection_user,
                    count(rii.inspection_id) as total_number,
                    count(if(inspection_state = '正常', true, null)) as normal_number,
                    count(if(inspection_state = '异常', true, null)) as abnormal_number,
                    count(if(inspection_state = '跳过', true, null)) as skip_number
        from record_inspection ri
        left join record_inspection_info rii on ri.inspection_id = rii.inspection_id
        <where>
            <if test="params.beginInspectionTime != null and params.beginInspectionTime != '' and params.endInspectionTime != null and params.endInspectionTime != ''">
                and inspection_time between #{params.beginInspectionTime} and #{params.endInspectionTime}
            </if>
            <if test="inspectionUser != null and inspectionUser != '' ">and inspection_user = #{inspectionUser}</if>
        </where>
        group by ri.inspection_id
    </select>

    <select id="selectRecordInspectionByInspectionId" parameterType="Long" resultMap="RecordInspectionResult">
        <include refid="selectRecordInspectionVo"/>
        where inspection_id = #{inspectionId}
    </select>

    <insert id="insertRecordInspection" parameterType="RecordInspection" useGeneratedKeys="true"
            keyProperty="inspectionId">
        insert into record_inspection
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inspectionTime != null">inspection_time,</if>
            <if test="inspectionUser != null">inspection_user,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inspectionTime != null">#{inspectionTime},</if>
            <if test="inspectionUser != null">#{inspectionUser},</if>
        </trim>
    </insert>

    <update id="updateRecordInspection" parameterType="RecordInspection">
        update record_inspection
        <trim prefix="SET" suffixOverrides=",">
            <if test="inspectionTime != null">inspection_time = #{inspectionTime},</if>
            <if test="inspectionUser != null">inspection_user = #{inspectionUser},</if>
        </trim>
        where inspection_id = #{inspectionId}
    </update>

    <delete id="deleteRecordInspectionByInspectionId" parameterType="Long">
        delete
        from record_inspection
        where inspection_id = #{inspectionId}
    </delete>

    <delete id="deleteRecordInspectionByInspectionIds" parameterType="String">
        delete from record_inspection where inspection_id in
        <foreach item="inspectionId" collection="array" open="(" separator="," close=")">
            #{inspectionId}
        </foreach>
    </delete>

</mapper>