<?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.record.dao.impl.BookBrowseRecordDAOImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.record.entity.BookBrowseRecord" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="book_adviser_id" property="bookAdviserId" jdbcType="BIGINT" />
    <result column="isbn" property="isbn" jdbcType="VARCHAR" />
    <result column="user_id" property="userId" jdbcType="BIGINT" />
    <result column="province" property="province" jdbcType="VARCHAR" />
    <result column="province_code" property="provinceCode" jdbcType="VARCHAR" />
    <result column="city" property="city" jdbcType="VARCHAR" />
    <result column="city_code" property="cityCode" jdbcType="VARCHAR" />
    <result column="district" property="district" jdbcType="VARCHAR" />
    <result column="district_code" property="districtCode" jdbcType="VARCHAR" />
    <result column="gra_Label__id" property="graLabelId" jdbcType="BIGINT" />
    <result column="sub_Label_Id" property="subLabelId" jdbcType="BIGINT" />
    <result column="first_Classify_Id" property="firstClassifyId" jdbcType="BIGINT" />
    <result column="second_Classify_Id" property="secondClassifyId" jdbcType="BIGINT" />
    <result column="third_Classify_Id" property="thirdClassifyId" jdbcType="BIGINT" />
    <result column="book_classify_id" property="bookClassifyId" jdbcType="BIGINT" />
    <result column="ip" property="ip" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, book_adviser_id, isbn, user_id, province, province_code, city, city_code, district, 
    district_code, gra_Label__id, sub_Label_Id, first_Classify_Id, second_Classify_Id, third_Classify_Id,
    book_classify_id, ip, create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from book_browse_record
    where id = #{id,jdbcType=BIGINT}
  </select>
  <select id="getBookCount4Country" resultType="com.pcloud.book.record.entity.BookMapStatistics" parameterType="map">
    select
    count(distinct book_adviser_id) bookCount,
    province,
    province_code provinceCode,
    city,
    city_code cityCode
    from
    book_browse_record
    where 1=1
    <if test="adviserIds != null and adviserIds.size>0">
      AND adviser_id in 
      <foreach collection="adviserIds" item="item" index="index" separator="," close=")" open="(">
        ${item}
      </foreach>
    </if>
    <choose>
      <when test="regionCode != null">
        AND province_code = #{regionCode}
        group by city_code
        order by bookCount desc, city_code asc
      </when>
      <otherwise>
        group by province_code
        order by bookCount desc, province_code asc
      </otherwise>
    </choose>
  </select>
  <select id="getAreaStatistics" resultType="com.pcloud.book.record.entity.AreaStatistics" parameterType="map">
    select
    (case when #{areaCode} = 1 then province_code when #{areaCode} = 2 then city_code  else 0 end) regionCode,
    (case when #{areaCode} = 1 then province when #{areaCode} = 2 then city when #{areaCode} = 0  then '全国' end) regionName ,
    count(distinct book_adviser_id)  bookCount
    from  book_browse_record b
    <where>
      <choose>
        <when test= 'areaCode == "1"'>
          province_code = #{regionCode}
        </when>
        <when test = 'areaCode == "2"'>
          city_code = #{regionCode}
        </when>
      </choose>
      <if test="adviserIds != null and adviserIds.size>0">
        AND adviser_id in
        <foreach collection="adviserIds" item="item" index="index" separator="," close=")" open="(">
          ${item}
        </foreach>
      </if>
    </where>
  </select>
  <select id="getClassifyStatistics" resultType="com.pcloud.book.record.entity.ClassifyStatistics" parameterType="map">
    select
    IFNULL(book_classify_id,0) classifyId,
    IFNULL(a.classify_name,"其他分类") classifyName,
    count(distinct book_adviser_id)  bookCount
    from
    book_browse_record b
    left join applet_book_classify a
    on b.book_classify_id = a.id
    <where>
      <choose>
        <when test= 'areaCode == "1"'>
          province_code = #{regionCode}
        </when>
        <when test = 'areaCode == "2"'>
          city_code = #{regionCode}
        </when>
      </choose>
      <if test="adviserIds != null and adviserIds.size>0">
        AND adviser_id in
        <foreach collection="adviserIds" item="item" index="index" separator="," close=")" open="(">
          ${item}
        </foreach>
      </if>
    </where>
    group by book_classify_id
    order by bookCount desc
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from book_browse_record
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.pcloud.book.record.entity.BookBrowseRecord" >
    insert into book_browse_record (id, book_adviser_id, isbn, 
      user_id, province, province_code, 
      city, city_code, district, 
      district_code, gra_Label__id, sub_Label_Id, 
      first_Classify_Id, second_Classify_Id, third_Classify_Id,
      book_classify_id,
      ip, create_time, update_time, book_name, agent_id, book_id, adviser_id
      )
    values (#{id,jdbcType=BIGINT}, #{bookAdviserId,jdbcType=BIGINT}, #{isbn,jdbcType=VARCHAR}, 
      #{userId,jdbcType=BIGINT}, #{province,jdbcType=VARCHAR}, #{provinceCode,jdbcType=VARCHAR}, 
      #{city,jdbcType=VARCHAR}, #{cityCode,jdbcType=VARCHAR}, #{district,jdbcType=VARCHAR}, 
      #{districtCode,jdbcType=VARCHAR}, #{graLabelId,jdbcType=BIGINT}, #{subLabelId,jdbcType=BIGINT}, 
      #{firstClassifyId,jdbcType=BIGINT}, #{secondClassifyId,jdbcType=BIGINT}, #{thirdClassifyId},
      #{bookClassifyId,jdbcType=BIGINT},
      #{ip,jdbcType=VARCHAR}, now(), now(), #{bookName}, #{agentId}, #{bookId}, #{adviserId}
      )
    on duplicate key
    update
    update_time=NOW(),
    user_id = #{userId,jdbcType=BIGINT},
    district = #{district,jdbcType=VARCHAR},
    district_code = #{districtCode,jdbcType=VARCHAR},
    gra_Label__id = #{graLabelId,jdbcType=BIGINT},
    sub_Label_Id = #{subLabelId,jdbcType=BIGINT},
    first_Classify_Id = #{firstClassifyId,jdbcType=BIGINT},
    second_Classify_Id = #{secondClassifyId,jdbcType=BIGINT},
    third_Classify_Id = #{thirdClassifyId,jdbcType=BIGINT},
    book_classify_id = #{bookClassifyId,jdbcType=BIGINT},
    ip = #{ip,jdbcType=VARCHAR},
    book_name = #{bookName},
    agent_id = #{agentId},
    adviser_id = #{adviserId}
  </insert>
  <insert id="insertSelective" parameterType="com.pcloud.book.record.entity.BookBrowseRecord" >
    insert into book_browse_record
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="bookAdviserId != null" >
        book_adviser_id,
      </if>
      <if test="isbn != null" >
        isbn,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="province != null" >
        province,
      </if>
      <if test="provinceCode != null" >
        province_code,
      </if>
      <if test="city != null" >
        city,
      </if>
      <if test="cityCode != null" >
        city_code,
      </if>
      <if test="district != null" >
        district,
      </if>
      <if test="districtCode != null" >
        district_code,
      </if>
      <if test="graLabelId != null" >
        gra_Label__id,
      </if>
      <if test="subLabelId != null" >
        sub_Label_Id,
      </if>
      <if test="firstClassifyId != null" >
        first_Classify_Id,
      </if>
      <if test="secondClassifyId != null" >
        second_Classify_Id,
      </if>
      <if test="thirdClassifyId != null" >
        third_Classify_Id,
      </if>
      <if test="bookClassifyId != null" >
        book_classify_id,
      </if>
      <if test="ip != null" >
        ip,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="bookAdviserId != null" >
        #{bookAdviserId,jdbcType=BIGINT},
      </if>
      <if test="isbn != null" >
        #{isbn,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=BIGINT},
      </if>
      <if test="province != null" >
        #{province,jdbcType=VARCHAR},
      </if>
      <if test="provinceCode != null" >
        #{provinceCode,jdbcType=VARCHAR},
      </if>
      <if test="city != null" >
        #{city,jdbcType=VARCHAR},
      </if>
      <if test="cityCode != null" >
        #{cityCode,jdbcType=VARCHAR},
      </if>
      <if test="district != null" >
        #{district,jdbcType=VARCHAR},
      </if>
      <if test="districtCode != null" >
        #{districtCode,jdbcType=VARCHAR},
      </if>
      <if test="graLabelId != null" >
        #{graLabelId,jdbcType=BIGINT},
      </if>
      <if test="subLabelId != null" >
        #{subLabelId,jdbcType=BIGINT},
      </if>
      <if test="firstClassifyId != null" >
        #{firstClassifyId,jdbcType=BIGINT},
      </if>
      <if test="secondClassifyId != null" >
        #{secondClassifyId,jdbcType=BIGINT},
      </if>
      <if test="thirdClassifyId != null" >
        #{thirdClassifyId,jdbcType=BIGINT},
      </if>
      <if test="bookClassifyId != null" >
        #{bookClassifyId,jdbcType=BIGINT},
      </if>
      <if test="ip != null" >
        #{ip,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.record.entity.BookBrowseRecord" >
    update book_browse_record
    <set >
      <if test="bookAdviserId != null" >
        book_adviser_id = #{bookAdviserId,jdbcType=BIGINT},
      </if>
      <if test="isbn != null" >
        isbn = #{isbn,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        user_id = #{userId,jdbcType=BIGINT},
      </if>
      <if test="province != null" >
        province = #{province,jdbcType=VARCHAR},
      </if>
      <if test="provinceCode != null" >
        province_code = #{provinceCode,jdbcType=VARCHAR},
      </if>
      <if test="city != null" >
        city = #{city,jdbcType=VARCHAR},
      </if>
      <if test="cityCode != null" >
        city_code = #{cityCode,jdbcType=VARCHAR},
      </if>
      <if test="district != null" >
        district = #{district,jdbcType=VARCHAR},
      </if>
      <if test="districtCode != null" >
        district_code = #{districtCode,jdbcType=VARCHAR},
      </if>
      <if test="graLabelId != null" >
        gra_Label__id = #{graLabelId,jdbcType=BIGINT},
      </if>
      <if test="subLabelId != null" >
        sub_Label_Id = #{subLabelId,jdbcType=BIGINT},
      </if>
      <if test="firstClassifyId != null" >
        first_Classify_Id = #{firstClassifyId,jdbcType=BIGINT},
      </if>
      <if test="secondClassifyId != null" >
        second_Classify_Id = #{secondClassifyId,jdbcType=BIGINT},
      </if>
      <if test="thirdClassifyId != null" >
       third_Classify_Id = #{thirdClassifyId,jdbcType=BIGINT},
      </if>
      <if test="bookClassifyId != null" >
        book_classify_id = #{bookClassifyId,jdbcType=BIGINT},
      </if>
      <if test="ip != null" >
        ip = #{ip,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.record.entity.BookBrowseRecord" >
    update book_browse_record
    set book_adviser_id = #{bookAdviserId,jdbcType=BIGINT},
      isbn = #{isbn,jdbcType=VARCHAR},
      user_id = #{userId,jdbcType=BIGINT},
      province = #{province,jdbcType=VARCHAR},
      province_code = #{provinceCode,jdbcType=VARCHAR},
      city = #{city,jdbcType=VARCHAR},
      city_code = #{cityCode,jdbcType=VARCHAR},
      district = #{district,jdbcType=VARCHAR},
      district_code = #{districtCode,jdbcType=VARCHAR},
      gra_Label__id = #{graLabelId,jdbcType=BIGINT},
      sub_Label_Id = #{subLabelId,jdbcType=BIGINT},
      first_Classify_Id = #{firstClassifyId,jdbcType=BIGINT},
      second_Classify_Id = #{secondClassifyId,jdbcType=BIGINT},
      third_Classify_Id = #{thirdClassifyId,jdbcType=BIGINT},
      book_classify_id = #{bookClassifyId,jdbcType=BIGINT},
      ip = #{ip,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getBookDetail" parameterType="map" resultType="com.pcloud.book.record.entity.BookDetail">
    select
    book_adviser_id bookAdviserId,
    book_name bookName,
    isbn,
    agent_id agentId,
    adviser_id adviserId,
    book_classify_id bookClassifyId
    from
    book_browse_record
    <where>
      <choose>
        <when test= 'areaCode == "1"'>
          province_code = #{regionCode}
        </when>
        <when test = 'areaCode == "2"'>
          city_code = #{regionCode}
        </when>
      </choose>
      <if test="bookClassifyId != null">
        and book_classify_id = #{bookClassifyId}
      </if>
      <if test="bookName != null">
        and book_name like concat("%", #{bookName}, "%")
      </if>
      <if test="agentId != null">
        and agent_id = #{agentId}
      </if>
      and book_adviser_id is not null
      group by book_adviser_id
      order by update_time desc
    </where>
  </select>
</mapper>