<?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.system.mapper.ReportDetailMapper">

    <resultMap type="com.ruoyi.system.domain.ReportDetail" id="ReportDetailResult">
        <result property="reportId" column="report_id"/>
        <result property="reportNumber" column="report_number"/>
        <result property="inspectId" column="inspect_id"/>
        <result property="sampleName" column="sample_name"/>
        <result property="inspectName" column="inspect_name"/>
        <result property="entrustName" column="entrust_name"/>
        <result property="isQualify" column="is_qualify"/>
        <result property="reportUrl" column="report_url"/>
        <result property="qrPath"    column="qr_path"    />
        <result property="isDel" column="is_del"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="phonenumber" column="phonenumber"/>
    </resultMap>

    <sql id="selectReportDetailVo">
        select report_id, report_number, inspect_id, sample_name, inspect_name, entrust_name, is_qualify, report_url,qr_path, is_del, create_by, create_time, update_by, update_time, remark from report_detail
    </sql>

    <select id="selectReportDetailList" parameterType="ReportDetail" resultMap="ReportDetailResult">
        <include refid="selectReportDetailVo"/>
        <where>
        is_del = 'N'
            <if test="reportNumber != null  and reportNumber != ''">and report_number = #{reportNumber}</if>
            <if test="inspectId != null ">and inspect_id = #{inspectId}</if>
            <if test="sampleName != null  and sampleName != ''">and sample_name like concat('%', #{sampleName}, '%')
            </if>
            <if test="inspectName != null  and inspectName != ''">and inspect_name like concat('%', #{inspectName},
                '%')
            </if>
            <if test="entrustName != null  and entrustName != ''">and entrust_name like concat('%', #{entrustName},
                '%')
            </if>
            <if test="isQualify != null  and isQualify != ''">and is_qualify = #{isQualify}</if>
            <if test="reportUrl != null  and reportUrl != ''">and report_url = #{reportUrl}</if>
        </where>
        order by create_time DESC
    </select>

    <select id="selectReportDetailByReportId" parameterType="Long" resultMap="ReportDetailResult">
        <include refid="selectReportDetailVo"/>
        where report_id = #{reportId} and is_del = 'N'
    </select>

    <insert id="insertReportDetail" parameterType="ReportDetail" useGeneratedKeys="true" keyProperty="reportId">
        insert into report_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reportNumber != null and reportNumber != ''">report_number,</if>
            <if test="inspectId != null">inspect_id,</if>
            <if test="sampleName != null and sampleName != ''">sample_name,</if>
            <if test="inspectName != null">inspect_name,</if>
            <if test="entrustName != null">entrust_name,</if>
            <if test="isQualify != null">is_qualify,</if>
            <if test="reportUrl != null">report_url,</if>
            <if test="qrPath != null">qr_path,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="reportNumber != null and reportNumber != ''">#{reportNumber},</if>
            <if test="inspectId != null">#{inspectId},</if>
            <if test="sampleName != null and sampleName != ''">#{sampleName},</if>
            <if test="inspectName != null">#{inspectName},</if>
            <if test="entrustName != null">#{entrustName},</if>
            <if test="isQualify != null">#{isQualify},</if>
            <if test="reportUrl != null">#{reportUrl},</if>
            <if test="qrPath != null">#{qrPath},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateReportDetail" parameterType="ReportDetail">
        update report_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="reportNumber != null and reportNumber != ''">report_number = #{reportNumber},</if>
            <if test="inspectId != null">inspect_id = #{inspectId},</if>
            <if test="sampleName != null and sampleName != ''">sample_name = #{sampleName},</if>
            <if test="inspectName != null">inspect_name = #{inspectName},</if>
            <if test="entrustName != null">entrust_name = #{entrustName},</if>
            <if test="isQualify != null">is_qualify = #{isQualify},</if>
            <if test="reportUrl != null">report_url = #{reportUrl},</if>
            <if test="qrPath != null">qr_path = #{qrPath},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where report_id = #{reportId}
    </update>

    <delete id="deleteReportDetailByReportId" parameterType="Long">
        delete from report_detail where report_id = #{reportId}
    </delete>

    <update id="deleteReportDetailByReportIds" parameterType="String">
        update  report_detail set is_del = 'Y' where report_id in
        <foreach item="reportId" collection="array" open="(" separator="," close=")">
            #{reportId}
        </foreach>
    </update>

    <select id="checkReportNumberUnique" resultMap="ReportDetailResult">
        <include refid="selectReportDetailVo"/>
        where report_number=#{reportNumber} and is_del = 'N' limit 1
    </select>

    <select id="selectReportDetailByReportNumber" resultMap="ReportDetailResult">
     select  r.report_number,  r.sample_name, r.inspect_name, r.entrust_name, r.is_qualify, r.report_url from report_detail r
        where r.report_number=#{reportNumber} and r.is_del = 'N'  limit 1
    </select>

    <select id="selectReportDetailByReportNumberDetail" resultMap="ReportDetailResult">
     select  r.report_number,  r.sample_name, r.inspect_name, r.entrust_name, r.is_qualify, r.report_url,s.phonenumber from report_detail r left join sys_user s on r.create_by = s.create_by
        where r.report_number=#{reportNumber} and r.is_del = 'N' and s.status = '0' and s.del_flag = '0' limit 1
    </select>

    <select id="selectReportDetailCount" parameterType="Date" resultType="int">
        select ifnull(count(1),0) as num from report_detail
        <where>
            is_del = 'N'
            <if test="today != null">and DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT(#{today},'%Y-%m-%d')</if>
            <if test="startTime != null">and DATE_FORMAT(create_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{startTime},'%Y-%m-%d')</if>
            <if test="endTime != null">and DATE_FORMAT(create_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{endTime},'%Y-%m-%d')</if>
        </where>
    </select>
</mapper>