<?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.oa.mapper.CmcSafeReportMapper">

    <resultMap type="CmcSafeReport" id="CmcSafeReportResult">
        <result property="safeReportId"    column="safe_report_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="reportDate"    column="report_date"    />
        <result property="member"    column="member"    />
        <result property="employee"    column="employee"    />
        <result property="content"    column="content"    />
        <result property="reporter"    column="reporter"    />
        <association property="reportUser"    javaType="SysUser"         resultMap="ReportUserResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
    </resultMap>

    <resultMap type="SysUser" id="ReportUserResult">
        <result property="member"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <resultMap type="CmcProject" id="CmcProjectResult">
        <result property="projectId"    column="project_id"    />
        <result property="projectNumber"    column="project_number"    />
        <result property="projectName"    column="project_name"    />
    </resultMap>

    <sql id="selectCmcSafeReportVo">
        select sr.safe_report_id, sr.project_id, p.project_number, p.project_name, sr.report_date, sr.member, sr.employee, sr.content, sr.reporter, u.nick_name from cmc_safe_report as sr
        left join sys_user as u on sr.reporter = u.user_id
        left join cmc_project as p on sr.project_id = p.project_id
    </sql>

    <select id="selectCmcSafeReportList" parameterType="CmcSafeReport" resultMap="CmcSafeReportResult">
        <include refid="selectCmcSafeReportVo"/>
        <where>
            <if test="projectId != null  and projectId != ''"> and sr.project_id = #{projectId}</if>
            <if test="reportDate != null "> and sr.report_date = #{reportDate}</if>
            <if test="member != null  and member != ''"> and (find_in_set(#{member}, sr.member) or sr.reporter = #{member})</if>
            <if test="employee != null  and employee != ''"> and sr.employee like concat('%', #{employee}, '%')</if>
            <if test="content != null  and content != ''"> and sr.content = #{content}</if>
            <if test="reporter != null "> and sr.reporter = #{reporter}</if>
        </where>
        order by sr.report_date desc
    </select>

    <select id="selectCmcSafeReportBySafeReportId" parameterType="Integer" resultMap="CmcSafeReportResult">
        <include refid="selectCmcSafeReportVo"/>
        where sr.safe_report_id = #{safeReportId}
    </select>

    <insert id="insertCmcSafeReport" parameterType="CmcSafeReport" useGeneratedKeys="true" keyProperty="safeReportId">
        insert into cmc_safe_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="reportDate != null">report_date,</if>
            <if test="member != null">member,</if>
            <if test="employee != null">employee,</if>
            <if test="content != null">content,</if>
            <if test="reporter != null">reporter,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="reportDate != null">#{reportDate},</if>
            <if test="member != null">#{member},</if>
            <if test="employee != null">#{employee},</if>
            <if test="content != null">#{content},</if>
            <if test="reporter != null">#{reporter},</if>
        </trim>
    </insert>

    <update id="updateCmcSafeReport" parameterType="CmcSafeReport">
        update cmc_safe_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="reportDate != null">report_date = #{reportDate},</if>
            <if test="member != null">member = #{member},</if>
            <if test="employee != null">employee = #{employee},</if>
            <if test="content != null">content = #{content},</if>
            <if test="reporter != null">reporter = #{reporter},</if>
        </trim>
        where safe_report_id = #{safeReportId}
    </update>

    <delete id="deleteCmcSafeReportBySafeReportId" parameterType="Integer">
        delete from cmc_safe_report where safe_report_id = #{safeReportId}
    </delete>

    <delete id="deleteCmcSafeReportBySafeReportIds" parameterType="Integer">
        delete from cmc_safe_report where safe_report_id in
        <foreach item="safeReportId" collection="array" open="(" separator="," close=")">
            #{safeReportId}
        </foreach>
    </delete>
</mapper>