<?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.pcloud.book.book.dao.impl.BookEdtionDaoImpl">
  <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.BookEdition">
    <constructor>
      <idArg column="id" javaType="java.lang.Long" jdbcType="BIGINT" />
      <arg column="book_id" javaType="java.lang.Long" jdbcType="BIGINT" />
      <arg column="edition_year" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="edition" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP" />
      <arg column="update_time" javaType="java.util.Date" jdbcType="TIMESTAMP" />
      <arg column="update_user" javaType="java.lang.Integer" jdbcType="INTEGER" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    id, book_id, edition_year, edition, create_time, update_time, update_user
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from book_edition
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from book_edition
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.pcloud.book.book.entity.BookEdition">
    insert into book_edition (book_id, edition_year,
      edition, create_time, update_time, 
      update_user)
    values (#{bookId,jdbcType=BIGINT}, #{editionYear,jdbcType=VARCHAR},
      #{edition,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{updateUser,jdbcType=INTEGER})
      ON DUPLICATE KEY UPDATE
        edition_year = ifnull(VALUES(edition_year),edition_year),
        edition = ifnull(VALUES(edition),edition),
        update_user = ifnull(VALUES(update_user),update_user),
        update_time = NOW()
  </insert>

  <insert id="batchInsert" parameterType="list">
    insert into edition_record (book_id, edition_year,
    edition, create_time, update_time,update_user
    )
    values
    <foreach collection="list" index="index" item="item" separator=",">
      (#{item.bookId,jdbcType=BIGINT}, #{item.editionYear,jdbcType=VARCHAR},
      #{item.edition,jdbcType=INTEGER}, now(),now(),#{item.updateUser}
      )
    </foreach>
    ON DUPLICATE KEY UPDATE
    edition_year = ifnull(VALUES(edition_year),edition_year),
    edition = ifnull(VALUES(edition),edition),
    update_user = ifnull(VALUES(update_user),update_user),
    update_time = NOW()
  </insert>

  <insert id="insertSelective" parameterType="com.pcloud.book.book.entity.BookEdition">
    insert into book_edition
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="bookId != null">
        book_id,
      </if>
      <if test="editionYear != null">
        edition_year,
      </if>
      <if test="edition != null">
        edition,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="updateUser != null">
        update_user,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="bookId != null">
        #{bookId,jdbcType=INTEGER},
      </if>
      <if test="editionYear != null">
        #{editionYear,jdbcType=VARCHAR},
      </if>
      <if test="edition != null">
        #{edition,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        #{updateUser,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.book.entity.BookEdition">
    update book_edition
    <set>
      <if test="bookId != null">
        book_id = #{bookId,jdbcType=INTEGER},
      </if>
      <if test="editionYear != null">
        edition_year = #{editionYear,jdbcType=VARCHAR},
      </if>
      <if test="edition != null">
        edition = #{edition,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        update_user = #{updateUser,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.book.entity.BookEdition">
    update book_edition
    set book_id = #{bookId,jdbcType=BIGINT},
      edition_year = #{editionYear,jdbcType=VARCHAR},
      edition = #{edition,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=INTEGER}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="projectEditionAnalysis" parameterType="Long" resultType="com.pcloud.book.book.dto.EditionAnalysisListVO">
       select
       edition edition,
       edition_year editionDate,
       ifnull(count(1),0) projectCount
       from book_edition
       where update_user=#{adviserId}
       GROUP BY edition,edition_year
       ORDER BY edition_year
  </select>

  <select id="projectCountSorted" parameterType="Long" resultType="com.pcloud.book.book.dto.EditionAnalysisListVO">
       select
       edition edition,
       edition_year editionDate,
       ifnull(count(1),0) projectCount
       from book_edition
       where update_user=#{adviserId}
       GROUP BY edition_year
       ORDER BY projectCount desc
  </select>

</mapper>