<?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.cool.store.mapper.PointTodoInfoMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.PointTodoInfoDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="point_id" jdbcType="BIGINT" property="pointId" />
    <result column="node_no" jdbcType="INTEGER" property="nodeNo" />
    <result column="handler_user_id" jdbcType="VARCHAR" property="handlerUserId" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="cycle_count" jdbcType="TINYINT" property="cycleCount" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>

  <insert id="batchInsertSelective">
    <foreach collection="addList" separator=";" item="item">
      insert into xfsg_point_todo_info
      <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="item.pointId != null">
          point_id,
        </if>
        <if test="item.nodeNo != null">
          node_no,
        </if>
        <if test="item.handlerUserId != null">
          handler_user_id,
        </if>
        <if test="item.status != null">
          status,
        </if>
        <if test="item.cycleCount != null">
          cycle_count,
        </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="item.pointId != null">
          #{item.pointId},
        </if>
        <if test="item.nodeNo != null">
          #{item.nodeNo},
        </if>
        <if test="item.handlerUserId != null">
          #{item.handlerUserId},
        </if>
        <if test="item.status != null">
          #{item.status},
        </if>
        <if test="item.cycleCount != null">
          #{item.cycleCount},
        </if>
      </trim>
    </foreach>
  </insert>
  
  <select id="getTodoUserList" resultType="java.lang.String">
    select handler_user_id from xfsg_point_todo_info where point_id = #{pointId} and status = 0 and deleted = 0
  </select>

  <select id="getTodoList" resultMap="BaseResultMap">
    select * from xfsg_point_todo_info where point_id = #{pointId} and status = 0 and deleted = 0
  </select>

  <select id="getPointToDoByUserIdAndPointId" resultMap="BaseResultMap">
    select * from xfsg_point_todo_info where point_id = #{pointId} and handler_user_id = #{userId} and status = 0 and deleted = 0
  </select>

  <update id="updatePointTodoInfo">
    update xfsg_point_todo_info set status = if(handler_user_id = #{handlerUserId}, 1, 2), update_time = now() where point_id = #{pointId} and node_no = #{nodeNo} and cycle_count = #{cycleCount} and deleted = 0
  </update>

  <select id="getUserTodoList" resultType="com.cool.store.entity.PointInfoDO">
    select
      b.id,
      b.point_name,
      b.point_code,
      b.point_status,
      b.point_score,
      b.development_manager,
      b.development_time,
      b.point_area,
      b.address,
      b.latitude,
      b.longitude,
      b.region_id,
      b.line_id,
      b.shop_id,
      b.select_status,
      b.operate_user_id,
      b.point_source,
      b.create_time,
      b.update_time
    from
      xfsg_point_todo_info a inner join xfsg_point_info b on a.point_id = b.id
    where
      a.handler_user_id = #{request.developmentManager} and a.status = 0 and a.deleted = 0 and b.deleted = 0 and b.point_status in (3,4)
  </select>

  <select id="getCurNodeNoByPoint" resultType="integer">
    select node_no from xfsg_point_todo_info where point_id = #{pointId} and status = 0 and deleted = 0 limit 1
  </select>

</mapper>