<?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.push.dao.impl.SelfPushDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.SelfPush" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="alt_id" property="altId" jdbcType="VARCHAR" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="BIGINT" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="send_now" property="sendNow" jdbcType="BIT" />
    <result column="send_time" property="sendTime" jdbcType="TIMESTAMP" />
    <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT" />
    <result column="send_all" property="sendAll" jdbcType="BIT" />
    <result column="start_time" property="startTime" jdbcType="TIMESTAMP" />
    <result column="end_time" property="endTime" jdbcType="TIMESTAMP" />
    <result column="push_type" property="pushType" jdbcType="BIT" />
    <result column="week_days" property="weekDays" jdbcType="VARCHAR" />
    <result column="push_send_time" property="pushSendTime" jdbcType="VARCHAR" />
    <result column="alt_type" property="altType" jdbcType="INTEGER" />
    <result column="push_status" property="pushStatus" jdbcType="INTEGER" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, alt_id, create_user, create_time, update_user, update_time, send_now, send_time, book_group_id, send_all,start_time,end_time,
    push_type,week_days,push_send_time,alt_type, push_status
  </sql>


  <insert id="insert" parameterType="com.pcloud.book.push.entity.SelfPush" useGeneratedKeys="true" keyProperty="id">
    insert into self_push (id, alt_id, create_user,
    create_time, update_user, update_time, send_now, send_time, book_group_id, send_all,start_time,end_time,
    push_type,week_days,push_send_time,alt_type,push_status
    )
    values (#{id,jdbcType=BIGINT}, #{altId,jdbcType=VARCHAR}, #{createUser,jdbcType=BIGINT},
    now(), #{updateUser,jdbcType=BIGINT}, now(), #{sendNow}, #{sendTime}, #{bookGroupId}, #{sendAll},#{startTime},
    #{endTime},#{pushType},#{weekDays},#{pushSendTime}, #{altType},#{pushStatus,jdbcType=INTEGER}
    )
  </insert>


  <select id="getById" parameterType="long" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM self_push
    WHERE id= #{id}
  </select>

  <delete id="deleteById" parameterType="long">
    delete from
    self_push
    WHERE id= #{id}
  </delete>

  <select id="getUnclosedByRobot" parameterType="string" resultMap="BaseResultMap">
    SELECT
	<include refid="Base_Column_List"/>
    FROM
        self_push
    WHERE
        send_all = 1
    AND send_now = 0
    AND (
        send_time > CURTIME()
        OR CURTIME() BETWEEN start_time
        AND end_time
    )
    AND alt_id = #{altId}
  </select>

  <update id="updatePushStatusById" parameterType="map">
    UPDATE self_push
    SET push_status = #{pushStatus}
    WHERE id= #{id}
  </update>

  <select id="listSelfPush" parameterType="map" resultType="com.pcloud.book.push.dto.SelfPushRecordDTO">
    SELECT
    p.alt_id altId,
    p.book_group_id bookGroupId,
    p.id pushId,
    p.send_all sendAll,
    p.create_user createUser,
    p.start_time startTime,
    p.end_time endTime,
    p.push_send_time pushSendTime,
    p.week_days weekDays,
    p.push_type pushType,
    p.alt_type groupRobotType,
    p.push_status pushStatus,
    p.create_time createTime,
    p.send_time sendTime
    FROM
    self_push p
    left join self_push_item i on i.push_id=p.id
    WHERE
    1 = 1
    and i.item_type in (1,2,3,4,5,6)
    <if test="startTime != null and endTime != null">
      AND (
      (p.send_time BETWEEN #{startTime} AND #{endTime} )
      OR (p.start_time >= #{startTime} AND p.end_time &lt;= #{endTime})
      )
    </if>
    <if test="pushStatus != null">
      AND p.push_status = #{pushStatus}
    </if>
    <choose>
      <when test="partyId == 0">
        AND p.alt_id IS NOT NULL AND p.alt_id !=""
      </when>
      <otherwise>
        AND p.create_user = #{partyId}
      </otherwise>
    </choose>
    group by p.id
    ORDER BY
    p.create_time DESC
  </select>

</mapper>