<?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.qxgmat.data.relation.UserRelationMapper">
  <resultMap id="IdMap" type="com.qxgmat.data.dao.entity.User">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
  </resultMap>
  <resultMap id="Prepare" type="com.qxgmat.data.relation.entity.UserPrepareRelation">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="i" jdbcType="INTEGER" property="i" />
    <id column="s" jdbcType="VARCHAR" property="s" />
    <id column="number" jdbcType="INTEGER" property="number" />
  </resultMap>
  <sql id="Id_Column_List">
    <!--
      WARNING - @mbg.generated
    -->
    ur.`id`
  </sql>

  <!--
    字符串字段统计
  -->
  <select id="groupPrepareString" resultMap="Prepare">
    select
      `${field}` as `s`, count(*) as `number`
    from `user`
    where
      `prepare_goal` > 0
    and `${field}` != null and `${field}` != ""
    group by `${field}`
  </select>

  <!--
    数字字段统计
  -->
  <select id="groupPrepareInteger" resultMap="Prepare">
    select
    `${field}` as `i`, count(*) as `number`
    from `user`
    where
    `prepare_goal` > 0
    and `${field}` > 0
    group by `${field}`
  </select>

  <!--
    购买过课程的用户
  -->
  <select id="listByCourse" resultMap="IdMap">
    <bind name="keywordLike" value="'%' + keyword + '%'" />
    select
    <include refid="Id_Column_List" />
    from `user` u
    left join `user_class` uc on uc.`user_id` = u.`id`
    <if test="courseId != null">
      and uc.`course_id` = #{courseId,jdbcType=VARCHAR}
    </if>
    where uc.`id` != null
    <if test="keyword != null">
      and (u.`mobile` like #{keywordLike,jdbcType=VARCHAR}
        or u.`id` like #{keywordLike,jdbcType=VARCHAR})
    </if>
  </select>
</mapper>