<?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.goafanti.common.dao.RdDetailsMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.RdDetails">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="rd_no" jdbcType="VARCHAR" property="rdNo" />
    <result column="ip_no" jdbcType="VARCHAR" property="ipNo" />
    <result column="rd_name" jdbcType="VARCHAR" property="rdName" />
    <result column="rd_start" jdbcType="TIMESTAMP" property="rdStart" />
    <result column="rd_end" jdbcType="TIMESTAMP" property="rdEnd" />
    <result column="technical_field" jdbcType="VARCHAR" property="technicalField" />
    <result column="technology_source" jdbcType="VARCHAR" property="technologySource" />
    <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
    <result column="recent_three_amount" jdbcType="DECIMAL" property="recentThreeAmount" />
    <result column="rd_objective" jdbcType="VARCHAR" property="rdObjective" />
    <result column="core_technology" jdbcType="VARCHAR" property="coreTechnology" />
    <result column="achieve_results" jdbcType="VARCHAR" property="achieveResults" />
    <result column="consultant_name" jdbcType="VARCHAR" property="consultantName" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="open_time" jdbcType="TIMESTAMP" property="openTime" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="browse_count" jdbcType="INTEGER" property="browseCount" />
    <result column="download_count" jdbcType="INTEGER" property="downloadCount" />
  </resultMap>
  <sql id="Base_Column_List">
    id, user_name, rd_no, ip_no, rd_name, rd_start, rd_end, technical_field, technology_source,
    total_amount, recent_three_amount, rd_objective, core_technology, achieve_results,
    consultant_name, `status`, open_time, create_time, aid, browse_count, download_count
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from rd_details
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from rd_details
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.RdDetails" useGeneratedKeys="true">
    insert into rd_details (user_name, rd_no, ip_no,
      rd_name, rd_start, rd_end,
      technical_field, technology_source, total_amount,
      recent_three_amount, rd_objective, core_technology,
      achieve_results, consultant_name, `status`,
      open_time, create_time, aid,
      browse_count, download_count)
    values (#{userName,jdbcType=VARCHAR}, #{rdNo,jdbcType=VARCHAR}, #{ipNo,jdbcType=VARCHAR},
      #{rdName,jdbcType=VARCHAR}, #{rdStart,jdbcType=TIMESTAMP}, #{rdEnd,jdbcType=TIMESTAMP},
      #{technicalField,jdbcType=VARCHAR}, #{technologySource,jdbcType=VARCHAR}, #{totalAmount,jdbcType=DECIMAL},
      #{recentThreeAmount,jdbcType=DECIMAL}, #{rdObjective,jdbcType=VARCHAR}, #{coreTechnology,jdbcType=VARCHAR},
      #{achieveResults,jdbcType=VARCHAR}, #{consultantName,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
      #{openTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP}, #{aid,jdbcType=VARCHAR},
      #{browseCount,jdbcType=INTEGER}, #{downloadCount,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.RdDetails" useGeneratedKeys="true">
    insert into rd_details
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userName != null">
        user_name,
      </if>
      <if test="rdNo != null">
        rd_no,
      </if>
      <if test="ipNo != null">
        ip_no,
      </if>
      <if test="rdName != null">
        rd_name,
      </if>
      <if test="rdStart != null">
        rd_start,
      </if>
      <if test="rdEnd != null">
        rd_end,
      </if>
      <if test="technicalField != null">
        technical_field,
      </if>
      <if test="technologySource != null">
        technology_source,
      </if>
      <if test="totalAmount != null">
        total_amount,
      </if>
      <if test="recentThreeAmount != null">
        recent_three_amount,
      </if>
      <if test="rdObjective != null">
        rd_objective,
      </if>
      <if test="coreTechnology != null">
        core_technology,
      </if>
      <if test="achieveResults != null">
        achieve_results,
      </if>
      <if test="consultantName != null">
        consultant_name,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="openTime != null">
        open_time,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="aid != null">
        aid,
      </if>
      <if test="browseCount != null">
        browse_count,
      </if>
      <if test="downloadCount != null">
        download_count,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="rdNo != null">
        #{rdNo,jdbcType=VARCHAR},
      </if>
      <if test="ipNo != null">
        #{ipNo,jdbcType=VARCHAR},
      </if>
      <if test="rdName != null">
        #{rdName,jdbcType=VARCHAR},
      </if>
      <if test="rdStart != null">
        #{rdStart,jdbcType=TIMESTAMP},
      </if>
      <if test="rdEnd != null">
        #{rdEnd,jdbcType=TIMESTAMP},
      </if>
      <if test="technicalField != null">
        #{technicalField,jdbcType=VARCHAR},
      </if>
      <if test="technologySource != null">
        #{technologySource,jdbcType=VARCHAR},
      </if>
      <if test="totalAmount != null">
        #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="recentThreeAmount != null">
        #{recentThreeAmount,jdbcType=DECIMAL},
      </if>
      <if test="rdObjective != null">
        #{rdObjective,jdbcType=VARCHAR},
      </if>
      <if test="coreTechnology != null">
        #{coreTechnology,jdbcType=VARCHAR},
      </if>
      <if test="achieveResults != null">
        #{achieveResults,jdbcType=VARCHAR},
      </if>
      <if test="consultantName != null">
        #{consultantName,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="openTime != null">
        #{openTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="browseCount != null">
        #{browseCount,jdbcType=INTEGER},
      </if>
      <if test="downloadCount != null">
        #{downloadCount,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.RdDetails">
    update rd_details
    <set>
      <if test="userName != null">
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="rdNo != null">
        rd_no = #{rdNo,jdbcType=VARCHAR},
      </if>
      <if test="ipNo != null">
        ip_no = #{ipNo,jdbcType=VARCHAR},
      </if>
      <if test="rdName != null">
        rd_name = #{rdName,jdbcType=VARCHAR},
      </if>
      <if test="rdStart != null">
        rd_start = #{rdStart,jdbcType=TIMESTAMP},
      </if>
      <if test="rdEnd != null">
        rd_end = #{rdEnd,jdbcType=TIMESTAMP},
      </if>
      <if test="technicalField != null">
        technical_field = #{technicalField,jdbcType=VARCHAR},
      </if>
      <if test="technologySource != null">
        technology_source = #{technologySource,jdbcType=VARCHAR},
      </if>
      <if test="totalAmount != null">
        total_amount = #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="recentThreeAmount != null">
        recent_three_amount = #{recentThreeAmount,jdbcType=DECIMAL},
      </if>
      <if test="rdObjective != null">
        rd_objective = #{rdObjective,jdbcType=VARCHAR},
      </if>
      <if test="coreTechnology != null">
        core_technology = #{coreTechnology,jdbcType=VARCHAR},
      </if>
      <if test="achieveResults != null">
        achieve_results = #{achieveResults,jdbcType=VARCHAR},
      </if>
      <if test="consultantName != null">
        consultant_name = #{consultantName,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="openTime != null">
        open_time = #{openTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="browseCount != null">
        browse_count = #{browseCount,jdbcType=INTEGER},
      </if>
      <if test="downloadCount != null">
        download_count = #{downloadCount,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.RdDetails">
    update rd_details
    set user_name = #{userName,jdbcType=VARCHAR},
      rd_no = #{rdNo,jdbcType=VARCHAR},
      ip_no = #{ipNo,jdbcType=VARCHAR},
      rd_name = #{rdName,jdbcType=VARCHAR},
      rd_start = #{rdStart,jdbcType=TIMESTAMP},
      rd_end = #{rdEnd,jdbcType=TIMESTAMP},
      technical_field = #{technicalField,jdbcType=VARCHAR},
      technology_source = #{technologySource,jdbcType=VARCHAR},
      total_amount = #{totalAmount,jdbcType=DECIMAL},
      recent_three_amount = #{recentThreeAmount,jdbcType=DECIMAL},
      rd_objective = #{rdObjective,jdbcType=VARCHAR},
      core_technology = #{coreTechnology,jdbcType=VARCHAR},
      achieve_results = #{achieveResults,jdbcType=VARCHAR},
      consultant_name = #{consultantName,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=INTEGER},
      open_time = #{openTime,jdbcType=TIMESTAMP},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      aid = #{aid,jdbcType=VARCHAR},
      browse_count = #{browseCount,jdbcType=INTEGER},
      download_count = #{downloadCount,jdbcType=INTEGER}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="checkRdNo" resultType="java.lang.Integer">
    select count(*)
    from rd_details
    where rd_no =#{rdNo}
    <if test="id !=null">
      and id !=#{id}
    </if>
  </select>
  <sql id="details_List">
     a.id, a.user_name userName, a.rd_no rdNo, a.ip_no ipNo, a.rd_name rdName,a.technical_field technicalField,
    a.technology_source technologySource, a.total_amount totalAmount, a.recent_three_amount recentThreeAmount,a.aid,
     a.rd_objective rdObjective, a.core_technology coreTechnology, a.achieve_results achieveResults,a.consultant_name consultantName,
    a.status,a.browse_count browseCount ,a.download_count downloadCount, date_format(a.rd_start, '%Y-%m-%d') rdStartStr,
     date_format(a.rd_end , '%Y-%m-%d') rdEndStr,date_format(a.open_time, '%Y-%m-%d %H:%i:%s')openTimeStr,
     date_format(a.create_time, '%Y-%m-%d %H:%i:%s') createTimeStr
  </sql>
  <select id="details" resultType="com.goafanti.RD.bo.OutRdDetails">
    select
    <include refid="details_List" />
    from rd_details a
    where a.id =#{id}
  </select>

  <select id="RdDetailsList" resultType="com.goafanti.RD.bo.OutExcelRdDetails">
    select
    <include refid="details_List" />
    from rd_details a
    where 1=1
    <if test="RdStart !=null and RdEnd !=null">
      and a.rd_start  <![CDATA[ >= ]]> #{RdStart} and a.rd_end <![CDATA[ <= ]]> #{RdEnd}
    </if>
    <if test="RdName !=null ">
      and a.rd_name like concat('%',#{RdName},'%')
    </if>
    <if test="RdNo !=null ">
      and a.rd_no like concat('%',#{RdNo},'%')
    </if>
    <if test="aid !=null ">
      and a.aid =#{aid}
    </if>
    <if test="page_sql !=null ">
        ${page_sql}
    </if>
  </select>
  <select id="RdDetailsCount" resultType="java.lang.Integer">
    select
    count(*)
    from rd_details a
    where 1=1
    <if test="RdStart !=null and RdEnd !=null">
      and a.rd_start  <![CDATA[ >=  ]]> #{RdStart} and a.rd_end <![CDATA[ <= ]]> #{RdEnd}
    </if>
    <if test="RdName !=null ">
      and a.rd_name like concat('%',#{RdName},'%')
    </if>
    <if test="RdNo !=null ">
      and a.rd_no like concat('%',#{RdNo},'%')
    </if>
    <if test="aid !=null ">
      and a.aid =#{aid}
    </if>
  </select>
  <insert id="insertBatch">
    insert into rd_details (user_name, rd_no, ip_no,
                            rd_name, rd_start, rd_end,
                            technical_field, technology_source, total_amount,
                            recent_three_amount, rd_objective, core_technology,
                            achieve_results, consultant_name, `status`,
                            open_time, create_time, aid
    ) values
    <foreach item="item" index="index" collection="list" separator=",">
      (#{item.userName,jdbcType=VARCHAR}, #{item.rdNo,jdbcType=VARCHAR}, #{item.ipNo,jdbcType=VARCHAR},
      #{item.rdName,jdbcType=VARCHAR}, #{item.rdStart,jdbcType=TIMESTAMP}, #{item.rdEnd,jdbcType=TIMESTAMP},
      #{item.technicalField,jdbcType=VARCHAR}, #{item.technologySource,jdbcType=VARCHAR}, #{item.totalAmount,jdbcType=DECIMAL},
      #{item.recentThreeAmount,jdbcType=DECIMAL}, #{item.rdObjective,jdbcType=VARCHAR}, #{item.coreTechnology,jdbcType=VARCHAR},
      #{item.achieveResults,jdbcType=VARCHAR}, #{item.consultantName,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER},
      #{item.openTime,jdbcType=TIMESTAMP}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.aid,jdbcType=VARCHAR}
      )
    </foreach>
  </insert>

  <update id="updateBrowseOrDownload">
    update rd_details
    <set>
      <if test="type==0">
        browse_count = browse_count+1
      </if>
      <if test="type==1">
        download_count = download_count+1
      </if>
      where id = #{id,jdbcType=BIGINT}
    </set>
  </update>

</mapper>
