<?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.HyInspectionSettingMappingMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.HyInspectionSettingMappingDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="inspection_setting_id" jdbcType="BIGINT" property="inspectionSettingId"/>
        <result column="open_area_mapping_id" jdbcType="BIGINT" property="openAreaMappingId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="deleted" jdbcType="BIT" property="deleted" />
    </resultMap>
    <sql id="Base_Column_List">
        id, inspection_setting_id, open_area_mapping_id, create_time, update_time, deleted
    </sql>
    <insert id="batchInsertSelective">
        <foreach collection="insertList" item="record" separator=";">
            insert into hy_inspection_setting_mapping
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="record.inspectionSettingId != null">
                    inspection_setting_id,
                </if>
                <if test="record.openAreaMappingId != null">
                    open_area_mapping_id,
                </if>
                <if test="record.createTime != null">
                    create_time,
                </if>
                <if test="record.updateTime != null">
                    update_time,
                </if>
                <if test="record.deleted != null">
                    deleted,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="record.inspectionSettingId != null">
                    #{record.inspectionSettingId},
                </if>
                <if test="record.openAreaMappingId != null">
                    #{record.openAreaMappingId},
                </if>
                <if test="record.createTime != null">
                    #{record.createTime},
                </if>
                <if test="record.updateTime != null">
                    #{record.updateTime},
                </if>
                <if test="record.deleted != null">
                    #{record.deleted},
                </if>
            </trim>
            ON DUPLICATE KEY UPDATE update_time = now(), deleted = values(deleted)
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective">
        update hy_inspection_setting_mapping
        <set>
            <if test="inspectionSettingId != null">
                inspection_setting_id = #{inspectionSettingId},
            </if>
            <if test="openAreaMappingId != null">
                open_area_mapping_id = #{openAreaMappingId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        where id = #{id}
    </update>

    <select id="getOpenAreaMappingIds" resultType="long">
        select open_area_mapping_id from hy_inspection_setting_mapping where inspection_setting_id = #{inspectionSettingId} and deleted = '0'
    </select>
    
    <update id="deleteNotInOpenAreaMappingIds">
        update
            hy_inspection_setting_mapping
        set
            deleted = '1' , update_time = now()
        where
            inspection_setting_id = #{inspectionSettingId} and open_area_mapping_id not in
            <foreach collection="openAreaMappingIds" separator="," open="(" close=")" item="openAreaMappingId" >#{openAreaMappingId}</foreach>
    </update>

    <update id="deleteInspectionSettingMapping">
        update hy_inspection_setting_mapping set deleted = '1' , update_time = now() where inspection_setting_id = #{inspectionSettingId}
    </update>
    
    <select id="getConflictInspectionSetting" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            hy_inspection_setting_mapping
        where
            deleted = '0'
        and
            open_area_mapping_id in
            <foreach collection="openAreaMappingIds" separator="," open="(" close=")" item="openAreaMappingId" >#{openAreaMappingId}</foreach>
        <if test="inspectionSettingId != null">
            and inspection_setting_id != #{inspectionSettingId}
        </if>
    </select>

    <update id="deleteInOpenAreaMappingIds">
        update
            hy_inspection_setting_mapping
        set
            deleted = '1' , update_time = now()
        where
            open_area_mapping_id in <foreach collection="openAreaMappingIds" separator="," open="(" close=")" item="openAreaMappingId" >#{openAreaMappingId}</foreach>
            <if test="excludeInspectionSettingId != null">
                and inspection_setting_id != #{excludeInspectionSettingId}
            </if>
    </update>
</mapper>