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

    <resultMap type="TBaseStorageAreaLocation" id="TBaseStorageAreaLocationResult">
        <result property="id" column="id"/>
        <result property="locationId" column="location_id"/>
        <result property="pyName" column="py_name"/>
        <result property="storageId" column="storage_id"/>
        <result property="isLock" column="is_lock"/>
        <result property="locationLong" column="location_long"/>
        <result property="locationWidth" column="location_width"/>
        <result property="locationHeight" column="location_height"/>
        <result property="volume" column="volume"/>
        <result property="hasVolume" column="has_volume"/>
        <result property="bearWeight" column="bear_weight"/>
        <result property="abcId" column="abc_id"/>
        <result property="sort" column="sort"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="version" column="version"/>
        <result property="decid" column="decid"/>
        <result property="instand" column="instand"/>
        <result property="outstand" column="outstand"/>
        <result property="inLine" column="in_line"/>
        <result property="outLine" column="out_line"/>
        <result property="line" column="line"/>
        <result property="cln" column="cln"/>
        <result property="layer" column="layer"/>
        <result property="depth" column="depth"/>
        <result property="instand1" column="instand1"/>
        <result property="instand2" column="instand2"/>
        <result property="outstand1" column="outstand1"/>
        <result property="instand3" column="instand3"/>
        <result property="outstand2" column="outstand2"/>
        <result property="outstand3" column="outstand3"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="fictitious" column="fictitious"/>
    </resultMap>

    <sql id="selectTBaseStorageAreaLocationVo">
        select id, location_id, py_name, storage_id, is_lock, location_long, location_width, location_height, volume, has_volume, bear_weight, abc_id, sort, status, remark, version, decid, instand, outstand, in_line, out_line, line, cln, layer, depth, instand1, instand2, outstand1, instand3, outstand2, outstand3, create_time, create_by, update_time, update_by,fictitious from t_base_storage_area_location
    </sql>

    <select id="selectTBaseStorageAreaLocationList" parameterType="TBaseStorageAreaLocation"
            resultMap="TBaseStorageAreaLocationResult">
        <include refid="selectTBaseStorageAreaLocationVo"/>
        <where>
            <if test="locationId != null  and locationId != ''">and location_id like concat('%', #{locationId}, '%')</if>
            <if test="pyName != null  and pyName != ''">and py_name like concat('%', #{pyName}, '%')</if>
            <if test="storageId != null  and storageId != ''">and storage_id = #{storageId}</if>
            <if test="isLock != null  and isLock != ''">and is_lock = #{isLock}</if>
            <if test="locationLong != null ">and location_long = #{locationLong}</if>
            <if test="locationWidth != null ">and location_width = #{locationWidth}</if>
            <if test="locationHeight != null ">and location_height = #{locationHeight}</if>
            <if test="volume != null ">and volume = #{volume}</if>
            <if test="hasVolume != null ">and has_volume = #{hasVolume}</if>
            <if test="bearWeight != null ">and bear_weight = #{bearWeight}</if>
            <if test="abcId != null  and abcId != ''">and abc_id = #{abcId}</if>
            <if test="sort != null ">and sort = #{sort}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="version != null  and version != ''">and version = #{version}</if>
            <if test="decid != null  and decid != ''">and decid = #{decid}</if>
            <if test="instand != null  and instand != ''">and instand = #{instand}</if>
            <if test="outstand != null  and outstand != ''">and outstand = #{outstand}</if>
            <if test="inLine != null  and inLine != ''">and in_line = #{inLine}</if>
            <if test="outLine != null  and outLine != ''">and out_line = #{outLine}</if>
            <if test="line != null  and line != ''">and line = #{line}</if>
            <if test="cln != null  and cln != ''">and cln = #{cln}</if>
            <if test="layer != null  and layer != ''">and layer = #{layer}</if>
            <if test="depth != null  and depth != ''">and depth = #{depth}</if>
            <if test="instand1 != null  and instand1 != ''">and instand1 = #{instand1}</if>
            <if test="instand2 != null  and instand2 != ''">and instand2 = #{instand2}</if>
            <if test="outstand1 != null  and outstand1 != ''">and outstand1 = #{outstand1}</if>
            <if test="instand3 != null  and instand3 != ''">and instand3 = #{instand3}</if>
            <if test="outstand2 != null  and outstand2 != ''">and outstand2 = #{outstand2}</if>
            <if test="outstand3 != null  and outstand3 != ''">and outstand3 = #{outstand3}</if>
            <if test="fictitious != null and fictitious != ''">and fictitious = #{fictitious} </if>
            <if test="selectIds !=null and selectIds.length>0">
                and id in
                <foreach collection="selectIds" item="itemId" open="(" close=")" separator=",">
                    #{itemId}
                </foreach>
            </if>
        </where>
        order by id asc
    </select>

    <select id="selectTBaseStorageAreaLocationById" parameterType="Long" resultMap="TBaseStorageAreaLocationResult">
        <include refid="selectTBaseStorageAreaLocationVo"/>
        where id = #{id}
    </select>

    <insert id="insertTBaseStorageAreaLocation" parameterType="TBaseStorageAreaLocation" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_base_storage_area_location
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="locationId != null and locationId != ''">location_id,</if>
            <if test="pyName != null">py_name,</if>
            <if test="storageId != null">storage_id,</if>
            <if test="isLock != null">is_lock,</if>
            <if test="locationLong != null">location_long,</if>
            <if test="locationWidth != null">location_width,</if>
            <if test="locationHeight != null">location_height,</if>
            <if test="volume != null">volume,</if>
            <if test="hasVolume != null">has_volume,</if>
            <if test="bearWeight != null">bear_weight,</if>
            <if test="abcId != null">abc_id,</if>
            <if test="sort != null">sort,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="version != null">version,</if>
            <if test="decid != null">decid,</if>
            <if test="instand != null">instand,</if>
            <if test="outstand != null">outstand,</if>
            <if test="inLine != null">in_line,</if>
            <if test="outLine != null">out_line,</if>
            <if test="line != null">line,</if>
            <if test="cln != null">cln,</if>
            <if test="layer != null">layer,</if>
            <if test="depth != null">depth,</if>
            <if test="instand1 != null">instand1,</if>
            <if test="instand2 != null">instand2,</if>
            <if test="outstand1 != null">outstand1,</if>
            <if test="instand3 != null">instand3,</if>
            <if test="outstand2 != null">outstand2,</if>
            <if test="outstand3 != null">outstand3,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="fictitious != null">fictitious,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="locationId != null and locationId != ''">#{locationId},</if>
            <if test="pyName != null">#{pyName},</if>
            <if test="storageId != null">#{storageId},</if>
            <if test="isLock != null">#{isLock},</if>
            <if test="locationLong != null">#{locationLong},</if>
            <if test="locationWidth != null">#{locationWidth},</if>
            <if test="locationHeight != null">#{locationHeight},</if>
            <if test="volume != null">#{volume},</if>
            <if test="hasVolume != null">#{hasVolume},</if>
            <if test="bearWeight != null">#{bearWeight},</if>
            <if test="abcId != null">#{abcId},</if>
            <if test="sort != null">#{sort},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="version != null">#{version},</if>
            <if test="decid != null">#{decid},</if>
            <if test="instand != null">#{instand},</if>
            <if test="outstand != null">#{outstand},</if>
            <if test="inLine != null">#{inLine},</if>
            <if test="outLine != null">#{outLine},</if>
            <if test="line != null">#{line},</if>
            <if test="cln != null">#{cln},</if>
            <if test="layer != null">#{layer},</if>
            <if test="depth != null">#{depth},</if>
            <if test="instand1 != null">#{instand1},</if>
            <if test="instand2 != null">#{instand2},</if>
            <if test="outstand1 != null">#{outstand1},</if>
            <if test="instand3 != null">#{instand3},</if>
            <if test="outstand2 != null">#{outstand2},</if>
            <if test="outstand3 != null">#{outstand3},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="fictitious != null">#{fictitious},</if>
        </trim>
    </insert>

    <update id="updateTBaseStorageAreaLocation" parameterType="TBaseStorageAreaLocation">
        update t_base_storage_area_location
        <trim prefix="SET" suffixOverrides=",">
            <if test="locationId != null and locationId != ''">location_id = #{locationId},</if>
            <if test="pyName != null">py_name = #{pyName},</if>
            <if test="storageId != null">storage_id = #{storageId},</if>
            <if test="isLock != null">is_lock = #{isLock},</if>
            <if test="locationLong != null">location_long = #{locationLong},</if>
            <if test="locationWidth != null">location_width = #{locationWidth},</if>
            <if test="locationHeight != null">location_height = #{locationHeight},</if>
            <if test="volume != null">volume = #{volume},</if>
            <if test="hasVolume != null">has_volume = #{hasVolume},</if>
            <if test="bearWeight != null">bear_weight = #{bearWeight},</if>
            <if test="abcId != null">abc_id = #{abcId},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="version != null">version = #{version},</if>
            <if test="decid != null">decid = #{decid},</if>
            <if test="instand != null">instand = #{instand},</if>
            <if test="outstand != null">outstand = #{outstand},</if>
            <if test="inLine != null">in_line = #{inLine},</if>
            <if test="outLine != null">out_line = #{outLine},</if>
            <if test="line != null">line = #{line},</if>
            <if test="cln != null">cln = #{cln},</if>
            <if test="layer != null">layer = #{layer},</if>
            <if test="depth != null">depth = #{depth},</if>
            <if test="instand1 != null">instand1 = #{instand1},</if>
            <if test="instand2 != null">instand2 = #{instand2},</if>
            <if test="outstand1 != null">outstand1 = #{outstand1},</if>
            <if test="instand3 != null">instand3 = #{instand3},</if>
            <if test="outstand2 != null">outstand2 = #{outstand2},</if>
            <if test="outstand3 != null">outstand3 = #{outstand3},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="fictitious != null">fictitious = #{fictitious},</if>
        </trim>
        where id = #{id}
    </update>

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

    <delete id="deleteTBaseStorageAreaLocationById" parameterType="Long">
        delete from t_base_storage_area_location  where id = #{id}
    </delete>

    <delete id="deleteTBaseStorageAreaLocationByIds" parameterType="String">
        delete from t_base_storage_area_location where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectFreeLoc" parameterType="String" resultMap="TBaseStorageAreaLocationResult">
        <include refid="selectTBaseStorageAreaLocationVo"/>
        where `status`='0' and decid='1'
        and location_id not in (select DISTINCT(t.location_id) from t_mi_stock t UNION select DISTINCT(p.location_id)
        from t_ck_pickingwavegoods p UNION select DISTINCT(o.location_id)
        from t_ongoodsshelf o where o.location_id is not null)
        order by depth DESC, sort ASC

    </select>
    <select id="occupy" resultMap="TBaseStorageAreaLocationResult">
        select DISTINCT d.location_id,case when IFNULL(t.location_id, 0) = 0  then 0 else 1 end `status`,t.ctl as remark,d.`status` as "islock"
        from t_base_storage_area_location d
        left join t_mi_stock t on d.location_id = t.location_id
    </select>

    <update id="updateLocationSts" parameterType="TBaseStorageAreaLocation">
        update t_base_storage_area_location set status = #{status} where location_id = #{locationId}
    </update>

    <update id="updateLockStatus" parameterType="TBaseStorageAreaLocation">
        update t_base_storage_area_location set is_lock=#{isLock} where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
