<?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.tmerclub.cloud.product.mapper.WarehouseMapper">
  <resultMap id="warehouseMap" type="com.tmerclub.cloud.product.model.Warehouse">
    <id column="warehouse_id" property="warehouseId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="warehouse_name" property="warehouseName"/>
    <result column="type" property="type"/>
    <result column="address" property="address"/>
    <result column="manage" property="manage"/>
    <result column="phone" property="phone"/>
    <result column="sys_type" property="sysType"/>
    <result column="province_id" property="provinceId"/>
    <result column="province" property="province"/>
    <result column="city_id" property="cityId"/>
    <result column="city" property="city"/>
    <result column="area_id" property="areaId"/>
    <result column="area" property="area"/>
  </resultMap>


  <resultMap id="warehouseVOMap" type="com.tmerclub.cloud.common.order.vo.WarehouseVO" extends="warehouseMap">
    <id column="warehouse_id" property="warehouseId" />
    <collection property="areaIds" ofType="java.lang.Long">
      <id column="area_id" property="value"/>
    </collection>
  </resultMap>

  <resultMap id="WarehouseAreaMap" type="com.tmerclub.cloud.common.order.vo.AreaVO">
    <id column="area_id" jdbcType="BIGINT" property="areaId"/>
    <result column="area_name" jdbcType="VARCHAR" property="areaName"/>
    <result column="parent_id" jdbcType="BIGINT" property="parentId"/>
    <result column="level" jdbcType="INTEGER" property="level"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `warehouse_id`,`create_time`,`update_time`,`shop_id`,`warehouse_name`,`type`,`address`,`manage`,`phone`,`sys_type`,`province_id`,`province`,`city_id`,`city`,`area_id`,`area`
  </sql>
  <sql id="Join_Column_List">
    w.`warehouse_id`,w.`create_time`,w.`update_time`,w.`shop_id`,w.`warehouse_name`,w.`type`,w.`address`,w.`manage`,w.`phone`,w.`sys_type`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.common.order.vo.WarehouseVO">
    select <include refid="Vo_Column_List"/> from warehouse
    <where>
      <if test="warehouse.shopId != null">
        shop_id = #{warehouse.shopId}
      </if>
      <if test="warehouse.sysType != null">
        and sys_type = #{warehouse.sysType}
      </if>
      <if test="warehouse.warehouseName != null and warehouse.warehouseName != ''">
        and warehouse_name like CONCAT('%',#{warehouse.warehouseName},'%')
      </if>
      <if test="warehouse.type != null">
        and type = #{warehouse.type}
      </if>
      <if test="warehouse.manage != null and warehouse.manage != ''">
        and manage like CONCAT('%',#{warehouse.manage},'%')
      </if>
      <if test="warehouse.warehouseIds != null and warehouse.warehouseIds.size() > 0">
        and warehouse_id in
          <foreach collection="warehouse.warehouseIds" item="warehouseId" open="(" separator="," close=")">
            #{warehouseId}
          </foreach>
      </if>
    </where>
    order by update_time desc
  </select>
  <select id="getByWarehouseId" resultType="com.tmerclub.cloud.common.order.vo.WarehouseVO">
    select <include refid="Vo_Column_List"/> from warehouse where warehouse_id = #{warehouseId}
  </select>
  <insert id="save">
    insert into warehouse (`warehouse_id`,`shop_id`,`warehouse_name`,`type`,`address`,`manage`,`phone`,`sys_type`,
    `province_id`,`province`,`city_id`,`city`,`area_id`,`area`)
    values (#{warehouse.warehouseId},#{warehouse.shopId},#{warehouse.warehouseName},#{warehouse.type},#{warehouse.address},#{warehouse.manage},#{warehouse.phone},#{warehouse.sysType},
    #{warehouse.provinceId},#{warehouse.province},#{warehouse.cityId},#{warehouse.city},#{warehouse.areaId},#{warehouse.area});
  </insert>
  <update id="update">
    update warehouse
    <set>
      <if test="warehouse.warehouseName != null">
        `warehouse_name` = #{warehouse.warehouseName},
      </if>
      <if test="warehouse.address != null">
        `address` = #{warehouse.address},
      </if>
      <if test="warehouse.manage != null">
        `manage` = #{warehouse.manage},
      </if>
      <if test="warehouse.phone != null">
        `phone` = #{warehouse.phone},
      </if>
      <if test="warehouse.provinceId != null">
        `province_id` = #{warehouse.provinceId},
      </if>
      <if test="warehouse.province != null">
        `province` = #{warehouse.province},
      </if>
      <if test="warehouse.cityId != null">
        `city_id` = #{warehouse.cityId},
      </if>
      <if test="warehouse.city != null">
        `city` = #{warehouse.city},
      </if>
      <if test="warehouse.areaId != null">
        `area_id` = #{warehouse.areaId},
      </if>
      <if test="warehouse.area != null">
        `area` = #{warehouse.area},
      </if>
      update_time = now()
    </set>
    where warehouse_id = #{warehouse.warehouseId}
  </update>
  <delete id="deleteById">
    delete from warehouse where warehouse_id = #{warehouseId}
  </delete>

  <select id="getWarehouseAreaByWarehouseId" resultType="java.lang.Long">
      select area_id
      from warehouse_area
      where warehouse_id = #{warehouseId}
      order by warehouse_area_id
  </select>

  <select id="countByWarehouseName" resultType="int">
    select count(*) from warehouse where shop_id = #{shopId} and warehouse_name = #{warehouseName} and sys_type = #{sysType}
    <if test="warehouseId != null">
      and warehouse_id != #{warehouseId}
    </if>
  </select>

  <select id="getDefaultWarehouseIdByShopId" resultType="com.tmerclub.cloud.common.order.vo.WarehouseVO">
    select <include refid="Vo_Column_List"/> from warehouse where shop_id = #{shopId} and sys_type = #{sysType}
    and type = 0
  </select>

  <select id="listareaByShopIdAndSysType" resultType="java.lang.Long">
    select wec.area_id from warehouse we
    left join warehouse_area wec on we.warehouse_id = wec.warehouse_id
    where we.shop_id = #{shopId} and we.sys_type = #{sysType}
    <if test="warehouseId != null ">
      and we.warehouse_id != #{warehouseId}
    </if>
  </select>
  <select id="listWarehouseByShopId" resultMap="warehouseVOMap">
    select <include refid="Join_Column_List"/>, wa.area_id
    from warehouse w
    left join warehouse_area wa on w.warehouse_id = wa.warehouse_id
    where w.shop_id = #{shopId} and w.sys_type = #{sysType}
  </select>

  <select id="listCanDeliveryWarehouse" resultType="com.tmerclub.cloud.common.order.vo.WarehouseVO">
    SELECT
      w.*
    FROM
      `warehouse` w
      LEFT JOIN `warehouse_area` wa ON w.warehouse_id = wa.warehouse_id
    WHERE
      ( wa.area_id = #{warehouseDTO.areaId} OR w.type = 0 )
      <if test="warehouseDTO.sysType != null">
        AND w.sys_type = #{warehouseDTO.sysType}
      </if>
      <if test="warehouseDTO.shopIds != null and warehouseDTO.shopIds.size() > 0">
        AND w.shop_id IN
          <foreach collection="warehouseDTO.shopIds" item="shopId" open="(" separator="," close=")">
            #{shopId}
          </foreach>
      </if>
  </select>

  <select id="batchSave">
    insert into warehouse (`warehouse_id`,`shop_id`,`warehouse_name`,`type`,`sys_type`) values
    <foreach collection="warehouseList" item="warehouse" separator=",">
      (#{warehouse.warehouseId},#{warehouse.shopId},#{warehouse.warehouseName},#{warehouse.type},#{warehouse.sysType})
    </foreach>
  </select>

  <select id="listDefaultWarehouses" resultType="com.tmerclub.cloud.common.order.vo.WarehouseVO">
    select * from warehouse where type = 0
  </select>
</mapper>
