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

    <resultMap type="BaseSpaceInfo" id="BaseSpaceInfoResult">
        <result property="objId" column="obj_id"/>
        <result property="spaceCode" column="space_code"/>
        <result property="spaceName" column="space_name"/>
        <result property="spaceCapacity" column="space_capacity"/>
        <result property="spaceStock" column="space_stock"/>
        <result property="spaceStatus" column="space_status"/>
        <result property="spaceType" column="space_type"/>
        <result property="storeCode" column="store_code"/>
        <result property="storeName" column="STORE_NAME"/>
        <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"/>
        <result property="materialType" column="material_type"/>
    </resultMap>

    <sql id="selectBaseSpaceInfoVo">
        select sp.obj_id,
               sp.space_code,
               sp.space_name,
               sp.space_capacity,
               sp.space_stock,
               sp.space_status,
               sp.space_type,
               sp.store_code,
               sp.is_flag,
               sp.created_by,
               sp.created_time,
               sp.updated_by,
               sp.updated_time,
               sp.material_type,
               si.STORE_NAME
        from base_spaceinfo sp
                 left join base_storeinfo si on si.STORE_CODE = sp.STORE_CODE
    </sql>

    <select id="selectBaseSpaceInfoList" parameterType="BaseSpaceInfo" resultMap="BaseSpaceInfoResult">
        <include refid="selectBaseSpaceInfoVo"/>
        <where>
            <if test="spaceCode != null  and spaceCode != ''">and sp.space_code like concat(concat('%', #{spaceCode}),
                '%')
            </if>
            <if test="spaceName != null  and spaceName != ''">and sp.space_name like concat(concat('%', #{spaceName}),
                '%')
            </if>
            <if test="spaceCapacity != null ">and sp.space_capacity = #{spaceCapacity}</if>
            <if test="spaceStock != null ">and sp.space_stock = #{spaceStock}</if>
            <if test="spaceStatus != null ">and sp.space_status = #{spaceStatus}</if>
            <if test="spaceType != null ">and sp.space_type = #{spaceType}</if>
            <if test="storeCode != null  and storeCode != ''">and sp.store_code = #{storeCode}</if>
            <if test="isFlag != null ">and sp.is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and sp.created_by = #{createdBy}</if>
            <if test="createdTime != null ">and sp.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and sp.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and sp.updated_time = #{updatedTime}</if>
        </where>
        order by sp.store_code,sp.space_code
    </select>

    <select id="selectBaseSpaceInfoByObjId" parameterType="Long" resultMap="BaseSpaceInfoResult">
        <include refid="selectBaseSpaceInfoVo"/>
        where sp.obj_id = #{objId}
    </select>

    <select id="getInventoryStatistics" parameterType="BaseSpaceInfo" resultType="java.util.LinkedHashMap">
        select si.store_code       MATERIALCODE,
               si.STORE_NAME       MATERIALNAME,
               sp.material_type    STORECODE,
               ml.MATERIAL_NAME    STORENAME,
               sum(sp.space_stock) SPACESTOCK
        from base_spaceinfo sp
                 left join base_storeinfo si on si.STORE_CODE = sp.STORE_CODE
                 left join BASE_MATERIALINFO ml on ml.MATERIAL_CODE = sp.MATERIAL_TYPE
        where sp.MATERIAL_TYPE is not null
        <if test="storeCode != null  and storeCode != ''">and sp.store_code = #{storeCode}</if>
        group by si.store_code, si.STORE_NAME, sp.material_type, ml.MATERIAL_NAME
        order by si.store_code, sp.material_type
    </select>

    <insert id="insertBaseSpaceInfo" parameterType="BaseSpaceInfo">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_base_spaceinfo.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_spaceinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="spaceCode != null">space_code,</if>
            <if test="spaceName != null">space_name,</if>
            <if test="spaceCapacity != null">space_capacity,</if>
            <if test="spaceStock != null">space_stock,</if>
            <if test="spaceStatus != null">space_status,</if>
            <if test="spaceType != null">space_type,</if>
            <if test="storeCode != null">store_code,</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>
            <if test="materialType != null">material_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="spaceCode != null">#{spaceCode},</if>
            <if test="spaceName != null">#{spaceName},</if>
            <if test="spaceCapacity != null">#{spaceCapacity},</if>
            <if test="spaceStock != null">#{spaceStock},</if>
            <if test="spaceStatus != null">#{spaceStatus},</if>
            <if test="spaceType != null">#{spaceType},</if>
            <if test="storeCode != null">#{storeCode},</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>
            <if test="materialType != null">#{materialType},</if>
        </trim>
    </insert>

    <update id="updateBaseSpaceInfo" parameterType="BaseSpaceInfo">
        update base_spaceinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="spaceCode != null">space_code = #{spaceCode},</if>
            <if test="spaceName != null">space_name = #{spaceName},</if>
            <if test="spaceCapacity != null">space_capacity = #{spaceCapacity},</if>
            <if test="spaceStock != null">space_stock = #{spaceStock},</if>
            <if test="spaceStatus != null">space_status = #{spaceStatus},</if>
            <if test="spaceType != null">space_type = #{spaceType},</if>
            <if test="storeCode != null">store_code = #{storeCode},</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>
            <if test="materialType != null">material_type = #{materialType},</if>
        </trim>
        where obj_id = #{objId}
    </update>

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

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