<?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.srs.diagnostic.mapper.DiaReportsMapper">

    <resultMap type="DiaReports" id="DiaReportsResult">
        <result property="reportId"    column="reportId"    />
        <result property="name"    column="name"    />
        <result property="parentId"    column="parentId"    />
        <result property="status"    column="status"    />
        <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="sort" column="sort"       />
        <result property="isMoudId"    column="isMoudId"    />
    </resultMap>

    <sql id="selectDiaReportsVo">
        select reportId, name, parentId, status, create_by, create_time, update_by, update_time,sort,isMoudId from dia_reports
    </sql>

    <select id="selectDiaReportsList" parameterType="DiaReports" resultMap="DiaReportsResult">
        <include refid="selectDiaReportsVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="parentId != null "> and parentId = #{parentId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by sort asc
    </select>

    <select id="selectDiaReportsByReportId" parameterType="Integer" resultMap="DiaReportsResult">
        <include refid="selectDiaReportsVo"/>
        where reportId = #{reportId}
    </select>

    <select id="getNameList" resultType="com.srs.diagnostic.domain.DiaReports">
        select reportId,name from  dia_reports where parentId=0 and status=0
    </select>

    <select id="xlkList" resultMap="DiaReportsResult">
        select reportId,name from dia_reports
        where parentId = 0
    </select>
    <insert id="insertDiaReports" parameterType="DiaReports" useGeneratedKeys="true" keyProperty="reportId">
        insert into dia_reports
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="parentId != null">parentId,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="sort != null">sort,</if>
            <if test="isMoudId != null">isMoudId,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="sort != null">#{sort},</if>
            <if test="isMoudId != null">#{isMoudId},</if>
         </trim>
    </insert>
    <select id="getAllReports" resultType="com.srs.diagnostic.domain.DiaReports">
          select reportId,name,parentId,status,create_by,create_time,update_by,update_time,sort from dia_reports
    </select>
    <insert id="insertReportItem" parameterType="DiaReportItems" useGeneratedKeys="true" keyProperty="itemId">
        insert into dia_report_items
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reportId != null">reportId,</if>
            <if test="dimension != null">dimension,</if>
            <if test="controlPoint != null">control_point,</if>
            <if test="indicatorName != null">indicator_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="reportId != null">#{reportId},</if>
            <if test="dimension != null">#{dimension},</if>
            <if test="controlPoint != null">#{controlPoint},</if>
            <if test="indicatorName != null">#{indicatorName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>
    <update id="updateDiaReports" parameterType="DiaReports">
        update dia_reports
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="parentId != null">parentId = #{parentId},</if>
            <if test="status != null">status = #{status},</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="sort != null">sort = #{sort},</if>
            <if test="isMoudId != null">isMoudId = #{isMoudId},</if>
        </trim>
        where reportId = #{reportId}
    </update>

    <delete id="deleteDiaReportsByReportId" parameterType="Integer">
        delete from dia_reports where reportId = #{reportId}
    </delete>

    <delete id="deleteDiaReportsByReportIds" parameterType="String">
        delete from dia_reports where reportId in
        <foreach item="reportId" collection="array" open="(" separator="," close=")">
            #{reportId}
        </foreach>
    </delete>
        <select id="selectAllReports" resultType="com.srs.diagnostic.domain.DiaReports" parameterType="Integer">
            SELECT
                reportId,
                name,
                parentId,
                status,
                create_by as createBy,
                create_time as createTime,
                update_by as updateBy,
                update_time as updateTime,
                sort,
                isMoudId
            FROM dia_reports
            where isMoudId=#{reportId}
            ORDER BY parentId, sort
        </select>
        <select id="selectChildrenByParentId" resultType="com.srs.diagnostic.domain.DiaReports">
            SELECT
                reportId,
                name,
                parentId,
                status,
                create_by as createBy,
                create_time as createTime,
                update_by as updateBy,
                update_time as updateTime,
                sort
            FROM dia_reports
            WHERE parentId = #{parentId}
            ORDER BY sort
        </select>
    <select id="selectParentById" parameterType="Integer" resultType="com.srs.diagnostic.domain.DiaReports">
        SELECT
            reportId
        FROM dia_reports
        WHERE reportId = #{reportId} and parentId=0
    </select>
</mapper>
