<?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 and shop_sub_stage !=85
        <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>
    <update id="batchUpdateByShopIdsAndSubStageStatus">
        <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 in
            <foreach collection="shopIds" item="shopId" open="(" separator="," close=")">
                #{shopId}
            </foreach>
            and shop_sub_stage = #{update.shopSubStage}
        </foreach>
    </update>
    <update id="dataUpdateShopStageInfo">
        <foreach collection="list" item="item" separator=";">
            update
            xfsg_shop_stage_info
            set
            shop_sub_stage_status = #{shopSubStageStatus},
            is_terminated = #{isTerminated},
            remark = #{remark},
            actual_complete_time = #{item.endTime}
            where
            shop_id = #{item.shopId}
            and shop_sub_stage = #{shopSubStage}
        </foreach>

    </update>
    <update id="dataUpdateStatus">
        update
        xfsg_shop_stage_info
        set
        shop_sub_stage_status = #{shopSubStageStatus},
        is_terminated = #{isTerminated},
        remark = #{remark},
        actual_complete_time = now()
        where
        shop_id in
        <foreach collection=" list" open="(" separator="," close=")" item="item" index="index">
            #{item}
        </foreach>
        and shop_sub_stage = #{shopSubStage}
        and shop_sub_stage_status != #{shopSubStageStatus}
    </update>
    <update id="dataUpdateAcceptanceStatus">
        update
        xfsg_shop_stage_info
        set
        shop_sub_stage_status = #{shopSubStageStatus},
        is_terminated = #{isTerminated},
        remark = #{remark}
        where
        shop_id in
        <foreach collection=" list" open="(" separator="," close=")" item="item" index="index">
            #{item}
        </foreach>
        and shop_sub_stage = #{shopSubStage}
        and shop_sub_stage_status = -100
    </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,
        a.shop_sub_stage_status as shopSubStageStatus
        from
        xfsg_shop_stage_info a
        inner join xfsg_line_info b on a.line_id = b.id
        left join xfsg_shop_info c on a.shop_id = c.id
        where c.deleted=0
       and 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)-1 as totalColumn,
        sum(if(is_terminated = 1 and shop_sub_stage!=85, 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
           left join xfsg_shop_info si on a.shop_id = si.id
        <if test="request.signTypes != null and request.signTypes.size() >0">
            left join xfsg_sign_franchise sign on a.shop_id = sign.shop_id
        </if>
        <where>
            si.deleted = 0 and a.is_terminated = 0 and si.shop_status !=2
            <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 test = "shopSubStage == 86 or  shopSubStage == 90 or shopSubStage == 110 or shopSubStage == 120">
                and (si.hqt_shop_id is  null or si.hqt_shop_id = '')
            </if>
            </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="request.stageStatus !=null and request.stageStatus.size() >0">
                and a.shop_sub_stage_status in
                <foreach collection="request.stageStatus" item="item" index="index" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="investmentUserId!=null and investmentUserId!='' and authRegionIds.size()==0">
                and si.investment_manager = #{investmentUserId}
            </if>
            <if test="authRegionIds != null and authRegionIds.size() > 0">
                and si.region_id in
                <foreach collection="authRegionIds" item="regionId" index="index" open="(" separator="," close=")">
                    #{regionId}
                </foreach>
            </if>
            <if test="ownShopFlag!=null">
                <if test="ownShopFlag">
                    and si.join_mode = 3
                </if>
                <if test="!ownShopFlag">
                    and si.join_mode != 3
                </if>
            </if>
            <if test="request.shopKeyword !=null and request.shopKeyword !=''">
                and (si.shop_name like concat('%',#{request.shopKeyword},'%') or si.shop_code like concat('%',#{request.shopKeyword},'%'))
            </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.storeTypes !=null and request.storeTypes.size()>0">
                and si.store_type in
                <foreach collection="request.storeTypes" item="storeType" index="index" open="(" separator="," close=")">
                    #{storeType}
                </foreach>
            </if>
            <if test="request.signTypes !=null and request.signTypes.size()>0">
                and sign.sign_type in
                <foreach collection="request.signTypes" item="signType" index="index" open="(" separator="," close=")">
                    #{signType}
                </foreach>
            </if>
            <if test="request.supervisorId != null and request.supervisorId!= ''">
                and si.investment_manager = #{request.supervisorId}
            </if>
            <if test="request.joinModes !=null and request.joinModes.size()>0">
                and si.join_mode in
                <foreach collection="request.joinModes" item="joinMode" index="index" open="(" separator="," close=")">
                    #{joinMode}
                </foreach>
            </if>
            order by a.update_time desc
        </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>
    <select id="getPlatformBuildList" resultType="com.cool.store.dto.Preparation.ScheduleDTO">
        select
        shop_id as shopId,
        count(1) as totalColumn,
        sum(if(is_terminated = 1, 1, 0)) as completionColumn
        from xfsg_shop_stage_info where shop_stage = 3
        <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="getPlatformBuildStage" resultType="com.cool.store.dto.PlatformBuildStageDTO">
        select shop_id as shopId,
        shop_sub_stage as shopSubStage,
        shop_sub_stage_status as shopSubStageStatus
        from xfsg_shop_stage_info
        where shop_stage = 3
        and shop_sub_stage_status != -100
        and shop_id in
        <foreach collection="shopIds" item="shopId" index="index" open="(" separator="," close=")">
            #{shopId}
        </foreach>

    </select>
    <select id="getSubStages" resultType="com.cool.store.entity.ShopStageInfoDO">
        select *
        from xfsg_shop_stage_info
        where is_terminated = 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="getOpenActivityActualCompletionTime" resultType="com.cool.store.entity.ShopStageInfoDO">
        select
        *
        from xfsg_shop_stage_info where shop_stage = 2 and shop_sub_stage = 140 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="getAllNumber" resultType="java.lang.Integer">
        select count(*)
        from xfsg_shop_stage_info
        where shop_stage = 2
        <if test="flag !=null and flag = 1">
            and is_terminated =1
        </if>
        and shop_sub_stage != 85
        <if test="shopId !=null">
            and shop_id = #{shopId}
        </if>
    </select>
    <select id="getScheduleAll" resultType="com.cool.store.dto.Preparation.ScheduleDTO">
        select
        shop_id as shopId,
        max(plan_complete_time) as planCompleteTime,
        count(1)-1 as totalColumn,
        sum(if(is_terminated = 1 and shop_sub_stage!=85, 1, 0)) as completionColumn
        from xfsg_shop_stage_info where shop_stage = 2 and
        shop_id = #{shopId}


    </select>
    <select id="getCompletionCount" 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_stage in (1,2)
    </select>
    <select id="getShopSubStages" resultType="com.cool.store.entity.ShopStageInfoDO">
        select shop_id as shopId,
        shop_sub_stage as shopSubStage,
        shop_sub_stage_status as shopSubStageStatus,
        is_terminated as isTerminated,
        actual_complete_time as actualCompleteTime
        from xfsg_shop_stage_info where shop_stage = 2 or shop_stage = 1  or shop_sub_stage = 230 or shop_sub_stage = 240
        <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>
</mapper>