<?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.ProductMapper">
    <!--插入数据-->
    <!--int insert(Product product);-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into PRODUCT (PRODUCT_NAME, PRODUCT_IDENTIFY, PRODUCT_DESCRIPTION, SURVEY_AREA, PRODUCT_IMAGE,
                             SEARCH_POLICY, DEADLINE, FOCUS_INFORMATION, REPORT_ID, CREATE_PERSON_NAME,
                             CREATE_PERSON_ID)
        values (#{productName}, #{productIdentify}, #{productDescription}, #{surveyArea}, #{productImage},
                #{searchPolicy}, #{deadLine}, #{focusInformation}, #{reportId}, #{createPersonName},
                #{createPersonId})
    </insert>

    <!--根据id修改数据-->
    <!--int update(Product product);-->
    <update id="update">
        update PRODUCT
        <set>
            <if test="productName != null">
                PRODUCT_NAME = #{productName},
            </if>
            <if test="productIdentify != null">
                PRODUCT_IDENTIFY = #{productIdentify},
            </if>
            <if test="productDescription != null">
                PRODUCT_DESCRIPTION = #{productDescription},
            </if>
            <if test="surveyArea != null">
                SURVEY_AREA = #{surveyArea},
            </if>
            <if test="productImage != null">
                PRODUCT_IMAGE = #{productImage},
            </if>
            <if test="searchPolicy != null">
                SEARCH_POLICY = #{searchPolicy},
            </if>
            DEADLINE = #{deadLine},
            <if test="focusInformation != null">
                FOCUS_INFORMATION = #{focusInformation},
            </if>
            <if test="reportId != null">
                REPORT_ID = #{reportId},
            </if>
            <if test="createPersonName != null">
                CREATE_PERSON_NAME = #{createPersonName},
            </if>
            <if test="createPersonId != null">
                CREATE_PERSON_ID = #{createPersonId},
            </if>
        </set>
        where ID = #{id}
    </update>

    <!--根据报告reportId统计数量-->
    <!--int countByReportId(Integer reportId);-->
    <select id="countByReportId" resultType="int">
        select count(*)
        from PRODUCT
        where REPORT_ID = #{reportId}
    </select>

    <!--根据报告reportId关联产品表、产品文件关联表、报告系统文件表查询产品和产品附件数据-->
    <!--ProductIncludeFilesVO getWholeByReportId(Integer reportId);-->
    <select id="getWholeByReportId" resultMap="WholeResultMap">
        select p.ID  p_ID,
               product_name,
               product_identify,
               product_description,
               survey_area,
               product_image,
               search_policy,
               deadline,
               focus_information,
               report_id,
               create_person_name,
               create_person_id,
               rf.ID rf_ID,
               name,
               address,
               zid,
               remark,
               update_time,
               uid,
               type,
               size,
               suffix,
               file_name
        from PRODUCT p
                 left join ASSO_PRODUCT_FILE apf on p.ID = apf.PRODUCT_ID
                 left join REPORT_FILE rf on apf.FILE_ID = rf.ID
        where REPORT_ID = #{reportId}
        order by rf_ID
    </select>

    <resultMap id="WholeResultMap" type="cn.cslg.report.common.model.vo.ProductIncludeFilesVO">
        <id column="p_ID" property="id"/>
        <result column="PRODUCT_NAME" property="productName"/>
        <result column="PRODUCT_IDENTIFY" property="productIdentify"/>
        <result column="PRODUCT_DESCRIPTION" property="productDescription"/>
        <result column="SURVEY_AREA" property="surveyArea"/>
        <result column="PRODUCT_IMAGE" property="productImage"/>
        <result column="SEARCH_POLICY" property="searchPolicy"/>
        <result column="DEADLINE" property="deadLine"/>
        <result column="FOCUS_INFORMATION" property="focusInformation"/>
        <result column="REPORT_ID" property="reportId"/>
        <result column="CREATE_PERSON_NAME" property="createPersonName"/>
        <result column="CREATE_PERSON_ID" property="createPersonId"/>
        <collection property="reportFiles" ofType="cn.cslg.report.common.model.vo.ReportFileStandardVO">
            <id column="rf_ID" property="id"/>
            <result column="NAME" property="name"/>
            <result column="ADDRESS" property="address"/>
            <result column="ZID" property="zid"/>
            <result column="REMARK" property="remark"/>
            <result column="UPDATE_TIME" property="updateTime"/>
            <result column="UID" property="uid"/>
            <result column="TYPE" property="type"/>
            <result column="SIZE" property="size"/>
            <result column="SUFFIX" property="suffix"/>
            <result column="FILE_NAME" property="fileName"/>
        </collection>
    </resultMap>

    <!--根据报告reportId删除数据-->
    <!--int deleteByReportId(Integer reportId);-->
    <delete id="deleteByReportId">
        delete
        from PRODUCT
        where REPORT_ID = #{reportId}
    </delete>

</mapper>