<?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.cim.idm.dao.WareHouseDao">

    <update id="edit">
        update BS_ERPLOCATION set USE_LOCATION = #{dto.useLocation}, IS_OVEN = #{dto.isOven},
        LOCATION_USER = #{dto.locationUser, jdbcType=VARCHAR}, ZDBHFLAG = #{dto.zdbhFlag}
        where ERPFACTORYNAME = #{dto.erpFactoryName} and ERPLOCATIONNAME = #{dto.erpLocationName}
    </update>

    <select id="page" resultType="com.cim.idm.model.po.storage.StorageSpec">
        select a.SITENAME, a.STORAGENAME, a.DESCRIPTION, a.STORAGEUSETYPE, a.ERPFACTORY, a.ERPLOCATION,
                o.ORGNAME, b.DESCRIPTION erpLocationName
        from STORAGESPEC a LEFT JOIN BS_ERPLOCATION b ON a.ERPLOCATION = b.ERPLOCATIONNAME
        LEFT JOIN ORG o ON a.ERPFACTORY = o.ORGNO
        <where>
            <if test="dto.storageName != null and dto.storageName != ''">
                AND a.STORAGENAME LIKE '%'||#{dto.storageName}||'%'
            </if>
            <if test="dto.description != null and dto.description != ''">
                AND a.DESCRIPTION LIKE '%'||#{dto.description}||'%'
            </if>
            <if test="dto.storageUseType != null and dto.storageUseType != ''">
                AND a.STORAGEUSETYPE = #{dto.storageUseType}
            </if>
            <if test="dto.erpFactory != null and dto.erpFactory != ''">
                AND a.ERPFACTORY = #{dto.erpFactory}
            </if>
            <if test="dto.erpLocation != null and dto.erpLocation != ''">
                AND a.ERPLOCATION = #{dto.erpLocation}
            </if>
        </where>
        ORDER BY a.ERPLOCATION,a.STORAGENAME
    </select>

    <select id="get" resultType="com.cim.idm.model.po.storage.StorageSpec">
        select SITENAME, STORAGENAME, DESCRIPTION
        from STORAGESPEC
        where SITENAME = #{siteName}
        and STORAGENAME = #{storageName}
    </select>

    <insert id="add">
        insert into STORAGESPEC(SITENAME, STORAGENAME, DESCRIPTION, STORAGEUSETYPE,
                ERPFACTORY, ERPLOCATION, CREATEUSER, CREATETIME)
        values (#{storageSpec.siteName}, #{storageSpec.storageName}, #{storageSpec.description}, #{storageSpec.storageUseType},
                #{storageSpec.erpFactory}, #{storageSpec.erpLocation}, #{storageSpec.createUser}, SYSDATE)
    </insert>

    <update id="editStorage">
        update STORAGESPEC set DESCRIPTION = #{edit.description} ,STORAGEUSETYPE = #{edit.storageUseType},
                               LASTEVENTTIME = SYSDATE, LASTEVENTUSER = #{edit.createUser}
        where SITENAME = #{edit.siteName}
        and STORAGENAME = #{edit.storageName}
    </update>

    <delete id="delStorage">
        delete from STORAGESPEC
        where SITENAME = #{del.siteName}
        and STORAGENAME = #{del.storageName}
    </delete>

    <select id="search" resultType="com.cim.idm.model.po.storage.StorageSpec">
        select SITENAME, STORAGENAME, DESCRIPTION
        from STORAGESPEC
        where 1 = 1 AND
        <foreach item="type" index="index" collection="prefix" open="(" separator="or" close=")">
            STORAGENAME LIKE #{type}||'%'
        </foreach>
        <if test="storageName != null and storageName != ''">
            AND STORAGENAME LIKE #{storageName}||'%'
        </if>
    </select>

    <select id="getLocation" resultType="com.cim.idm.model.po.storage.StorageSpec"
            parameterType="java.lang.String">
        SELECT DISTINCT ERPLOCATIONNAME erpLocation,DESCRIPTION erpLocationName,ERPFACTORYNAME erpFactory
        FROM BS_ERPLOCATION
        WHERE ERPLOCATIONNAME = #{erpLocation, jdbcType=VARCHAR}
    </select>
</mapper>