<?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.timecontrol.mapper.TimeControlSelfPlanMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.timecontrol.entity.TimeControlSelfPlan" >
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="robot_wx_id" property="robotWxId" jdbcType="VARCHAR" />
    <result column="wx_id" property="wxId" jdbcType="VARCHAR" />
    <result column="task_id" property="taskId"  jdbcType="INTEGER" />
    <result column="total_price" property="totalPrice" jdbcType="DECIMAL" />
    <result column="buy_reply" property="buyReply" jdbcType="VARCHAR" />
    <result column="subscribe_reply" property="subscribeReply" jdbcType="VARCHAR" />
    <result column="evaluate_open" property="evaluateOpen" jdbcType="INTEGER" />
    <result column="service_state" property="serviceState" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="evaluate_guide" property="evaluateGuide" jdbcType="VARCHAR" />
    <result column="change_reply" property="changeReply" jdbcType="VARCHAR" />
    <result column="plan_order" property="planOrder" jdbcType="VARCHAR" />
    <result column="has_click_plan" property="hasClickPlan" jdbcType="INTEGER" />
  </resultMap>

  <sql id="Base_Column_List">
    id, robot_wx_id, wx_id, task_id, total_price, buy_reply, subscribe_reply, evaluate_open,
    service_state, create_time, update_time,evaluate_guide,change_reply, plan_order, has_click_plan
  </sql>

  <insert id="insert" parameterType="TimeControlSelfPlan" useGeneratedKeys="true" keyProperty="id">
    insert into time_control_self_plan (robot_wx_id, wx_id, task_id,
      total_price, buy_reply, subscribe_reply,
      evaluate_open, service_state, create_time, update_time)
    values (#{robotWxId,jdbcType=VARCHAR}, #{wxId,jdbcType=VARCHAR}, #{taskId,jdbcType=INTEGER},
      #{totalPrice,jdbcType=DECIMAL}, #{buyReply,jdbcType=VARCHAR}, #{subscribeReply,jdbcType=VARCHAR},
      0, 0, NOW(), NOW())
  </insert>

  <update id="update" parameterType="TimeControlSelfPlan">
    update time_control_self_plan
    <set>
      <if test="robotWxId != null">
        robot_wx_id = #{robotWxId,jdbcType=VARCHAR},
      </if>
      <if test="wxId != null">
        wx_id = #{wxId,jdbcType=VARCHAR},
      </if>
      <if test="taskId != null">
        task_id = #{taskId,jdbcType=INTEGER},
      </if>
      <if test="totalPrice != null">
        total_price = #{totalPrice,jdbcType=DECIMAL},
      </if>
      <if test="buyReply != null">
        buy_reply = #{buyReply,jdbcType=VARCHAR},
      </if>
      <if test="subscribeReply != null">
        subscribe_reply = #{subscribeReply,jdbcType=VARCHAR},
      </if>
      <if test="evaluateOpen != null">
        evaluate_open = #{evaluateOpen,jdbcType=INTEGER},
      </if>
      <if test="serviceState != null">
        service_state = #{serviceState,jdbcType=INTEGER},
      </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=INTEGER}
  </update>

  <delete id="deleteById" parameterType="java.lang.Integer">
    delete from time_control_self_plan
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <select id="getById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
        <include refid="Base_Column_List" />
    from time_control_self_plan
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="getSelfPlanCount" parameterType="integer" resultType="java.lang.Integer">
    SELECT
        ifnull(COUNT(1), 0)
    FROM
        `time_control_self_plan`
        where 1=1
    <if test="serviceState != null">
      and service_state = #{serviceState}
    </if>
  </select>

  <select id="listSelfPlan" resultType="com.pcloud.book.timecontrol.vo.SelfPlanVO">
    SELECT
        id planId,
        robot_wx_id robotWxId,
        wx_id wxId,
        task_id taskId,
        total_price totalPrice,
        buy_reply buyReply,
        subscribe_reply subscribeReply,
        evaluate_open evaluateOpen,
        service_state serviceState,
        create_time createTime
    FROM
        time_control_self_plan
        where 1=1
    <if test="serviceState != null">
      and service_State = #{serviceState}
    </if>
    ORDER BY
        create_time DESC
    LIMIT #{pageNum}, #{numPerPage}
  </select>

  <update id="bindTask">
    update time_control_self_plan set task_id = #{taskId}, update_time = now()
    where id = #{planId}
  </update>

  <select id="getSelfPlanUserDtoMap" parameterType="list" resultType="com.pcloud.book.timecontrol.dto.SelfPlanUserDto">
    SELECT
    wx_id wxUserId,
    count(id) slefPlanCount,
    sum(service_state = 1) orderCount
    FROM
    time_control_self_plan
    <where>
    <if test="list != null">
      wx_id in
      <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
    </where>
    group by wx_id
  </select>

  <update id="updateServiceState" parameterType="com.pcloud.book.timecontrol.entity.TimeControlSelfPlan">
    update time_control_self_plan set service_state = #{serviceState}
    where id = #{id}
  </update>

  <update id="updatePlanOrder">
    update time_control_self_plan set plan_order = #{planOrder}
    where id = #{id}
  </update>

  <update id="updateHasClickPlan">
    update time_control_self_plan set has_click_plan = 1, update_time = now()
    where id = #{id}
  </update>

    <update id="advancedSetting" parameterType="com.pcloud.book.timecontrol.entity.TimeControlSelfPlan">
      update
      time_control_self_plan
      set
      total_price = #{totalPrice} ,
      buy_reply =  #{buyReply} ,
      change_reply = #{changeReply}, update_time = now()
      <if test="evaluateOpen != null">
       ,evaluate_open = #{evaluateOpen}
      </if>
      <if test="evaluateGuide != null">
       , evaluate_guide = #{evaluateGuide}
      </if>
      where id = #{id}
    </update>

  <update id="updateSelfPlan" parameterType="com.pcloud.book.timecontrol.vo.UpdateSelfPlanVO">
    update
    time_control_self_plan
    set
    robot_wx_id = #{robotWxId},
    wx_id = #{wxId}, update_time = now()
    where
    id = #{planId}
  </update>

  <select id="getLatestPlan" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from time_control_self_plan
    where wx_id = #{wxId}
    order by create_time desc
    limit 1
  </select>
</mapper>