<?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="shop_id" jdbcType="BIGINT" property="shopId"/>
        <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,shop_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="getShopPushPointNumMap" resultType="com.cool.store.dto.point.ShopCountDTO">
        select
        shop_id as shopId,
        count(1) as recommendShopNum
        from
        xfsg_point_recommend
        where
        deleted = 0 and shop_id in
        <foreach collection="shopIds" item="shopId" index="index" open="(" separator="," close=")">
            #{shopId}
        </foreach>
        group by shop_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>


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

    <insert id="batchInsert">
        <foreach collection="recommendList" item="item" index="index" separator=";">
            insert into xfsg_point_recommend (shop_id, point_id, development_manager, status)
            values (#{item.shopId}, #{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
        shop_id = #{shopId}
    </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.longitude as longitude,
        p.latitude as latitude,
        p.address as address,
        p.point_score as pointScore,
        p.point_area as pointArea,
        p.point_status as pointStatus,
        p.select_status as selectStatus,
        p.create_time as createTime,
        p.opportunity_point_code as opportunityPointCode,
        p.opportunity_point_name as opportunityPointName,
        c.picture_obj as pictureObj,
        pr.status as recommendStatus
        from
        xfsg_point_recommend pr
        inner join xfsg_point_info p on p.id = pr.point_id
        left join xfsg_point_detail_info c on p.id = c.point_id
        where
        p.deleted = 0 and pr.shop_id = #{request.shopId} 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.shop_id = #{request.shopId}
        </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(shop_id = #{shopId}, 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 shop_id = #{shopId} 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>

    <select id="getAllRecommendPointList" resultMap="BaseResultMap">
        select
        <include refid="allColumn"/>
        from
        xfsg_point_recommend
        <where>
            and (shop_id = 0 or shop_id is null)
            <if test="lineId!=null">
                and line_id = #{lineId}
            </if>
        </where>
    </select>


    <update id="batchUpdateShopId">
        update xfsg_point_recommend
        set
        shop_id = case id
        <foreach collection="recommendList" item="item" index="index">
            when #{item.id} then #{item.shopId}
        </foreach>
        end
        where
        id in
        <foreach collection="recommendList" item="entity" open="(" separator="," close=")">
            #{entity.id}
        </foreach>
    </update>

</mapper>