<?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.PublicReleaseCountMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.PublicReleaseCount">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="province" jdbcType="INTEGER" property="province" />
    <result column="dep_id" jdbcType="VARCHAR" property="depId" />
    <result column="uid" jdbcType="VARCHAR" property="uid" />
    <result column="quantity" jdbcType="INTEGER" property="quantity" />
    <result column="new_user" jdbcType="INTEGER" property="newUser" />
    <result column="total" jdbcType="DECIMAL" property="total" />
    <result column="date" jdbcType="VARCHAR" property="date" />
    <result column="sign" jdbcType="INTEGER" property="sign" />
  </resultMap>
  <sql id="Base_Column_List">
    id, province, dep_id, `uid`, quantity, new_user, total, `date`, sign
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from public_release_count
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from public_release_count
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.PublicReleaseCount">
    insert into public_release_count (id, province, dep_id, 
      `uid`, quantity, new_user, 
      total, `date`, sign)
    values (#{id,jdbcType=INTEGER}, #{province,jdbcType=INTEGER}, #{depId,jdbcType=VARCHAR}, 
      #{uid,jdbcType=VARCHAR}, #{quantity,jdbcType=INTEGER}, #{newUser,jdbcType=INTEGER}, 
      #{total,jdbcType=DECIMAL}, #{date,jdbcType=VARCHAR}, #{sign,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.PublicReleaseCount">
    insert into public_release_count
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="province != null">
        province,
      </if>
      <if test="depId != null">
        dep_id,
      </if>
      <if test="uid != null">
        `uid`,
      </if>
      <if test="quantity != null">
        quantity,
      </if>
      <if test="newUser != null">
        new_user,
      </if>
      <if test="total != null">
        total,
      </if>
      <if test="date != null">
        `date`,
      </if>
      <if test="sign != null">
        sign,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="province != null">
        #{province,jdbcType=INTEGER},
      </if>
      <if test="depId != null">
        #{depId,jdbcType=VARCHAR},
      </if>
      <if test="uid != null">
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="quantity != null">
        #{quantity,jdbcType=INTEGER},
      </if>
      <if test="newUser != null">
        #{newUser,jdbcType=INTEGER},
      </if>
      <if test="total != null">
        #{total,jdbcType=DECIMAL},
      </if>
      <if test="date != null">
        #{date,jdbcType=VARCHAR},
      </if>
      <if test="sign != null">
        #{sign,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.PublicReleaseCount">
    update public_release_count
    <set>
      <if test="province != null">
        province = #{province,jdbcType=INTEGER},
      </if>
      <if test="depId != null">
        dep_id = #{depId,jdbcType=VARCHAR},
      </if>
      <if test="uid != null">
        `uid` = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="quantity != null">
        quantity = #{quantity,jdbcType=INTEGER},
      </if>
      <if test="newUser != null">
        new_user = #{newUser,jdbcType=INTEGER},
      </if>
      <if test="total != null">
        total = #{total,jdbcType=DECIMAL},
      </if>
      <if test="date != null">
        `date` = #{date,jdbcType=VARCHAR},
      </if>
      <if test="sign != null">
        sign = #{sign,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.PublicReleaseCount">
    update public_release_count
    set province = #{province,jdbcType=INTEGER},
      dep_id = #{depId,jdbcType=VARCHAR},
      `uid` = #{uid,jdbcType=VARCHAR},
      quantity = #{quantity,jdbcType=INTEGER},
      new_user = #{newUser,jdbcType=INTEGER},
      total = #{total,jdbcType=DECIMAL},
      `date` = #{date,jdbcType=VARCHAR},
      sign = #{sign,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="followStatisticsList" resultType="com.goafanti.user.bo.outFollowStatisticsList">
    select z.id,z.name,z.use_interview useInterview,z.use_quantity useQuantity,z.use_total useTotal,ifnull(z.useRatio,0)useRatio,
    z.new_interview newInterview,z.new_quantity newQuantity,z.new_total newTotal,ifnull(z.newRatio,0)newRatio,
    z.totalInterview,z.totalQuantity,z.total,ifnull((z.totalQuantity/z.totalInterview),0)totalRatio
    from(select y.id,name,use_interview , use_quantity ,use_total ,(use_quantity/use_interview)useRatio,
    new_interview ,new_quantity ,new_total ,(new_quantity/new_interview)newRatio,
    (use_interview+new_interview)totalInterview,(use_quantity+new_quantity)totalQuantity,(use_total+new_total)total
    from (select dg.id,dg.name,use_interview, use_quantity,use_total,new_interview,new_quantity,new_total
    from (select province,sum(if(new_user=1,a.quantity,0))use_interview,sum(if(new_user=1,a.sign ,0))use_quantity,
    sum(if(new_user=1,a.total ,0))use_total, sum(if(new_user=0,a.quantity,0))new_interview,sum(if(new_user=0,a.sign ,0))new_quantity,
    sum(if(new_user=0,a.total ,0))new_total from public_release_count a
    where a.`date` = #{date}
    <if test="depId !=null">
      and a.dep_id= #{depId}
    </if>
    <if test="province !=null">
      and a.province= #{province}
    </if>
     group by a.province)x
    left join district_glossory dg on x.province=dg.id )y )z
    order by
    <if test="sort==0">
      totalQuantity
    </if>
    <if test="sort==1">
      total
    </if>
    <if test="sort==2">
      totalInterview
    </if>
    desc
  </select>
  <select id="provinceFollowStatisticsList" resultType="com.goafanti.user.bo.outProvinceFollowStatistic">
    select x.id,x.name,c.id uid,c.nickname ,x.new_user newUser,x.quantity,x.sign
    from (select b.id,b.name,a.new_user,a.uid,sum(a.quantity)quantity,sum(a.sign)sign from public_release_count a  left join department b on a.dep_id =b.id
    where a.date= #{date} and a.province = #{province}
    <if test="depId !=null">
      and a.dep_id = #{depId}
    </if>
    <if test="type ==1">
      and a.new_user =1
    </if>
    <if test="type ==2">
      and a.new_user =0
    </if>
    group by b.id,b.name ,a.uid ,a.new_user )x
    left join `user` c on x.uid =c.id
    order by
    <if test="sort==0">
      x.sign
    </if>
    <if test="sort==1">
      x.quantity
    </if>
    desc
  </select>

  <insert id="insertBatch">
    insert into public_release_count (province, dep_id, `uid`,
    quantity, new_user, total,
    `date`,sign)
    values
    <foreach collection="list" index="index" item="item" separator=",">
      (#{item.province,jdbcType=INTEGER}, #{item.depId,jdbcType=VARCHAR}, #{item.uid,jdbcType=VARCHAR},
      #{item.quantity,jdbcType=INTEGER}, #{item.newUser,jdbcType=INTEGER}, #{item.total,jdbcType=DECIMAL},
      #{item.date,jdbcType=VARCHAR},#{item.sign,jdbcType=INTEGER})
    </foreach>
  </insert>

</mapper>