<?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="cn.cslg.report.mapper.FollowUpMapper">
    <!--插入数据-->
    <!--int add(FollowUp followUp);-->
    <insert id="add" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO FOLLOW_UP(REPORT_ID, PARENT_ID, FOLLOW_UP_NAME, REMARK, TIME_LIMIT,
                              AGENT, ASSIST_PERSON, FINISH_TIME, STATUS, CONCLUSION, CREATE_PERSON_ID,
                              CREATE_PERSON_NAME)
        VALUES
            (#{reportId}, #{parentId}, #{followUpName}, #{remark},
             #{timeLimit}, #{agent}, #{assistPerson}, #{finishTime}, #{status},
             #{conclusion}, #{createPersonId}, #{createPersonName})
    </insert>
    <!--根据id修改数据-->
    <!--int update(FollowUp followUp);-->
    <update id="update">
        UPDATE FOLLOW_UP
        <set>
            <if test="followUpName != null">
                FOLLOW_UP_NAME = #{followUpName},
            </if>
            <if test="remark != null">
                REMARK = #{remark},
            </if>
            <if test="timeLimit != null">
                TIME_LIMIT = #{timeLimit},
            </if>
            <if test="agent != null">
                AGENT = #{agent},
            </if>
            <if test="assistPerson != null">
                ASSIST_PERSON = #{assistPerson},
            </if>
            <if test="finishTime != null">
                FINISH_TIME = #{finishTime},
            </if>
            <if test="status != null">
                STATUS = #{status},
            </if>
            <if test="conclusion != null">
                CONCLUSION = #{conclusion},
            </if>
        </set>
        WHERE ID = #{id}
    </update>

    <!--根据报告id查询后续事项-->
    <!--List<FollowUpVO> query(Integer reportId);-->
    <resultMap id="queryMap" type="cn.cslg.report.common.model.vo.FollowUpVO">
        <id column="ID" property="id"/>
        <result column="REPORT_ID" property="reportId"/>
        <result column="PARENT_ID" property="parentId"/>
        <result column="FOLLOW_UP_NAME" property="followUpName"/>
        <result column="REMARK" property="remark"/>
        <result column="TIME_LIMIT" property="timeLimit"/>
        <result column="AGENT" property="agent"/>
        <result column="ASSIST_PERSON" property="assistPerson"/>
        <result column="FINISH_TIME" property="finishTime"/>
        <result column="STATUS" property="status"/>
        <result column="CONCLUSION" property="conclusion"/>
        <result column="CREATE_PERSON_ID" property="createPersonId"/>
        <result column="CREATE_PERSON_NAME" property="createPersonName"/>
        <result column="CREATE_TIME" property="createTime"/>
        <collection property="filesVOs" resultMap="FilesVOsResultMap"/>
    </resultMap>
    <resultMap id="FilesVOsResultMap" type="cn.cslg.report.common.model.vo.FilesVO">
        <result column="FILE_ID" property="fileId"/>
        <result column="NAME" property="name"/>
        <result column="ADDRESS" property="url"/>
        <result column="ZID" property="zId"/>
        <result column="FILEREMARK" property="remark"/>
        <result column="UPDATE_TIME" property="updateTime"/>
        <result column="UID" property="uId"/>
        <result column="TYPE" property="type"/>
        <result column="SUFFIX" property="suffix"/>
        <result column="FILE_NAME" property="fileName"/>
    </resultMap>

    <!--    <select id="query" resultMap="queryMap">-->
    <!--        select id,-->
    <!--               report_id,-->
    <!--               parent_id,-->
    <!--               follow_up_name,-->
    <!--               remark,-->
    <!--               time_limit,-->
    <!--               agent,-->
    <!--               assist_person,-->
    <!--               finish_time,-->
    <!--               status,-->
    <!--               conclusion,-->
    <!--               create_person_id,-->
    <!--               create_person_name,-->
    <!--               create_time-->
    <!--        from follow_up-->
    <!--        where report_id = #{reportId}-->
    <!--    </select>-->

    <select id="query" resultMap="queryMap">
        SELECT a.ID, a.REPORT_ID, a.PARENT_ID, a.FOLLOW_UP_NAME,a.REMARK,a.TIME_LIMIT,a.AGENT,a.ASSIST_PERSON,a.FINISH_TIME,a.STATUS, a.CONCLUSION,
               a.CREATE_PERSON_ID, a.CREATE_PERSON_NAME, a.CREATE_TIME, d.FILE_ID, d.NAME, d.ADDRESS, d.ZID,d.FILEREMARK, d.UPDATE_TIME, d.UID,d.TYPE, d.SIZE, d.SUFFIX, d.FILE_NAME
        FROM FOLLOW_UP a LEFT JOIN (SELECT b.FOLLOW_UP_ID, b.FILE_ID, c.ID, c.NAME, c.ADDRESS, c.ZID, c.REMARK AS FILEREMARK, c.UPDATE_TIME, c.UID, c.TYPE, c.SIZE, c.SUFFIX, c.FILE_NAME
                                    FROM ASSO_FOLLOW_UP_FILE b LEFT JOIN REPORT_FILE c ON b.FILE_ID = c.ID) d
                                    ON a.ID = d.FOLLOW_UP_ID
        WHERE a.REPORT_ID = #{reportId} ORDER BY CREATE_TIME DESC
    </select>

    <!--根据报告id统计后续事项数量-->
    <!--int countByReportId();-->
    <select id="countByReportId" resultType="int">
        SELECT count(*)
        FROM FOLLOW_UP
        WHERE REPORT_ID = #{reportId}
    </select>

    <!--根据ids删除数据-->
    <!--int delete(List<Integer> ids);-->
    <delete id="delete" parameterType="java.util.List">
        DELETE
        FROM FOLLOW_UP
        WHERE ID IN
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!--根据后续事项id和文件id删除数据-->
    <!--int deleteAssoId(Integer followUpId, List<Integer> fileIds);-->
    <delete id="deleteAssoId">
        DELETE
        FROM ASSO_FOLLOW_UP_FILE
        WHERE FOLLOW_UP_ID = #{followUpId}
        AND FILE_ID IN
        <foreach collection="fileIds" item="fileId" index="index" open="(" close=")" separator=",">
            #{fileId}
        </foreach>
    </delete>


    <!--根据报告id统计后续事项数量-->
    <!--List<Integer> queryFileIdByFollowUpId(Integer followUpId);-->
    <select id="queryFileIdByFollowUpId" resultType="Integer">
        SELECT FILE_ID FROM ASSO_FOLLOW_UP_FILE WHERE FOLLOW_UP_ID = #{followUpId};
    </select>


    <!--插入数据-->
    <!--int addAssoIds(AssoFollowUpFile assoFollowUpFile);-->
    <insert id="addAssoIds" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ASSO_FOLLOW_UP_FILE(FOLLOW_UP_ID, FILE_ID, FILE_TYPE)
        VALUES
            (#{followUpId}, #{fileId}, #{fileType})
    </insert>

</mapper>