<?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.zbf.system.mapper.TBaseStorageMapper">

    <resultMap type="TBaseStorage" id="TBaseStorageResult">
            <result property="id"    column="id"    />
            <result property="factoryId"    column="factory_id"    />
            <result property="deptId"    column="dept_id"    />
            <result property="storageId"    column="storage_id"    />
            <result property="storageShortName"    column="storage_short_name"    />
            <result property="storageName"    column="storage_name"    />
            <result property="storageAddress"    column="storage_address"    />
            <result property="storageConcatsName"    column="storage_concats_name"    />
            <result property="storageConcatsMobile"    column="storage_concats_mobile"    />
            <result property="autoStatus"    column="auto_status"    />
            <result property="status"    column="status"    />
            <result property="isAgv"    column="is_agv"    />
            <result property="createTime"    column="create_time"    />
            <result property="createBy"    column="create_by"    />
            <result property="updateBy"    column="update_by"    />
            <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTBaseStorageVo">
        select id, factory_id, dept_id, storage_id,storage_short_name, storage_name, storage_address, storage_concats_name, storage_concats_mobile, auto_status, status,is_agv,type,create_time, create_by, update_by, update_time from t_base_storage d
    </sql>

    <select id="selectTBaseStorageList" parameterType="TBaseStorage" resultMap="TBaseStorageResult">
        <include refid="selectTBaseStorageVo"/>
        <where>
                        <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
                        <if test="storageId != null  and storageId != ''"> and storage_id = #{storageId}</if>
                        <if test="storageName != null  and storageName != ''"> and storage_name like concat('%', #{storageName}, '%')</if>
                        <if test="storageShortName != null  and storageShortName != ''"> and storage_short_name like concat('%', #{storageShortName}, '%')</if>
                        <if test="type != null  and type != ''"> and type = #{type}</if>
                        <if test="selectIds !=null and selectIds.length>0">
                            and id in
                            <foreach collection="selectIds" item="itemId" open="(" close=")" separator=",">
                                #{itemId}
                            </foreach>
                        </if>
        ${params.dataScope}
        </where>
        order by id asc
    </select>



    <select id="selectTBaseStorageById" parameterType="Long" resultMap="TBaseStorageResult">
            <include refid="selectTBaseStorageVo"/>
            where id = #{id}
    </select>

    <insert id="insertTBaseStorage" parameterType="TBaseStorage" useGeneratedKeys="true" keyProperty="id">
        insert into t_base_storage
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="factoryId != null">factory_id,</if>
                    <if test="deptId != null">dept_id,</if>
                    <if test="storageId != null and storageId != ''">storage_id,</if>
                    <if test="storageShortName != null and storageShortName != ''">storage_short_name,</if>
                    <if test="storageName != null and storageName != ''">storage_name,</if>
                    <if test="storageAddress != null">storage_address,</if>
                    <if test="storageConcatsName != null">storage_concats_name,</if>
                    <if test="storageConcatsMobile != null">storage_concats_mobile,</if>
                    <if test="autoStatus != null">auto_status,</if>
                    <if test="status != null">status,</if>
                    <if test="type != null">type,</if>
                    <if test="isAgv != null">is_agv,</if>
                    <if test="createTime != null">create_time,</if>
                    <if test="createBy != null">create_by,</if>
                    <if test="updateBy != null">update_by,</if>
                    <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                    <if test="factoryId != null">#{factoryId},</if>
                    <if test="deptId != null">#{deptId},</if>
                    <if test="storageId != null and storageId != ''">#{storageId},</if>
                    <if test="storageShortName != null and storageShortName != ''">#{storageShortName},</if>
                    <if test="storageName != null and storageName != ''">#{storageName},</if>
                    <if test="storageAddress != null">#{storageAddress},</if>
                    <if test="storageConcatsName != null">#{storageConcatsName},</if>
                    <if test="storageConcatsMobile != null">#{storageConcatsMobile},</if>
                    <if test="autoStatus != null">#{autoStatus},</if>
                    <if test="status != null">#{status},</if>
                    <if test="type != null">#{type},</if>
                    <if test="isAgv != null">#{isAgv},</if>
                    <if test="createTime != null">#{createTime},</if>
                    <if test="createBy != null">#{createBy},</if>
                    <if test="updateBy != null">#{updateBy},</if>
                    <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateTBaseStorage" parameterType="TBaseStorage">
        update t_base_storage
        <trim prefix="SET" suffixOverrides=",">
                    <if test="factoryId != null">factory_id = #{factoryId},</if>
                    <if test="deptId != null">dept_id = #{deptId},</if>
                    <if test="storageId != null and storageId != ''">storage_id = #{storageId},</if>
                    <if test="storageShortName != null and storageShortName != ''">storage_short_name = #{storageShortName},</if>
                    <if test="storageName != null and storageName != ''">storage_name = #{storageName},</if>
                    <if test="storageAddress != null">storage_address = #{storageAddress},</if>
                    <if test="storageConcatsName != null">storage_concats_name = #{storageConcatsName},</if>
                    <if test="storageConcatsMobile != null">storage_concats_mobile = #{storageConcatsMobile},</if>
                    <if test="autoStatus != null">auto_status = #{autoStatus},</if>
                    <if test="status != null">status = #{status},</if>
                    <if test="type != null">type = #{type},</if>
                    <if test="isAgv != null">is_agv = #{isAgv},</if>
                    <if test="createTime != null">create_time = #{createTime},</if>
                    <if test="createBy != null">create_by = #{createBy},</if>
                    <if test="updateBy != null">update_by = #{updateBy},</if>
                    <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="enableTBaseStorageByIds" parameterType="String">
        update t_base_storage set status=0 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="disabledTBaseStorageByIds" parameterType="String">
        update t_base_storage set status=1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteTBaseStorageById" parameterType="Long">
        delete from t_base_storage  where id = #{id}
    </delete>

    <delete id="deleteTBaseStorageByIds" parameterType="String">
        delete from t_base_storage where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!--查询存在的库存信息-->
    <select id="selectStockByIds" parameterType="String" resultMap="TBaseStorageResult">
        select a.id, a.storage_id,a.storage_short_name, a.storage_name, a.storage_address, a.storage_concats_name, a.storage_concats_mobile, a.auto_status, a.status, a.is_agv, a.create_time, a.create_by, a.update_by, a.update_time
        from t_base_storage a , t_mi_stock b where a.storage_id=b.storage_id
        where a.storage_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{storageId}
        </foreach>
    </select>

    <!--查询存在的库存库位库区信息-->
    <select id="selectStorageAndAreaAndLocation" parameterType="TBaseStorageLocationRecord" resultType="TBaseStorageLocationRecord">
        select d.id
            , d.storage_id
            , d.storage_name
            , c.location_id
            ,d.storage_short_name
        from t_base_storage d
            left join t_base_storage_area_location c
                on d.storage_id = c.storage_id
               and c.status = '0'
               and (c.fictitious = '0' or c.fictitious is null)
        <where>
            d.status = '0'
            <if test="storageId != null and storageId != ''">and d.storage_id = #{storageId}</if>
            ${params.dataScope}
        </where>


    </select>


    <select id="callUpdateStorageIds" statementType="CALLABLE">
        CALL update_storage_ids();
    </select>

</mapper>
