<?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.boco.nbd.wios.manage.mapper.extdb.WarehouseMapperEx">

    <select id="getWarehouseLevenByNameCount" resultType="integer">
        SELECT
            count( 1 )
        FROM
            t_warehouse_leven
        WHERE
            `status` = 1
            <if test="parentId != null and parentId != ''">
                AND parent_id = #{parentId}
            </if>
            <if test="id != null and id != ''">
                and id != #{id}
            </if>
            AND NAME = #{name}
    </select>

    <select id="getWarehouseLevenParentLastId" parameterType="integer" resultType="integer">
        SELECT
            IFNULL( max( id ), 0 ) + 1
        FROM
            t_warehouse_leven
    </select>

    <select id="getWarehouseLevenParentSeq" parameterType="integer" resultType="integer">
        SELECT
            IFNULL( max( seq ), 0 ) + 1
        FROM
            t_warehouse_leven
        WHERE
            parent_id = #{parentId}
            and status = 1
    </select>

    <select id="qryWarehouseLevenById" resultType="map">
        select
            id,
            name,
            parent_id parentId
        from
            t_warehouse_leven
        where
            status = 1
            and id = #{parentId}
        limit 1
    </select>


    <insert id="insertWarehouseLeven" parameterType="com.boco.nbd.wios.manage.entity.bo.WarehouseLeven">
        INSERT INTO t_warehouse_leven(id,appkey,name,parent_id,seq,path_code,path_name,status,create_time,create_account,modify_time,modify_account,msg)
        values (
            #{id},#{appkey},#{name},#{parentId},#{seq},#{pathCode},#{pathName},#{status},#{createTime},#{createAccount},#{modifyTime},#{modifyAccount},#{msg}
        )
         ON DUPLICATE KEY UPDATE parent_id = values(parent_id),seq = values(seq),name = values(name),path_code = values(path_code),path_name=values(path_name),msg=values(msg)
    </insert>


    <insert id="insertWarehouse" parameterType="com.boco.nbd.wios.manage.entity.bo.Warehouse">
        insert  into t_warehouse(id,appkey,warehouse_leven_id,name,area,service_provider,warehouse_attribution,msg,status,create_time,create_account,modify_account,modify_time,is_enabled,superior_warehouse_id,receive_people,receive_phone,receive_address)
        values (#{id},#{appkey},#{warehouseLevenId},#{name},#{area},#{serviceProvider},#{warehouseAttribution},#{msg},#{status},#{createTime},#{createAccount},#{modifyAccount},#{modifyTime},#{isEnabled},#{superiorWarehouseId},#{receivePeople},#{receivePhone},#{receiveAddress})
        ON DUPLICATE KEY UPDATE  warehouse_leven_id = values(warehouse_leven_id),name = values(name),area=values(area),service_provider = values(service_provider),warehouse_attribution = values(warehouse_attribution),msg = values(msg),is_enabled = values(is_enabled),superior_warehouse_id = values(superior_warehouse_id),receive_people = values(receive_people),receive_phone = values(receive_phone),receive_address = values(receive_address)
    </insert>


    <select id="qryWarehouseList" parameterType="string" resultType="com.boco.nbd.wios.manage.entity.bo.WarehouseEx">
        select
        tw.id,
        tw.appkey,
        tw.warehouse_leven_id,
        tw.name,
        tw.area,
        tw.service_provider,
        tw.warehouse_attribution,
        tw.msg,
        tw.status,
        tw.is_enabled,
        tw.create_time,
        tw.create_account,
        tw.modify_account,
        tw.modify_time,
        tw.superior_warehouse_id,
        tw.receive_people,
        tw.receive_phone,
        tw.receive_address,
        twl.parent_id parentId,
        twl.path_code pathCode,
        twl.path_name pathName,
        case
            when tw.warehouse_attribution = 1 then 'CAMS仓库'
            when tw.warehouse_attribution = 2 then '安装服务商仓库'
        end warehouseAttributionName,
        case
            when tw.warehouse_leven_id = 0 then '总仓'
            when tw.warehouse_leven_id = 1 then '仓库'
            when tw.warehouse_leven_id = 2 then '网点'
        end warehouseLevenName,
        case
            when tw.is_enabled = 0 then '已禁用'
            when tw.is_enabled = 1 then '已启用'
        end isEnabledName
        from
        t_warehouse tw
        LEFT JOIN t_warehouse_leven twl on tw.warehouse_leven_id = twl.id
        where
        tw.status = 1
        and tw.appkey = #{appkey}
        <if test="id != null and id != ''">
            and tw.id = #{id}
        </if>
        <if test="isEnable != null">
            and tw.is_enabled = #{isEnable}
        </if>
        <if test="serviceProviders != null and serviceProviders.size() > 0">
            and tw.service_provider in
            <foreach collection="serviceProviders" item="item" index="index"
                     open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="area != null and area != ''">
            and tw.area = #{area}
        </if>
        <if test="warehouseAttribution != null and warehouseAttribution != ''">
            and tw.warehouse_attribution = #{warehouseAttribution}
        </if>
        <if test="createAccount != null and createAccount != ''">
            and tw.create_account = #{createAccount}
        </if>
        <if test="name != null and name != ''">
            and tw.name like CONCAT('%',#{name},'%')
        </if>
        <if test="warehouseLevenId != null and warehouseLevenId != '' ">
            and tw.warehouse_leven_id = #{warehouseLevenId}
        </if>
        <if test="superiorWarehouseId != null and superiorWarehouseId != '' ">
            and tw.superior_warehouse_id = #{superiorWarehouseId}
        </if>
        order by tw.create_time desc
    </select>

    <select id="qryWarehouseLeven" resultType="com.boco.nbd.wios.manage.entity.bo.WarehouseLeven">
        SELECT
            twl.id,
            twl.name,
            twl.parent_id,
            twl.seq,
            twl.msg,
            twl.path_name,
            twl.path_code
        FROM
            t_warehouse_leven twl
            LEFT JOIN t_warehouse tw on tw.warehouse_leven_id = twl.id
        WHERE
            twl.`status` = 1
            and twl.appkey = #{appkey}
            <if test="serviceProvider != null and serviceProvider != '' and serviceProvider != 'null'">
                and tw.service_provider = #{serviceProvider}
            </if>
            <if test="parentId != null">
                and twl.parent_id = #{parentId}
            </if>
            <if test="id != null">
                and twl.id = #{id}
            </if>
            <if test="name != null and name != ''">
                and twl.name  like CONCAT('%',#{name},'%')
            </if>
        GROUP BY twl.id
    </select>


    <update id="updateWarehouseisEnabled">
        update
            t_warehouse
        set
            is_enabled = #{isEnabled}
        where id = #{id}
    </update>

    <select id="wareHouseBySuperiorWarehouseId" parameterType="string" resultType="com.boco.nbd.wios.manage.entity.bo.WarehouseEx">
        select id,name from t_warehouse where id = #{superiorWarehouseId}
    </select>

</mapper>