<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.no1.wms.warehouse.WarehouseMapper">


    <select id="count" parameterType="map" resultType="java.lang.Integer">
        select count(*)

        from  warehouse w

        <where>
            <choose>
                <when test="searchn == 0"> w.activation = 1 and w.name like concat('%',#{search},'%')</when>
                <when test="searchn == 1"> w.activation = 1 and w.capacity like concat('%',#{search},'%') </when>
                <when test="searchn == 2"> w.activation = 1 and w.current_capacity like concat('%',#{search},'%')</when>
            </choose>
        </where>
    </select>

    <select id="list" parameterType="map" resultType="com.no1.wms.warehouse.WarehouseDto">
        select w.id, w.name, w.capacity, w.current_capacity as currentCapacity

        from  warehouse w

        <where>
            <choose>
                <when test="searchn == 0"> w.activation = 1 and w.name like concat('%',#{search},'%')</when>
                <when test="searchn == 1"> w.activation = 1 and w.capacity like concat('%',#{search},'%') </when>
                <when test="searchn == 2"> w.activation = 1 and w.current_capacity like concat('%',#{search},'%')</when>
            </choose>
        </where>
        order by w.name ASC limit #{start}, #{perPage}
    </select>

    <select id="One" parameterType="String" resultType="com.no1.wms.warehouse.WarehouseDto">
        select w.id, w.name, w.capacity, w.current_capacity as currentCapacity, w.address

        from  warehouse w

        where w.id = #{id}
    </select>

    <select id="warehouseOne" parameterType="Map" resultType="Map">
        select p.name, s.quantity, w.id

        from  warehouse w
        LEFT JOIN stock s ON w.id = s.warehouse_id
        LEFT JOIN product p ON s.product_id = p.id

        <where>
            <choose>
                <when test="searchn == 0"> w.id = #{id} and w.activation = 1 and p.name like concat('%',#{search},'%')</when>
                <when test="searchn == 1"> w.id = #{id} and w.activation = 1 and s.quantity like concat('%',#{search},'%') </when>
            </choose>
        </where>

<!--         order by p.name desc limit #{start}, #{perPage}-->

    </select>

    <select id="warehouseOneCount" parameterType="Map" resultType="java.lang.Integer">
        select count(*)

        from  warehouse w
        LEFT JOIN stock s ON w.id = s.warehouse_id
        LEFT JOIN product p ON s.product_id = p.id

        <where>
            <choose>
                <when test="searchn == 0"> w.id = #{id} and w.activation = 1 and p.name like concat('%',#{search},'%')</when>
                <when test="searchn == 1"> w.id = #{id} and w.activation = 1 and s.quantity like concat('%',#{search},'%') </when>
            </choose>
        </where>
    </select>


    <!-- select -->


    <!-- update -->
    <update id="updateWarehouse" parameterType="com.no1.wms.warehouse.WarehouseDto">
        update warehouse
        set capacity = #{capacity}, name = #{name}, address = #{address}
        where id = #{id}
    </update>

    <!-- update -->


    <!-- insert -->
    <insert id="createWarehouse" parameterType="com.no1.wms.warehouse.WarehouseDto">
        insert into warehouse (id, name, capacity, current_capacity, manager_id, address, activation)
        values (UUID(), #{name}, #{capacity}, #{currentCapacity}, #{managerId}, #{address}, #{activation})
    </insert>
    <!-- insert -->

    <!-- delete -->
    <delete id="deleteWarehouse" parameterType="String">
        delete from warehouse where id = #{id}
    </delete>
    <!-- delete -->
</mapper>