<?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.PointInfoMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.PointInfoDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="point_code" jdbcType="VARCHAR" property="pointCode"/>
        <result column="point_name" jdbcType="VARCHAR" property="pointName"/>
        <result column="region_id" jdbcType="BIGINT" property="regionId"/>
        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="line_id" jdbcType="BIGINT" property="lineId"/>
        <result column="point_area" jdbcType="VARCHAR" property="pointArea"/>
        <result column="longitude" jdbcType="VARCHAR" property="longitude"/>
        <result column="latitude" jdbcType="VARCHAR" property="latitude"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="development_manager" jdbcType="VARCHAR" property="developmentManager"/>
        <result column="operate_user_id" jdbcType="VARCHAR" property="operateUserId"/>
        <result column="development_time" jdbcType="TIMESTAMP" property="developmentTime"/>
        <result column="point_status" jdbcType="TINYINT" property="pointStatus"/>
        <result column="point_score" jdbcType="INTEGER" property="pointScore"/>
        <result column="select_status" jdbcType="TINYINT" property="selectStatus"/>
        <result column="submit_audit_count" jdbcType="TINYINT" property="submitAuditCount"/>
        <result column="point_source" jdbcType="TINYINT" property="pointSource"/>
        <result column="deleted" jdbcType="BIT" property="deleted"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="province" jdbcType="VARCHAR" property="province"/>
        <result column="city" jdbcType="VARCHAR" property="city"/>
        <result column="district" jdbcType="VARCHAR" property="district"/>
        <result column="township" jdbcType="VARCHAR" property="township"/>
        <result column="storage_status" jdbcType="TINYINT" property="storageStatus"/>
        <result column="opportunity_point_code" jdbcType="VARCHAR" property="opportunityPointCode"/>
        <result column="opportunity_point_name" jdbcType="VARCHAR" property="opportunityPointName"/>
        <result column="province_code" jdbcType="VARCHAR" property="provinceCode"/>
        <result column="city_code" jdbcType="VARCHAR" property="cityCode"/>
        <result column="district_code" jdbcType="VARCHAR" property="districtCode"/>
        <result column="point_location" jdbcType="VARCHAR" property="pointLocation"/>
    </resultMap>

    <sql id="allColumn">
        id, point_code, point_name, region_id, shop_id, line_id, point_area, longitude, latitude, address,
        development_manager, operate_user_id,
        development_time, point_status, point_score, select_status, submit_audit_count, point_source, deleted,
        create_time, update_time,
        province, city, district, township, opportunity_point_code, opportunity_point_name,
        province_code, city_code, district_code, point_location
    </sql>

    <select id="getMyPointData" resultType="com.cool.store.vo.point.PointHomePageDataVO">
        select
        sum(if(point_status != 7, 1, 0)) as myPoint,
        sum(if(point_status in (4, 5, 6), 1, 0)) as poolPoint,
        sum(if(point_status = 1, 1, 0)) as collectPoint,
        sum(if(point_status = 2, 1, 0)) as evaluatePoint,
        sum(if(point_status in (3, 4), 1, 0)) as waitAuditPoint,
        sum(if(point_status = 7, 1, 0)) as signPoint
        from xfsg_point_info
        where deleted = 0 and development_manager = #{userId}
    </select>
    <select id="getMyPointPage" resultType="com.cool.store.dto.PCPointListDTO">
        select
        a.id,
        a.point_name,
        a.point_code,
        a.address,
        a.region_id,
        a.point_status,
        a.point_score,
        a.point_area,
        a.development_manager,
        a.operate_user_id,
        a.development_time,
        a.select_status,
        a.create_time,
        a.update_time,
        a.opportunity_point_code,
        a.opportunity_point_name,
        c.store_name as openName
        from xfsg_point_info a
        left join xfsg_shop_info b on a.shop_id = b.id
        left join store_${eid} c on b.shop_code = c.store_num
        where a.deleted = 0 and a.development_manager = #{request.developmentManager}
        <if test="request.keyword != null and request.keyword != ''">
            and (a.point_code like concat('%', #{request.keyword}, '%') or a.point_name like concat('%',
            #{request.keyword},
            '%'))
        </if>
        <if test="request.developmentStartTime != null and request.developmentStartTime != ''">
            and a.development_time >= #{request.developmentStartTime}
        </if>
        <if test="request.developmentEndTime != null and request.developmentEndTime != ''">
            <![CDATA[and a.development_time <= #{request.developmentEndTime}]]>
        </if>
        <if test="request.pointStatusList != null and request.pointStatusList.size() > 0">
            and a.point_status in
            <foreach collection="request.pointStatusList" item="pointStatus" open="(" close=")" separator=",">
                #{pointStatus}
            </foreach>
        </if>
        <if test="request.storageStatus != null">
            and a.storage_status = #{request.storageStatus}
        </if>
        <if test="request.operateUserId != null and request.operateUserId != ''">
            and a.operate_user_id = #{request.operateUserId}
        </if>
        <if test="request.createStartTime != null and request.createStartTime != ''">
            and a.create_time >= #{request.createStartTime}
        </if>
        <if test="request.createEndTime != null and request.createEndTime != ''">
            <![CDATA[and a.create_time <= #{request.createEndTime}]]>
        </if>
        <if test="request.regionIds != null and request.regionIds.size() > 0">
            and a.region_id in
            <foreach collection="request.regionIds" item="regionId" index="index" open="(" separator="," close=")">
                #{regionId}
            </foreach>
        </if>
        <if test="request.areaCode!=null and request.areaCode!=''">
            and (a.province_code = #{request.areaCode} or a.city_code = #{request.areaCode} or a.district_code =
            #{request.areaCode})
        </if>
        order by a.id desc
    </select>
    <select id="getSelectedShopNum" resultType="com.cool.store.dto.point.LineCountDTO">
        select
        line_id as lineId,
        count(1) as selectedShopNum
        from xfsg_point_info
        where deleted = 0 and select_status = 1 and point_status !=7 and line_id in
        <foreach collection="lineIds" item="lineId" index="index" open="(" separator="," close=")">
            #{lineId}
        </foreach>
        group by line_id
    </select>
    <select id="getPointListByIds" resultMap="BaseResultMap">
        select
        <include refid="allColumn"/>
        from xfsg_point_info
        where deleted = 0 and id in
        <foreach collection="pointIds" item="pointId" index="index" open="(" separator="," close=")">
            #{pointId}
        </foreach>
    </select>
    <select id="getRecommendPointList" resultType="com.cool.store.dto.PCPointListDTO">
        select
        <include refid="allColumn"/>
        from xfsg_point_info
        where deleted = 0 and point_status in (4,5) and development_manager = #{request.developmentManager} and
        select_status = 0
        <if test="request.pointStatusList != null and request.pointStatusList.size() > 0">
            and point_status in
            <foreach collection="request.pointStatusList" item="pointStatus" separator="," open="(" close=")">
                #{pointStatus}
            </foreach>
        </if>
    </select>

    <update id="updateSelectedDevelopmentManager">
        update
        xfsg_point_info
        set
        development_manager = #{developmentManager}
        where
        shop_id = #{shopId} and select_status = '1' and deleted = 0
    </update>

    <select id="getTeamPointPage" resultType="com.cool.store.dto.PCPointListDTO">
        select
        a.id,
        a.point_name,
        a.point_code,
        a.address,
        a.region_id,
        a.point_status,
        a.point_score,
        a.point_area,
        a.development_manager,
        a.operate_user_id,
        a.development_time,
        a.select_status,
        a.create_time,
        a.update_time,
        a.opportunity_point_code,
        a.opportunity_point_name,
        c.store_name as openName
        from xfsg_point_info a
        left join xfsg_shop_info b on a.shop_id = b.id
        left join store_${eid} c on b.shop_code = c.store_num
        where a.deleted = 0 and a.point_status in (4,5,6,7)
        <if test="request.keyword != null and request.keyword != ''">
            and (a.point_code like concat('%', #{request.keyword}, '%') or a.point_name like concat('%',
            #{request.keyword},
            '%'))
        </if>
        <if test="request.developmentManager != null and request.developmentManager != ''">
            and a.development_manager = #{request.developmentManager}
        </if>
        <if test="request.pointStatus != null and request.pointStatus != ''">
            and a.point_status = #{request.pointStatus}
        </if>
        <if test="request.createStartTime != null and request.createStartTime != ''">
            and a.create_time >= #{request.createStartTime}
        </if>
        <if test="request.createEndTime != null and request.createEndTime != ''">
            <![CDATA[and a.create_time <= #{request.createEndTime}]]>
        </if>
        <if test="request.regionIds != null and request.regionIds.size() > 0">
            and a.region_id in
            <foreach collection="request.regionIds" item="regionId" index="index" open="(" separator="," close=")">
                #{regionId}
            </foreach>
        </if>
        <if test="request.authRegionIds != null and request.authRegionIds.size() > 0">
            and a.region_id in
            <foreach collection="request.authRegionIds" item="regionId" index="index" open="(" separator="," close=")">
                #{regionId}
            </foreach>
        </if>
        <if test="request.storageStatus != null ">
            and a.storage_status = #{request.storageStatus}
        </if>
        <if test="request.areaCode!=null and request.areaCode!=''">
            and (a.province_code = #{request.areaCode} or a.city_code = #{request.areaCode} or a.district_code =
            #{request.areaCode})
        </if>
        order by a.id desc
    </select>
    <select id="getDataByShopIdAndLineId" resultType="com.cool.store.entity.PointInfoDO">
        SELECT
        <include refid="allColumn"/>
        FROM xfsg_point_info
        WHERE shop_id = #{shopId}
        AND line_id = #{lineId}
        AND deleted = 0
    </select>
    <select id="MiniPointPageVO" resultType="com.cool.store.vo.point.MiniPointPageVO">
        select a.id as pointId,
        a.point_name as pointName,
        a.point_code as pointCode,
        a.longitude as longitude,
        a.latitude as latitude,
        a.address as address,
        a.create_time as createTime,
        a.opportunity_point_code as opportunityPointCode,
        a.opportunity_point_name as opportunityPointName,
        c.picture_obj as pictureObj,
        a.point_area as pointArea,
        c.landlord_mobile as landlordMobile,
        c.month_rent as monthRent,
        d.store_name as openShopName
        from xfsg_point_info a
        left JOIN xfsg_shop_info b ON a.shop_id = b.id
        LEFT JOIN xfsg_point_detail_info c on a.id = c.point_id
        left JOIN store_${enterpriseId} d ON b.shop_code = d.store_num
        where a.deleted = 0
        <if test="request.areaCode!=null and request!=''">
            and (a.province_code = #{request.areaCode} or a.city_code = #{request.areaCode} or a.district_code =
            #{request.areaCode})
        </if>
    </select>
    <select id="getPointByOpportunityPointCode" resultType="com.cool.store.vo.point.MiniPointPageVO">
        select a.id as pointId,
        a.point_name as pointName,
        a.point_code as pointCode,
        a.longitude as longitude,
        a.latitude as latitude,
        a.address as address,
        a.create_time as createTime,
        a.opportunity_point_code as opportunityPointCode,
        a.opportunity_point_name as opportunityPointName,
        c.picture_obj as pictureObj,
        a.point_area as pointArea,
        c.landlord_mobile as landlordMobile,
        c.month_rent as monthRent
        from xfsg_point_info a
        LEFT JOIN xfsg_point_detail_info c on a.id = c.point_id
        where a.opportunity_point_code = #{code}
    </select>
    <select id="getShopByOpportunityPointCode" resultType="com.cool.store.dto.MiniShopDTO">
        select c.store_name as shopName,
        c.store_address as address
        from xfsg_point_info a
        inner join xfsg_shop_info b on a.shop_id = b.id
        inner join store_${enterpriseId} c on b.shop_code = c.store_num
        where a.opportunity_point_code = #{code}
    </select>
    <select id="getRecommendOrMyList" resultType="com.cool.store.vo.point.MiniPointPageVO">
        select
        a.id as pointId,
        a.point_name as pointName,
        a.point_code as pointCode,
        a.longitude as longitude,
        a.latitude as latitude,
        a.address as address,
        a.create_time as createTime,
        a.opportunity_point_code as opportunityPointCode,
        a.opportunity_point_name as opportunityPointName,
        c.picture_obj as pictureObj,
        a.point_area as pointArea,
        c.landlord_mobile as landlordMobile,
        c.month_rent as monthRent
        from xfsg_point_info a
        LEFT JOIN xfsg_point_detail_info c on a.id = c.point_id
        where a.deleted = 0
        <if test="request.keyword!=null and request.keyword !=''">
            and a.point_name like concat('%', #{request.keyword}, '%')
        </if>
        <if test="request.type == 1">
            and a.point_status in( 4,5)
        </if>
        <if test="request.type == 2">
            and a.line_id = #{request.lineId}
        </if>
        <if test="request.areaCode!=null and request!=''">
            and (a.province_code = #{request.areaCode} or a.city_code = #{request.areaCode} or a.district_code =
            #{request.areaCode})
        </if>
    </select>
    <select id="getPointAndDetailByIds" resultType="com.cool.store.dto.PointInfoDTO">
        select a.id as pointId,
        a.province as province,
        a.city as city,
        a.district as district,
        a.point_area as pointArea,
        b.month_rent as monthRent
        from xfsg_point_info a
        left join xfsg_point_detail_info b on a.id = b.point_id
        where a.id in
        <foreach collection="pointIds" item="pointId" index="index" open="(" separator="," close=")">
            #{pointId}
        </foreach>
    </select>

    <update id="recyclePoint">
        update xfsg_point_info set line_id = null, shop_id = null, select_status = 0, update_time = now(), point_status
        = if(point_status = 6, 5, point_status) where id = #{pointId}
    </update>

    <update id="perfectPointInfo">
        update
        xfsg_point_info
        set
        point_name = #{request.pointName},
        region_id = #{request.regionId},
        point_area = #{request.pointArea},
        latitude = #{request.latitude},
        longitude = #{request.longitude},
        address = #{request.address},
        province = #{request.province},
        city = #{request.city},
        district = #{request.district},
        township = #{request.township},
        point_score = #{request.pointScore},
        opportunity_point_code = #{request.opportunityPointCode},
        opportunity_point_name = #{request.opportunityPointName},
        province_code = #{request.provinceCode},
        city_code = #{request.cityCode},
        district_code = #{request.districtCode},
        point_location = #{request.pointLocation}
        where id = #{request.id}
    </update>
    <update id="updateAddress">
        <foreach collection="list" item="item" index="index" separator=";">
            update xfsg_point_info
            <set>
                <if test="item.province != null and item.province !=''">province = #{item.province},</if>
                <if test="item.address !=null and item.address !=''">address = #{item.address},</if>
                <if test="item.city !=null and item.city !=''">city = #{item.city},</if>
                <if test="item.district !=null and item.district !=''">district = #{item.district},</if>
                <if test="item.pointArea !=null and item.pointArea!=''">
                    point_area = #{item.pointArea}
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

</mapper>