<?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.material.warehouse.mapper.WhHouseInfoMapper">
    <resultMap type="com.bonus.material.warehouse.domain.WhHouseInfo" id="WhHouseInfoResult">
        <result property="houseId"    column="house_id"    />
        <result property="houseName"    column="house_name"    />
        <result property="physicalName"    column="physical_name"    />
        <result property="geoLocation"    column="geo_location"    />
        <result property="parentId"    column="parent_id"    />
        <result property="status"    column="status"    />
        <result property="deptId"    column="dept_id"    />
        <result property="delFlag"    column="del_flag"    />
        <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="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="sort"    column="sort"    />
        <result property="concat"    column="concat"    />
        <result property="phone"    column="phone"    />
    </resultMap>

    <sql id="selectWhHouseInfoVo">
        select house_id, house_name, physical_name, geo_location, parent_id, status, dept_id,
               del_flag, create_by, create_time, update_by, update_time, remark,
               company_id, sort, concat, phone
        from wh_house_info
    </sql>

    <select id="selectWhHouseInfoList" parameterType="com.bonus.material.warehouse.domain.WhHouseInfo" resultMap="WhHouseInfoResult">
        <include refid="selectWhHouseInfoVo"/>
        <where>
            <if test="keyWord != null and keyWord != ''">
                and house_name like concat('%', #{keyWord}, '%')
            </if>
            <if test="houseName != null  and houseName != ''"> and house_name like concat('%', #{houseName}, '%')</if>
            <if test="physicalName != null and physicalName != ''"> and physical_name like concat('%', #{physicalName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="companyId != null  and companyId != ''"> and company_id = #{companyId}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="concat != null  and concat != ''"> and concat = #{concat}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            and del_flag = '0'
        </where>
    </select>
    
    <select id="selectWhHouseInfoByHouseId" parameterType="Long" resultMap="WhHouseInfoResult">
        <include refid="selectWhHouseInfoVo"/>
        where house_id = #{houseId}
    </select>

    <select id="selectByHouseId" resultType="com.bonus.material.warehouse.domain.WhHouseInfo">
        SELECT
            whi.house_name,
            whs.type_id
        FROM
            wh_house_info whi
                LEFT JOIN wh_house_set whs ON whi.house_id = whs.house_id
        WHERE
            whs.del_flag = '0'
          AND whi.house_id = #{houseId}
    </select>

    <insert id="insertWhHouseInfo" parameterType="com.bonus.material.warehouse.domain.WhHouseInfo" useGeneratedKeys="true" keyProperty="houseId">
        insert into wh_house_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="houseName != null and houseName != ''">house_name,</if>
            <if test="physicalName != null and physicalName != ''">physical_name,</if>
            <if test="geoLocation != null and geoLocation != ''">geo_location,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="status != null">status,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="sort != null">sort,</if>
            <if test="concat != null">concat,</if>
            <if test="phone != null">phone,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="houseName != null and houseName != ''">#{houseName},</if>
            <if test="physicalName != null and physicalName != ''">#{physicalName},</if>
            <if test="geoLocation != null and geoLocation != ''">#{geoLocation},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="status != null">#{status},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="sort != null">#{sort},</if>
            <if test="concat != null">#{concat},</if>
            <if test="phone != null">#{phone},</if>
         </trim>
    </insert>

    <update id="updateWhHouseInfo" parameterType="com.bonus.material.warehouse.domain.WhHouseInfo">
        update wh_house_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="houseName != null and houseName != ''">house_name = #{houseName},</if>
            <if test="physicalName != null and physicalName != ''">physical_name = #{physicalName},</if>
            <if test="geoLocation != null and geoLocation != ''">geo_location = #{geoLocation},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="concat != null">concat = #{concat},</if>
            <if test="phone != null">phone = #{phone},</if>
        </trim>
        where house_id = #{houseId}
    </update>

    <delete id="deleteWhHouseInfoByHouseId" parameterType="Long">
        delete from wh_house_info where house_id = #{houseId}
    </delete>

    <delete id="deleteWhHouseInfoByHouseIds" parameterType="String">
        update wh_house_info
        set del_flag = '2'
        where house_id in
        <foreach item="houseId" collection="array" open="(" separator="," close=")">
            #{houseId}
        </foreach>
    </delete>
</mapper>