<?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.ShopStageInfoMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.ShopStageInfoDO">
    <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="shop_stage" jdbcType="TINYINT" property="shopStage" />
    <result column="shop_sub_stage" jdbcType="TINYINT" property="shopSubStage" />
    <result column="shop_sub_stage_status" jdbcType="TINYINT" property="shopSubStageStatus" />
    <result column="is_terminated" jdbcType="BIT" property="isTerminated" />
    <result column="plan_complete_time" jdbcType="VARCHAR" property="planCompleteTime" />
    <result column="actual_complete_time" jdbcType="VARCHAR" property="actualCompleteTime" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="audit_id" jdbcType="BIGINT" property="auditId" />
    <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, shop_stage, shop_sub_stage, shop_sub_stage_status, is_terminated, plan_complete_time, actual_complete_time, remark, audit_id, deleted, create_time, update_time
  </sql>

  <insert id="batchInsert">
    <foreach collection="addShopStageList" separator=";" item="shop">
      INSERT INTO xfsg_shop_stage_info(line_id, shop_id, shop_stage, shop_sub_stage, shop_sub_stage_status, is_terminated, plan_complete_time, remark)
      VALUES(#{shop.lineId}, #{shop.shopId}, #{shop.shopStage}, #{shop.shopSubStage}, #{shop.shopSubStageStatus}, #{shop.isTerminated}, #{shop.planCompleteTime}, #{shop.remark})
    </foreach>
  </insert>


  <update id="batchUpdate">
    update xfsg_shop_stage_info
    <set>
      shop_sub_stage_status = CASE id
      <foreach collection="addShopStageList" separator=" " item="item">
        WHEN #{item.id} THEN IFNULL(#{item.shopSubStageStatus},shop_sub_stage_status)
      </foreach>
      END
    </set>
    where id in (
    <foreach collection="addShopStageList" item="item" separator=",">
      #{item.id}
    </foreach>
    )
  </update>

  <select id="getShopStageInfo" resultMap="BaseResultMap">
    select
      <include refid="allColumn"/>
    from
      xfsg_shop_stage_info
    where
      shop_id = #{shopId} and deleted = 0
      <if test="shopStage != null">
        and shop_stage = #{shopStage}
      </if>
  </select>

  <select id="getPreparationProcess" resultType="com.cool.store.vo.Preparation.PreparationProcessVO">
    select
    max(plan_complete_time) as planStartTime,
    sum(if(is_terminated = 1, 1, 0)) as finishCount
    from xfsg_shop_stage_info where shop_id = #{shopId}
  </select>

  <select id="getAllCompletionCount" resultType="java.lang.Integer">
    select
    count(1)
    from
    xfsg_shop_stage_info
    where
    shop_id = #{shopId} and deleted = 0 and is_terminated = 1
    and shop_sub_stage in (60,40,120,140,150)
  </select>

  <update id="updateShopStageInfo">
    update
      xfsg_shop_stage_info
    set
      shop_sub_stage_status = #{shopSubStageStatus},
      is_terminated = #{isTerminated},
      remark = #{remark},
      actual_complete_time = if(is_terminated, now(), null)
    where
      shop_id = #{shopId} and shop_sub_stage = #{shopSubStage}
  </update>


  <update id="batchUpdateShopStageStatus">
    <foreach collection="updateList" separator=";" item="update">
      update
        xfsg_shop_stage_info
      set
        shop_sub_stage_status = #{update.shopSubStageStatus},
        is_terminated = #{update.isTerminated},
        remark = #{update.remark},
        actual_complete_time = if(is_terminated, now(), null)
      where
        shop_id = #{shopId} and shop_sub_stage = #{update.shopSubStage}
    </foreach>
  </update>

  <select id="getShopSubStageInfo" resultMap="BaseResultMap">
    select
      <include refid="allColumn"/>
    from
      xfsg_shop_stage_info
    where
      shop_id = #{shopId} and shop_sub_stage = #{shopSubStage} and deleted = 0
  </select>

  <update id="updateShopStageAndAuditInfo">
    update
      xfsg_shop_stage_info
    set
      shop_sub_stage_status = #{shopSubStageStatus},
      is_terminated = #{isTerminated},
      remark = #{remark},
      actual_complete_time = if(is_terminated, now(), null),
      audit_id = #{auditId}
    where
      shop_id = #{shopId} and shop_sub_stage = #{shopSubStage}
  </update>

  <update id="updateShopStageToNotStarted">
    update
      xfsg_shop_stage_info
    set
      shop_sub_stage_status = -100,
      is_terminated = 0,
      remark = '未开始',
      actual_complete_time = null,
      audit_id = null
    where
      shop_id = #{shopId} and shop_sub_stage = #{shopSubStage}
  </update>
  <update id="updateByShopId">
      update xfsg_shop_stage_info
      <set>
      <if test="shopStageInfoDO.planCompleteTime">
          plan_complete_time = #{shopStageInfoDO.planCompleteTime},
      </if>
      <if test="shopStageInfoDO.actualCompleteTime">
          actual_complete_time = #{shopStageInfoDO.actualCompleteTime},
      </if>
      </set>
    where shop_id = #{shopStageInfoDO.shopId} AND shop_sub_stage = #{shopStageInfoDO.shopSubStage}

  </update>
  <update id="batchUpdateStatus">
    update xfsg_shop_stage_info
    <set>

    <if test="shopSubStageStatusEnum !=null">
      shop_sub_stage_status = #{shopSubStageStatusEnum}
    </if>
    </set>
    where shop_id in
    <foreach collection="shopIdList" item="shopId" open="(" separator="," close=")">
      #{shopId}
    </foreach>
    <if test="shopSubStageEnum != null">
      and shop_sub_stage = #{shopSubStageEnum}
    </if>
  </update>

  <select id="getRentContractToDoPage" resultType="com.cool.store.vo.shop.RentInfoToDoVO">
    select
      b.id as lineId,
      b.username as lineUsername,
      b.mobile as lineMobile,
      a.shop_id as shopId
    from
      xfsg_shop_stage_info a
          inner join xfsg_line_info b on a.line_id = b.id
    where
      a.shop_sub_stage = #{shopSubStage} and a.shop_sub_stage_status = #{shopSubStageStatus} and a.deleted = 0 and b.deleted = 0 and b.development_manager = #{userId}
  </select>


  <select id="getScheduleList" resultType="com.cool.store.dto.Preparation.ScheduleDTO">
    select
    shop_id as shopId,
    max(plan_complete_time) as planCompleteTime,
    count(1) as totalColumn,
    sum(if(is_terminated = 1, 1, 0)) as completionColumn
    from xfsg_shop_stage_info   where shop_stage = 2
    <if test="shopIds != null and shopIds.size() > 0">
      and shop_id in
      <foreach collection="shopIds" item="shopId" index="index" open="(" separator="," close=")">
        #{shopId}
      </foreach>
    </if>
    group by shop_id
  </select>


  <select id="getShopContractActualCompletionTime" resultMap="BaseResultMap">
    select
    *
    from xfsg_shop_stage_info   where shop_stage = 1 and shop_sub_stage = 20 and actual_complete_time is not null
    <if test="shopIds != null and shopIds.size() > 0">
      and shop_id in
      <foreach collection="shopIds" item="shopId" index="index" open="(" separator="," close=")">
        #{shopId}
      </foreach>
    </if>
  </select>
  
  <select id="getCanSubmitRentContractShopIds" resultType="long">
    select
      shop_id
    from
      xfsg_shop_stage_info
    where shop_sub_stage_status in(200, 220) and shop_id in
    <foreach collection="shopIds" item="shopId" index="index" open="(" separator="," close=")">
      #{shopId}
    </foreach>
  </select>
  <select id="getShopContractIncompletion" resultType="java.lang.Long">
    SELECT
      shop_id
    FROM
      xfsg_shop_stage_info
    WHERE
      shop_sub_stage = 110
      AND (shop_sub_stage_status = 1100 OR shop_sub_stage_status = 1110)
  </select>


  <select id="getSpecialShopStageInfo" resultMap="BaseResultMap">
    select
    *
    from xfsg_shop_stage_info a
    <if test="investmentUserId!=null and investmentUserId!='' and queryUserType == 0 ">
      left join xfsg_line_info b on a.line_id = b.id
    </if>
    <if test="investmentUserId!=null and investmentUserId!='' and queryUserType == 1 ">
      left join xfsg_system_building_shop c on a.shop_id = c.shop_id
    </if>
    <where>
      <if test="shopIds != null and shopIds.size() > 0">
        and a.shop_id in
        <foreach collection="shopIds" item="shopId" index="index" open="(" separator="," close=")">
          #{shopId}
        </foreach>
      </if>
      <if test="shopSubStage!=null">
        and a.shop_sub_stage = #{shopSubStage}
      </if>
      <if test="shopSubStageStatusList != null and shopSubStageStatusList.size() > 0">
        and a.shop_sub_stage_status in
        <foreach collection="shopSubStageStatusList" item="stageStatus" index="index" open="(" separator="," close=")">
          #{stageStatus}
        </foreach>
      </if>
      <if test="investmentUserId!=null and investmentUserId!=''  and queryUserType == 0">
        and  b.investment_manager = #{investmentUserId}
      </if>
      <if test="supervisorId!=null and supervisorId!=''  and queryUserType == 1">
        and  c.supervisor_id = #{supervisorId}
      </if>
    </where>

  </select>
  <select id="getSubStageList" resultType="com.cool.store.entity.ShopStageInfoDO">
    select *
    from xfsg_shop_stage_info
    where 1=1
    <if test="shopIds !=null and shopIds.size()>0">
      and shop_id in
      <foreach collection="shopIds" item="shopId" index="index" open="(" separator="," close=")">
        #{shopId}
      </foreach>
    </if>
    <if test="shopSubStage !=null">
     and  shop_sub_stage =#{shopSubStage}
    </if>
  </select>
  <select id="getShopIdListByStageStatus" resultType="com.cool.store.entity.ShopStageInfoDO">
    select *
    from xfsg_shop_stage_info
    where  shop_sub_stage_status = #{shopSubStageStatus}
  </select>
  
  <select id="getShopCountByLineIdAndStageStatus" resultType="integer">
    select count(1) from xfsg_shop_stage_info where shop_stage = #{shopStage} and shop_sub_stage = #{shopSubStage} and shop_sub_stage_status = #{shopSubStageStatus} and line_id = #{lineId}
  </select>
  <select id="getSubStageListBySubStageStatus" resultType="com.cool.store.entity.ShopStageInfoDO">
    select *
    from xfsg_shop_stage_info
    where 1=1
    <if test="subStageStatusA !=null">
      or shop_sub_stage_status = #{subStageStatusA}
    </if>
    <if test="subStageStatusB !=null">
      or shop_sub_stage_status = #{subStageStatusB}
    </if>
    <if test="subStageStatusC !=null">
      and shop_sub_stage_status = #{subStageStatusC}
    </if>
  </select>

  <select id="getByShopIdAndSubStage" resultType="com.cool.store.entity.ShopStageInfoDO">
    SELECT <include refid="allColumn"/>
    FROM xfsg_shop_stage_info
    WHERE shop_id = #{shopId}
    AND shop_sub_stage = #{shopSubStage}
  </select>
</mapper>