<?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" />
  </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
  </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" resultMap="BaseResultMap">
    select
      id,
      point_name,
      point_code,
      address,
      region_id,
      point_status,
      point_score,
      point_area,
      development_manager,
      operate_user_id,
      development_time,
      select_status,
      create_time,
      update_time
    from xfsg_point_info
    where deleted = 0 and development_manager = #{request.developmentManager}
    <if test="request.keyword != null and request.keyword != ''">
      and (point_code like concat('%', #{request.keyword}, '%') or point_name like concat('%', #{request.keyword}, '%'))
    </if>
    <if test="request.developmentStartTime != null and request.developmentStartTime != ''">
      and development_time >= #{request.developmentStartTime}
    </if>
    <if test="request.developmentEndTime != null and request.developmentEndTime != ''">
      <![CDATA[and development_time <= #{request.developmentEndTime}]]>
    </if>
    <if test="request.pointStatusList != null and request.pointStatusList.size() > 0">
      and point_status in
      <foreach collection="request.pointStatusList" item="pointStatus" open="(" close=")" separator=",">
            #{pointStatus}
      </foreach>
    </if>
    <if test="request.storageStatus != null">
      and storage_status = #{request.storageStatus}
    </if>
    <if test="request.operateUserId != null and request.operateUserId != ''">
      and operate_user_id = #{request.operateUserId}
    </if>
    <if test="request.createStartTime != null and request.createStartTime != ''">
      and create_time >= #{request.createStartTime}
    </if>
    <if test="request.createEndTime != null and request.createEndTime != ''">
      <![CDATA[and create_time <= #{request.createEndTime}]]>
    </if>
    <if test="request.regionIds != null and request.regionIds.size() > 0">
      and region_id in
      <foreach collection="request.regionIds" item="regionId" index="index" open="(" separator="," close=")">
        #{regionId}
      </foreach>
    </if>
  </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.entity.PointInfoDO">
    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
      line_id = #{lineId} and select_status = '1' and deleted = 0
  </update>

  <select id="getTeamPointPage" resultMap="BaseResultMap">
    select
      id,
      point_name,
      point_code,
      address,
      region_id,
      point_status,
      point_score,
      point_area,
      development_manager,
      operate_user_id,
      development_time,
      select_status,
      create_time,
      update_time
    from
      xfsg_point_info
    where deleted = 0 and point_status in (4,5,6,7)
      <if test="request.keyword != null and request.keyword != ''">
        and (point_code like concat('%', #{request.keyword}, '%') or point_name like concat('%', #{request.keyword}, '%'))
      </if>
      <if test="request.developmentManager != null and request.developmentManager != ''">
        and development_manager = #{request.developmentManager}
      </if>
      <if test="request.pointStatus != null and request.pointStatus != ''">
        and point_status = #{request.pointStatus}
      </if>
      <if test="request.createStartTime != null and request.createStartTime != ''">
        and create_time >= #{request.createStartTime}
      </if>
      <if test="request.createEndTime != null and request.createEndTime != ''">
        <![CDATA[and create_time <= #{request.createEndTime}]]>
      </if>
      <if test="request.regionIds != null and request.regionIds.size() > 0">
        and 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 region_id in
        <foreach collection="request.authRegionIds" item="regionId" index="index" open="(" separator="," close=")">
          #{regionId}
        </foreach>
      </if>
  </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>

    <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}
    where id = #{request.id}
  </update>

</mapper>