<?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.custom.mapper.CustomPlanModuleSuggestionMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.custom.entity.CustomPlanModuleSuggestion" >
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="batch_id" jdbcType="INTEGER" property="batchId" />
    <result column="wx_id" jdbcType="VARCHAR" property="wx_id" />
    <result column="plan_id" jdbcType="INTEGER" property="planId" />
    <result column="module_id" jdbcType="INTEGER" property="moduleId" />
    <result column="suggestion_level" jdbcType="INTEGER" property="suggestionLevel" />
    <result column="suggestion" jdbcType="VARCHAR" property="suggestion" />
    <result column="create_time" jdbcType="TIMESTAMP" property="planId" />
    <result column="user_type" jdbcType="INTEGER" property="userType" />
  </resultMap>

  <select id="getSuggestionCount" resultType="int">
      SELECT
      COUNT(DISTINCT batch_id)
      FROM
      custom_plan_module_suggestion a
      LEFT JOIN custom_plan b ON a.plan_id = b.id
      WHERE
      plan_id = #{planId}
      <if test="startTime != null and endTime != null">
          and a.create_time between #{startTime} and #{endTime}
      </if>
      <if test="userType!=null">
         and a.user_type=#{userType}
      </if>
      <if test="wxIdList != null">
          and wx_id in
          <foreach collection="wxIdList" open="(" close=")" separator="," item="item">
              #{item}
          </foreach>
      </if>
  </select>

  <select id="listSuggestion4Plan" resultType="com.pcloud.book.custom.vo.SuggestionListVO">
    SELECT
        batch_id batchId,
        wx_id wxId,
        SUM(suggestion_level = 0) defaultCount,
        SUM(suggestion_level = 1) goodCount,
        SUM(suggestion_level = 2) badCount,
        a.create_time createTime,
        a.plan_id planId,
        a.user_type userType
    FROM
        custom_plan_module_suggestion a
    LEFT JOIN custom_plan b ON a.plan_id = b.id
    WHERE
    plan_id = #{planId}
    <if test="startTime != null and endTime != null">
      and a.create_time between #{startTime} and #{endTime}
    </if>
      <if test="userType!=null">
        and  a.user_type=#{userType}
      </if>
    <if test="wxIdList != null">
      and wx_id in
      <foreach collection="wxIdList" open="(" close=")" separator="," item="item">
        #{item}
      </foreach>
    </if>
    GROUP BY batch_id
    order by batchId desc
    limit #{pageNum}, #{numPerPage}
  </select>

    <select id="getSuggestionInfo" resultType="com.pcloud.book.custom.vo.ModuleSuggestionVO">
        SELECT
            module_type moduleType,
            module_id moduleId,
            open_feedback openFeedback,
            suggestion_level suggestionLevel,
            suggestion
        FROM
            custom_plan_module_suggestion
        WHERE
            batch_id = #{batchId}
    </select>

    <select id="getMaxBatchId" resultType="Integer">
        select ifnull(max(batch_id), 0) from custom_plan_module_suggestion
    </select>

    <insert id="batchInsert" parameterType="com.pcloud.book.custom.entity.CustomPlanModuleSuggestion">
        insert into custom_plan_module_suggestion(batch_id, wx_id, plan_id, module_Id, suggestion_Level, suggestion, create_time,
        module_type, open_feedback,user_type)
        values
        <foreach collection="list" separator="," item="item">
            (#{item.batchId}, #{item.wxId}, #{item.planId}, #{item.moduleId}, #{item.suggestionLevel}, #{item.suggestion}, now(),
            #{item.moduleType}, #{item.openFeedback},#{item.userType})
        </foreach>
    </insert>

    <insert id="addCustomPlan4User" parameterType="com.pcloud.book.custom.vo.AddCustomPlan4UserVO">
        insert into custom_plan_user(wx_id, plan_id, create_time)
        values (#{wxId}, #{planId}, now())
    </insert>

    <select id="getLatestPlanId" resultType="Integer">
        select plan_id from custom_plan_user
        where wx_id =#{wxId}
        order by create_time desc
        limit 1
    </select>

    <select id="getPlanIdListByUser" parameterType="string" resultType="Integer">
        SELECT DISTINCT plan_id
        FROM custom_plan_module_suggestion
        WHERE wx_id = #{wxId}
    </select>

    <insert id="addBookNameAndServiceType" parameterType="com.pcloud.book.custom.vo.AddBookNameVO">
        insert into user_book_service(wx_id, robot_wx_id, book_name, service_type, create_time)
        values(#{wxId}, #{robotWxId}, #{bookName}, #{serviceType}, now())
    </insert>

    <select id="getUserBookAdnServiceType" resultType="com.pcloud.book.custom.vo.AddBookNameVO">
        SELECT
            wx_id wxId,
            robot_wx_id robotWxId,
            book_name bookName,
            service_type serviceType
        FROM
            user_book_service
        WHERE
            wx_id = #{wxId}
        AND robot_wx_id = #{robotWxId}
        <if test="type == 1">
            and book_name is not null
        </if>
        <if test="type == 2">
            and service_type is not null
        </if>
        order by create_time desc
        limit 1
    </select>

    <select id="getLastUserBookService" resultType="com.pcloud.book.custom.vo.UserBookServiceVO">
        SELECT
        id id,
        wx_id wxId,
        robot_wx_id robotWxId,
        book_name bookName,
        service_type serviceType
        FROM
        user_book_service
        WHERE
        book_name is not null
        and wx_id = #{wxId}
        AND robot_wx_id = #{robotWxId}
        order by create_time desc
        limit 1
    </select>

    <select id="getUserBookServiceById" resultType="com.pcloud.book.custom.vo.UserBookServiceVO">
        SELECT
        id id,
        wx_id wxId,
        robot_wx_id robotWxId,
        book_name bookName,
        service_type serviceType
        FROM
        user_book_service
        where id=#{userBookServiceId}
        limit 1
    </select>
</mapper>