<?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.aucma.base.mapper.BaseSpaceDetailMapper">

    <resultMap type="BaseSpaceDetail" id="BaseSpaceDetailResult">
        <result property="objId" column="obj_id"/>
        <result property="storeCode" column="store_code"/>
        <result property="storeName" column="store_name"/>
        <result property="spaceCode" column="space_code"/>
        <result property="spaceName" column="space_name"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="materialType" column="material_type"/>
        <result property="materialAmount" column="material_amount"/>
        <result property="isFlag" column="is_flag"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
    </resultMap>

    <sql id="selectBaseSpaceDetailVo">
        select sd.obj_id,
               sd.store_code,
               si.store_name,
               sd.space_code,
               sp.space_name,
               sd.material_code,
               sd.material_name,
               sd.material_type,
               sd.material_amount,
               sd.is_flag,
               sd.created_by,
               sd.created_time,
               sd.updated_by,
               sd.updated_time
        from base_spacedetail sd
                 left join BASE_STOREINFO si on si.STORE_CODE = sd.STORE_CODE
                 left join BASE_SPACEINFO sp on sp.SPACE_CODE = sd.SPACE_CODE
    </sql>

    <select id="selectBaseSpaceDetailList" parameterType="BaseSpaceDetail" resultMap="BaseSpaceDetailResult">
        <include refid="selectBaseSpaceDetailVo"/>
        <where>
            <if test="storeCode != null  and storeCode != ''">and sd.store_code = #{storeCode}</if>
            <if test="spaceCode != null  and spaceCode != ''">and sd.space_code = #{spaceCode}</if>
            <if test="materialCode != null  and materialCode != ''">and sd.material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and sd.material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="materialType != null  and materialType != ''">and sd.material_type = #{materialType}</if>
            <if test="materialAmount != null ">and sd.material_amount = #{materialAmount}</if>
            <if test="isFlag != null ">and sd.is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and sd.created_by = #{createdBy}</if>
            <if test="createdTime != null ">and sd.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and sd.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and sd.updated_time = #{updatedTime}</if>
        </where>
    </select>

    <select id="selectBaseSpaceDetailByObjId" parameterType="Long" resultMap="BaseSpaceDetailResult">
        <include refid="selectBaseSpaceDetailVo"/>
        where sd.obj_id = #{objId}
    </select>

    <insert id="insertBaseSpaceDetail" parameterType="BaseSpaceDetail">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_base_spacedetail.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_spacedetail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="storeCode != null">store_code,</if>
            <if test="spaceCode != null">space_code,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialType != null">material_type,</if>
            <if test="materialAmount != null">material_amount,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="storeCode != null">#{storeCode},</if>
            <if test="spaceCode != null">#{spaceCode},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="materialAmount != null">#{materialAmount},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateBaseSpaceDetail" parameterType="BaseSpaceDetail">
        update base_spacedetail
        <trim prefix="SET" suffixOverrides=",">
            <if test="storeCode != null">store_code = #{storeCode},</if>
            <if test="spaceCode != null">space_code = #{spaceCode},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="materialAmount != null">material_amount = #{materialAmount},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedBy != null">updated_by = #{updatedBy},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteBaseSpaceDetailByObjId" parameterType="Long">
        delete
        from base_spacedetail
        where obj_id = #{objId}
    </delete>

    <delete id="deleteBaseSpaceDetailByObjIds" parameterType="String">
        delete from base_spacedetail where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>
</mapper>