<?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.bonus.canteen.core.warehouse.mapper.WareHouseInfoMapper">
    <resultMap type="com.bonus.canteen.core.warehouse.domain.WareHouseInfo" id="WareHouseInfoResult">
        <result property="warehouseId"            column="warehouse_id"              />
        <result property="warehouseNum"           column="warehouse_num"             />
        <result property="warehouseName"          column="warehouse_name"              />
        <result property="userId"                 column="user_id"                     />
        <result property="regionProvince"         column="region_province"             />
        <result property="regionCity"             column="region_city"                 />
        <result property="regionDistrict"         column="region_district"             />
        <result property="address"                column="address"                     />
        <result property="warehouseType"          column="warehouse_type"              />
        <result property="status"                 column="status"                      />
        <result property="delFlag"                column="del_flag"                    />
        <result property="typeId"                 column="type_id"                     />
        <result property="areaId"                 column="area_id"                     />
        <result property="revision"               column="revision"                    />
        <result property="createBy"               column="create_by"                   />
        <result property="createTime"             column="create_time"                 />
        <result property="updateBy"               column="update_by"                   />
        <result property="updateTime"             column="update_time"                 />
        <result property="ifUseWarehouseArea"     column="if_use_warehouse_area"       />
        <result property="ifUseWarehouseLocation" column="if_use_warehouse_location"   />
        <result property="parentId"               column="parent_id"                   />
        <result property="canteenId"              column="canteen_id"                  />
        <result property="fetchUserId"            column="fetch_user_id"               />
    </resultMap>

    <sql id="selectWareHouseInfoVo">
        select warehouse_id,warehouse_num,warehouse_name,user_id,region_province,region_city,region_district,address,warehouse_type,status,del_flag,type_id,area_id,revision,create_by,create_time,update_by,update_time,if_use_warehouse_area,if_use_warehouse_location,parent_id,canteen_id,fetch_user_id from warehouse_info
    </sql>

    <select id="selectWareHouseInfoList" parameterType="com.bonus.canteen.core.warehouse.domain.WareHouseInfo" resultType="com.bonus.canteen.core.warehouse.domain.WareHouseInfo">
        select wh.warehouse_id as warehouseid,wh.warehouse_num as warehousenum,wh.warehouse_name as warehousename,wh.user_id as userid,wh.region_province as regionprovince,
        wh.region_city as regioncity,wh.region_district as regiondistrict,wh.address,wh.warehouse_type as warehousetype,wh.status,wh.del_flag as delflag,
        wh.type_id as typeid,wh.area_id as areaid,aa.area_name as areaname,wh.revision,wh.create_by as createby,wh.create_time as createtime,wh.update_by as updateby,wh.update_time as updatetime,
        wh.if_use_warehouse_area as ifusewarehousearea,wh.if_use_warehouse_location as ifusewarehouselocation
        ,wh.parent_id as parentid,wh.canteen_id as canteenid,ac.canteen_name as canteenname,wh.fetch_user_id,su.user_name as username
        from warehouse_info wh
        left join alloc_area aa on wh.area_id=aa.area_id
        left join alloc_canteen ac on wh.canteen_id=ac.canteen_id
        left join sys_user su on wh.fetch_user_id=su.user_id
        <where>
            <if test="areaId != null  and areaId != ''"> and wh.area_id = #{areaId}</if>
            <if test="warehouseName != null  and warehouseName != ''"> and wh.warehouse_name like concat('%', #{warehouseName}, '%')</if>
            <if test="userName != null  and userName != ''"> and su.user_name like concat('%', #{userName}, '%')</if>
        </where>
    </select>
        
    <insert id="insertWareHouseInfo" parameterType="com.bonus.canteen.core.warehouse.domain.WareHouseInfo">
        insert into warehouse_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warehouseNum          !=null"  >warehouse_num            , </if>
            <if test="warehouseName         !=null"  >warehouse_name           , </if>
            <if test="userId                !=null"  >user_id                  , </if>
            <if test="userType              !=null"  >user_type                  , </if>
            <if test="regionProvince        !=null"  >region_province          , </if>
            <if test="regionCity            !=null"  >region_city              , </if>
            <if test="regionDistrict        !=null"  >region_district          , </if>
            <if test="address               !=null"  >address                  , </if>
            <if test="warehouseType         !=null"  >warehouse_type           , </if>
            <if test="status                !=null"  >status                   , </if>
            <if test="delFlag               !=null"  >del_flag                 , </if>
            <if test="typeId                !=null"  >type_id                  , </if>
            <if test="areaId                !=null"  >area_id                  , </if>
            <if test="revision              !=null"  >revision                 , </if>
            <if test="createBy              !=null"  >create_by                , </if>
            <if test="createTime            !=null"  >create_time              , </if>
            <if test="updateBy              !=null"  >update_by                , </if>
            <if test="updateTime            !=null"  >update_time              , </if>
            <if test="ifUseWarehouseArea    !=null"  >if_use_warehouse_area    , </if>
            <if test="ifUseWarehouseLocation!=null"  >if_use_warehouse_location, </if>
            <if test="parentId              !=null"  >parent_id                , </if>
            <if test="canteenId             !=null"  >canteen_id               , </if>
            <if test="fetchUserId           !=null"  >fetch_user_id            , </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="warehouseNum          !=null"  >#{warehouseNum          },</if>
            <if test="warehouseName         !=null"  >#{warehouseName         },</if>
            <if test="userId                !=null"  >#{userId                },</if>
            <if test="userType              !=null"  >#{userType              },</if>
            <if test="regionProvince        !=null"  >#{regionProvince        },</if>
            <if test="regionCity            !=null"  >#{regionCity            },</if>
            <if test="regionDistrict        !=null"  >#{regionDistrict        },</if>
            <if test="address               !=null"  >#{address               },</if>
            <if test="warehouseType         !=null"  >#{warehouseType         },</if>
            <if test="status                !=null"  >#{status                },</if>
            <if test="delFlag               !=null"  >#{delFlag               },</if>
            <if test="typeId                !=null"  >#{typeId                },</if>
            <if test="areaId                !=null"  >#{areaId                },</if>
            <if test="revision              !=null"  >#{revision              },</if>
            <if test="createBy              !=null"  >#{createBy              },</if>
            <if test="createTime            !=null"  >#{createTime            },</if>
            <if test="updateBy              !=null"  >#{updateBy              },</if>
            <if test="updateTime            !=null"  >#{updateTime            },</if>
            <if test="ifUseWarehouseArea    !=null"  >#{ifUseWarehouseArea    },</if>
            <if test="ifUseWarehouseLocation!=null"  >#{ifUseWarehouseLocation},</if>
            <if test="parentId              !=null"  >#{parentId              },</if>
            <if test="canteenId             !=null"  >#{canteenId             },</if>
            <if test="fetchUserId           !=null"  >#{fetchUserId           },</if>
         </trim>
    </insert>

    <update id="updateWareHouseInfo" parameterType="com.bonus.canteen.core.warehouse.domain.WareHouseInfo">
        update warehouse_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseNum          !=null"  >warehouse_num            =#{warehouseNum          },</if>
            <if test="warehouseName         !=null"  >warehouse_name           =#{warehouseName         },</if>
            <if test="userId                !=null"  >user_id                  =#{userId                },</if>
            <if test="userType                !=null"  >user_type                  =#{userType                },</if>
            <if test="regionProvince        !=null"  >region_province          =#{regionProvince        },</if>
            <if test="regionCity            !=null"  >region_city              =#{regionCity            },</if>
            <if test="regionDistrict        !=null"  >region_district          =#{regionDistrict         },</if>
            <if test="address               !=null"  >address                  =#{address               },</if>
            <if test="warehouseType         !=null"  >warehouse_type           =#{warehouseType         },</if>
            <if test="status                !=null"  >status                   =#{status                },</if>
            <if test="delFlag               !=null"  >del_flag                 =#{delFlag               },</if>
            <if test="typeId                !=null"  >type_id                  =#{typeId                },</if>
            <if test="areaId                !=null"  >area_id                  =#{areaId                },</if>
            <if test="revision              !=null"  >revision                 =#{revision              },</if>
            <if test="updateBy              !=null"  >create_by                =#{updateBy               },</if>
            <if test="updateTime            !=null"  >create_time              =#{updateTime            	},</if>
            <if test="ifUseWarehouseArea    !=null"  >update_by                =#{ifUseWarehouseArea     },</if>
            <if test="ifUseWarehouseLocation!=null"  >update_time              =#{ifUseWarehouseLocation },</if>
            <if test="parentId              !=null"  >if_use_warehouse_area    =#{parentId              	},</if>
            <if test="canteenId             !=null"  >if_use_warehouse_location=#{canteenId              },</if>
            <if test="fetchUserId           !=null"  >parent_id                =#{fetchUserId            },</if>
        </trim>
        where warehouse_id = #{warehouseId}
    </update>

    <delete id="deleteWareHouseInfoByWareHouseId" parameterType="Long">
        delete from warehouse_info where warehouse_id = #{warehouseId}
    </delete>

    <delete id="deleteWareHouseInfoByWareHouseIds" parameterType="String">
        delete from warehouse_info where warehouse_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{warehouseId}
        </foreach>
    </delete>
</mapper>