<?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.PointRecommendMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.PointRecommendDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="line_id" jdbcType="BIGINT" property="lineId" />
    <result column="point_id" jdbcType="BIGINT" property="pointId" />
    <result column="development_manager" jdbcType="VARCHAR" property="developmentManager" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="reason" jdbcType="VARCHAR" property="reason" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>

  <sql id="allColumn">
    id, line_id, point_id, development_manager, status, reason, deleted, create_time, update_time
  </sql>

  <update id="updateShopPointRecommendStatus">
    update xfsg_point_recommend set status = #{status} where point_id = #{pointId} and deleted = 0
    <if test="excludeStatus != null and excludeStatus.size()>0">
      and status not in
      <foreach collection="excludeStatus" separator="," open="(" close=")" item="status">
        #{status}
      </foreach>
    </if>
  </update>

  <update id="updateRecommendStatusByStatusAndPointId">
    update xfsg_point_recommend set status = #{status} where point_id = #{pointId} and deleted = 0 and status in
    <foreach collection="statusList" item="status" index="index" open="(" separator="," close=")">
      #{status}
    </foreach>
  </update>

  <select id="getPushShopNumMap" resultType="com.cool.store.dto.point.LineCountDTO">
    select
      line_id as lineId,
      count(1) as recommendShopNum
    from
      xfsg_point_recommend
    where
      deleted = 0 and line_id in
      <foreach collection="lineIds" item="lineId" index="index" open="(" separator="," close=")">
        #{lineId}
      </foreach>
    group by line_id
  </select>

  <select id="getRecommendPointList" resultMap="BaseResultMap">
    select
      id,
      point_id,
      line_id,
      development_manager,
      status,
      reason
    from
      xfsg_point_recommend
    where
      line_id = #{lineId} and deleted = 0
  </select>

  <insert id="batchInsert">
    <foreach collection="recommendList" item="item" index="index" separator=";">
      insert into xfsg_point_recommend (line_id, point_id, development_manager, status)
      values (#{item.lineId}, #{item.pointId}, #{item.developmentManager}, #{item.status})
    </foreach>
  </insert>

  <update id="turnLineUpdateRecommendStatus">
    update
      xfsg_point_recommend
    set
      deleted = if(status = 1, 1, deleted),
      development_manager = if(status in (2,4), #{developmentManager}, development_manager)
    where
      line_id = #{lineId}
  </update>

  <select id="getLineRecommendPointPage" resultType="com.cool.store.dto.point.MiniPointPageDTO">
    select
      p.id as pointId,
      pr.line_id as lineId,
      p.point_name as pointName,
      p.point_code as pointCode,
      p.region_id as regionId,
      p.address as address,
      p.point_score as pointScore,
      p.point_area as pointArea,
      p.point_status as pointStatus,
      p.select_status as selectStatus,
      pr.status as recommendStatus
    from
      xfsg_point_recommend pr
        inner join xfsg_point_info p on p.id = pr.point_id
    where
      p.deleted = 0 and pr.line_id = #{request.lineId} and pr.deleted = 0
    <if test="request.status != null and request.status == 1">
      and pr.status = 1 and p.select_status = 0
    </if>
    <if test="request.status != null and request.status == 2">
      and pr.status = 2 and p.select_status = 1 and p.line_id = #{request.lineId}
    </if>
    <if test="request.status != null and request.status == 3">
      and pr.status in (5, 6)
    </if>
  </select>

  <update id="updateStatusByPointIdAndLineId">
    update
      xfsg_point_recommend
    set
      status = if(line_id = #{lineId}, 2, 3)
    where point_id = #{pointId} and deleted = 0 and status = 1
  </update>

  <update id="rejectPoint">
    update
      xfsg_point_recommend
    set
      status = 5,
      reason = #{reason}
    where point_id = #{pointId} and line_id = #{lineId} and deleted = 0 and status = 1
  </update>
  
  <select id="getRecommendPointListByPointId" resultMap="BaseResultMap">
    select
      <include refid="allColumn"/>
    from
      xfsg_point_recommend
    where
      point_id = #{pointId} and deleted = 0
  </select>
</mapper>