<?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.closeup.CloseUpApplyFormMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.closeup.CloseUpApplyFormDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="type" jdbcType="TINYINT" property="type" />
    <result column="store_id" jdbcType="VARCHAR" property="storeId" />
    <result column="apply_no" jdbcType="VARCHAR" property="applyNo" />
    <result column="line_id" jdbcType="VARCHAR" property="lineId" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="plan_close_date" jdbcType="DATE" property="planCloseDate" />
    <result column="actual_close_date" jdbcType="DATE" property="actualCloseDate" />
    <result column="plan_open_date" jdbcType="DATE" property="planOpenDate" />
    <result column="actual_open_date" jdbcType="TIMESTAMP" property="actualOpenDate" />
    <result column="user_id" jdbcType="VARCHAR" property="userId" />
    <result column="reason" jdbcType="VARCHAR" property="reason" />
    <result column="source_apply_id" jdbcType="BIGINT" property="sourceApplyId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime" />
    <result column="urls" jdbcType="LONGVARCHAR" property="urls" />
  </resultMap>
  <sql id="Base_Column_List_A">
    a.id, a.type, a.store_id, a.apply_no, a.line_id, a.status, a.plan_close_date, a.actual_close_date, a.plan_open_date, a.actual_open_date,
        a.user_id, a.reason, a.source_apply_id, a.create_time, a.updated_time, a.urls
  </sql>

  <select id="getList" parameterType="com.cool.store.request.closeup.CloseUpQueryRequest" resultMap="BaseResultMap">
      SELECT <include refid="Base_Column_List_A" />
      FROM zxjp_close_up_apply_form a
      INNER JOIN store_${enterpriseId} b ON a.store_id = b.store_id AND b.is_delete = 'effective'
      <if test="lineKeyword != null and lineKeyword != ''">
        INNER JOIN xfsg_line_info c ON a.line_id = c.id AND c.deleted = 0
      </if>
      <if test="closeUpApplyNo != null and closeUpApplyNo != ''">
        LEFT JOIN zxjp_close_up_apply_form d ON a.source_apply_id = d.id
      </if>
      <where>
        <if test="type != null">
          AND a.type = #{type}
        </if>
        <if test="status != null">
          AND a.status = #{status}
        </if>
        <if test="beginPlanCloseDate != null and endPlanCloseDate!= null">
          AND a.plan_close_date BETWEEN #{beginPlanCloseDate} AND #{endPlanCloseDate}
        </if>
        <if test="beginActualOpenDate != null and endActualOpenDate != null">
          AND a.actual_open_date BETWEEN #{beginActualOpenDate} AND #{endActualOpenDate}
        </if>
        <if test="applyNo != null and applyNo != ''">
          AND a.apply_no LIKE CONCAT('%',#{applyNo},'%')
        </if>
        <if test="storeKeyword != null and storeKeyword != ''">
          AND (b.store_name LIKE CONCAT('%',#{storeKeyword},'%') OR b.store_num LIKE CONCAT('%',#{storeKeyword},'%'))
        </if>
        <if test="joinBrand != null">
          AND b.join_brand = #{joinBrand}
        </if>
        <if test="regionIds != null and regionIds.size() > 0">
          <foreach item="regionId" collection="regionIds" separator=" OR " open="AND (" close=")" index="">
            b.region_path LIKE CONCAT('%/',#{regionId},'/%')
          </foreach>
        </if>
        <if test="lineKeyword != null and lineKeyword != ''">
          AND (c.username LIKE CONCAT('%',#{lineKeyword},'%') OR c.mobile LIKE CONCAT('%',#{lineKeyword},'%'))
        </if>
        <if test="closeUpApplyNo != null and closeUpApplyNo != ''">
          AND d.apply_no LIKE CONCAT('%', #{closeUpApplyNo}, '%')
        </if>
      </where>
      ORDER BY a.create_time DESC
  </select>

  <insert id="insertOrUpdate" parameterType="com.cool.store.entity.closeup.CloseUpApplyFormDO" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO zxjp_close_up_apply_form (
      id, type, store_id, apply_no, line_id, status, plan_close_date, actual_close_date,
      plan_open_date, actual_open_date, user_id, reason, source_apply_id, urls
    ) VALUES (
      #{id}, #{type}, #{storeId}, #{applyNo}, #{lineId}, #{status}, #{planCloseDate}, #{actualCloseDate},
      #{planOpenDate}, #{actualOpenDate}, #{userId}, #{reason}, #{sourceApplyId}, #{urls}
    ) ON DUPLICATE KEY UPDATE
      type = #{type},
      store_id = #{storeId},
      apply_no = #{applyNo},
      line_id = #{lineId},
      status = #{status},
      plan_close_date = #{planCloseDate},
      actual_close_date = #{actualCloseDate},
      plan_open_date = #{planOpenDate},
      actual_open_date = #{actualOpenDate},
      user_id = #{userId},
      reason = #{reason},
      source_apply_id = #{sourceApplyId},
      urls = #{urls}
  </insert>

  <select id="existsTimeOverlap" resultType="int">
    SELECT COUNT(1) FROM zxjp_close_up_apply_form
    WHERE store_id = #{storeId}
      AND type = 0
      AND status != 5
      AND plan_close_date &lt;= #{planOpenDate}
      AND plan_open_date >= #{planCloseDate}
  </select>
</mapper>