<?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.OpeningOperationPlanMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.OpeningOperationPlanDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="shop_id" jdbcType="BIGINT" property="shopId" />
    <result column="plan_source" jdbcType="BIT" property="planSource" />
    <result column="survey_result" jdbcType="VARCHAR" property="surveyResult" />
    <result column="survey_result_url" jdbcType="VARCHAR" property="surveyResultUrl" />
    <result column="activity_theme" jdbcType="VARCHAR" property="activityTheme" />
    <result column="activity_theme_url" jdbcType="VARCHAR" property="activityThemeUrl" />
    <result column="submission_time" jdbcType="TIMESTAMP" property="submissionTime" />
    <result column="submitted_user_id" jdbcType="VARCHAR" property="submittedUserId" />
    <result column="preparation_user_ids" jdbcType="VARCHAR" property="preparationUserIds" />
    <result column="route_completed" jdbcType="TINYINT" property="routeCompleted" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="create_user_id" jdbcType="VARCHAR" property="createUserId" />
    <result column="update_user_id" jdbcType="VARCHAR" property="updateUserId" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="audit_id" jdbcType="BIGINT" property="auditId" />
    <result column="result_type" jdbcType="TINYINT" property="resultType"/>
  </resultMap>
  <sql id="Base_Column_List">
    id,shop_id,plan_source,survey_result,survey_result_url,activity_theme,activity_theme_url,
      submission_time,submitted_user_id,preparation_user_ids,route_completed,create_time,
      update_time,create_user_id,update_user_id,deleted,audit_id,result_type
  </sql>
  <update id="updateByShopId">
    update  xfsg_opening_operation_plan
    set audit_id = #{auditId},result_type = #{resultType},update_time = NOW()
    where shop_id = #{shopId}
  </update>

  <select id="selectByShopId" resultType="com.cool.store.entity.OpeningOperationPlanDO">
    select
        <include refid="Base_Column_List"/>
    from
        xfsg_opening_operation_plan
    where shop_id = #{shopId}
        and
          deleted = 0
  </select>
  <select id="getOpenPlanShopListByCondition"
          resultType="com.cool.store.dto.openPreparation.OpenPlanShopInfoDTO">
      select si.id as shopId, si.region_id AS regionId,si.line_id as lineId, si.shop_name as shopName,
      si.store_num as storeNum,si.shop_code as shopCode, si.shop_manager_user_id as shopManagerUserId,op.submission_time AS submissionTime ,
      op.result_type AS resultType,si.supervisor_user_id as supervisorUserId
      from xfsg_opening_operation_plan op
      join xfsg_shop_info si on si.id = op.shop_id
      where si.deleted = 0
      <if test="request.shopName != null and request.shopName != '' ">
          AND si.shop_name like concat('%', #{request.shopName}, '%')
      </if>
      <if test="request.resultType != null ">
          AND op.result_type = #{request.resultType}
      </if>
      <if test="request.planStartDate == null and request.planEndDate == null">
          and op.create_time >=  DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
      </if>
      <if test="request.planStartDate != null">
          and op.create_time >= #{request.planStartDate}
      </if>
      <if test="request.planEndDate != null">
          AND op.create_time &lt;= #{request.planEndDate}
      </if>
      <if test="request.regionIds != null and request.regionIds.size() > 0">
          and si.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 si.region_id in
          <foreach collection="request.authRegionIds" item="regionId" index="index" open="(" separator="," close=")">
              #{regionId}
          </foreach>
      </if>
      order by op.id desc
  </select>


</mapper>