<?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.OperationLogMapper">

    <resultMap id="BaseResultMap" type="com.cool.store.entity.OperationLogDO">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="shopId" column="shop_id" jdbcType="BIGINT"/>
        <result property="shopSubStage" column="shop_sub_stage" jdbcType="INTEGER"/>
        <result property="shopSubStageStatus" column="shop_sub_stage_status" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
        <result property="operatorName" column="operator_name" jdbcType="VARCHAR"/>
        <result property="operator" column="operator" jdbcType="VARCHAR"/>
        <result property="auditResultId" column="audit_result_id" jdbcType="BIGINT"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createUserId" column="create_user_id" jdbcType="VARCHAR"/>
        <result property="updateUserId" column="update_user_id" jdbcType="VARCHAR"/>
        <result property="deleted" column="deleted" jdbcType="TINYINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,shop_id,shop_sub_stage,
        shop_sub_stage_status,status,type,remarks,
        operator_name,operator,audit_result_id,
        create_time,update_time,create_user_id,
        update_user_id,deleted
    </sql>
    <insert id="batchAddOperationLog">
        <foreach collection="list" separator=";" item="item">
            INSERT INTO xfsg_operation_log( shop_id, shop_sub_stage, shop_sub_stage_status, status, type, remarks, operator_name,operator,audit_result_id,create_time,create_user_id,update_user_id,update_time)
            VALUES(#{item.shopId}, #{item.shopSubStage}, #{item.shopSubStageStatus}, #{item.status}, #{item.type}, #{item.remarks}, #{item.operatorName}, #{item.operator},#{item.auditResultId}
            ,#{item.createTime},#{item.createUserId},#{item.updateUserId},#{item.updateTime})
        </foreach>
    </insert>
    <select id="getOrderByTime" resultType="com.cool.store.entity.OperationLogDO">
        select *
        from xfsg_operation_log
        where shop_id = #{shopId}
        and shop_sub_stage_status = #{shopSubStageStatus}
        <if test="type !=null">
            and type = #{type}
        </if>
        and audit_result_id is not null
        order by create_time desc limit 1
    </select>
    <select id="getByShopSubStage" resultType="com.cool.store.entity.OperationLogDO">
        select *
        from xfsg_operation_log
        where shop_id = #{shopId}
        <if test="shopSubStageList !=null and shopSubStageList.size()>0">
            and shop_sub_stage in
            <foreach collection="shopSubStageList" index="index" item="shopSubStage" open="(" close=")"
                     separator=",">
                #{shopSubStage}
            </foreach>
        </if>
        <if test="types !=null and types.size()>0">
            and type in
            <foreach collection="types" index="index" item="type" open="(" close=")" separator=",">
                #{type}
            </foreach>
        </if>
    </select>
    <select id="getBySubStageStatusEnumAndsStatus" resultType="com.cool.store.entity.OperationLogDO">
        select *
        from xfsg_operation_log
        where shop_id = #{shopId}
        and shop_sub_stage_status = #{shopSubStageStatus}
        <if test="type !=null">
            and type = #{type}
        </if>
        and status = 0
        order by create_time desc
    </select>
    <select id="getBySubStageStatusAndUser" resultType="com.cool.store.entity.OperationLogDO">
        select *
        from xfsg_operation_log a
        left join xfsg_shop_info b on a.shop_id = b.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
        b.deleted=0
        and b.shop_status !=2
        and a.operator = #{userId}
        and a.shop_sub_stage_status in
        <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and a.status = 0
        <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="request.shopKeyword !=null and request.shopKeyword !=''">
            and (b.shop_name like concat('%',#{request.shopKeyword},'%') or b.shop_code like concat('%',#{request.shopKeyword},'%'))
        </if>
        <if test="request.regionIds !=null and request.regionIds.size()>0">
            and b.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 b.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 b.investment_manager = #{request.supervisorId}
        </if>
        <if test="request.joinModes !=null and request.joinModes.size()>0">
            and b.join_mode in
            <foreach collection="request.joinModes" item="joinMode" index="index" open="(" separator="," close=")">
                #{joinMode}
            </foreach>
        </if>
        order by a.create_time desc
    </select>
    <select id="getByCondition" resultType="com.cool.store.entity.OperationLogDO">
        select *
        from xfsg_operation_log
        where shop_id = #{shopId}
          and shop_sub_stage_status = #{shopSubStageStatus}
          and audit_result_id is NOT null
        order by create_time desc limit 1
    </select>
</mapper>
