<?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.WhHouseSetMapper">
    <resultMap type="com.bonus.material.warehouse.domain.WhHouseSet" id="WhHouseSetResult">
        <result property="id"    column="id"    />
        <result property="houseId"    column="house_id"    />
        <result property="houseName"    column="house_name"    />
        <result property="typeId"    column="type_id"    />
        <result property="maId"    column="ma_id"    />
        <result property="num"    column="num"    />
        <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="shelfNum"    column="shelf_num"    />
    </resultMap>

    <sql id="selectWhHouseSetVo">
        select id, house_id, type_id, ma_id, num, status, dept_id, del_flag, create_by, create_time, update_by, update_time, remark, company_id, shelf_num from wh_house_set
    </sql>

    <select id="selectWhHouseSetList" parameterType="com.bonus.material.warehouse.domain.WhHouseSet" resultMap="WhHouseSetResult">
        <include refid="selectWhHouseSetVo"/>
        <where>  
            <if test="houseId != null "> and house_id = #{houseId}</if>
            <if test="typeId != null "> and type_id = #{typeId}</if>
            <if test="maId != null "> and ma_id = #{maId}</if>
            <if test="num != null "> and num = #{num}</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 company_id = #{companyId}</if>
            <if test="shelfNum != null  and shelfNum != ''"> and shelf_num = #{shelfNum}</if>
        </where>
    </select>
    
    <select id="selectWhHouseSetById" parameterType="Long" resultMap="WhHouseSetResult">
        <include refid="selectWhHouseSetVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertWhHouseSet" parameterType="com.bonus.material.warehouse.domain.WhHouseSet" useGeneratedKeys="true" keyProperty="id">
        insert into wh_house_set
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="houseId != null">house_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="num != null">num,</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="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="shelfNum != null">shelf_num,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="houseId != null">#{houseId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="num != null">#{num},</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="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="shelfNum != null">#{shelfNum},</if>
         </trim>
    </insert>

    <update id="updateWhHouseSet" parameterType="com.bonus.material.warehouse.domain.WhHouseSet">
        update wh_house_set
        <trim prefix="SET" suffixOverrides=",">
            <if test="houseId != null">house_id = #{houseId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="maId != null">ma_id = #{maId},</if>
            <if test="num != null">num = #{num},</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="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</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="shelfNum != null">shelf_num = #{shelfNum},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWhHouseSetById" parameterType="Long">
        delete from wh_house_set where id = #{id}
    </delete>

    <delete id="deleteWhHouseSetByIds" parameterType="String">
        delete from wh_house_set where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectListByMaType" resultMap="WhHouseSetResult">
        SELECT
               mhi.house_id, mhs.type_id, mhs.ma_id, mhs.num, mhs.status, mhs.dept_id, mhs.del_flag,
               mhs.create_by, mhs.create_time, mhs.update_by,mhs.update_time,mhs.remark,mhs.company_id,mhi.house_name
        from wh_house_set mhs
                 left join ma_type mt on mt.type_id = mhs.type_id
                 left join ma_type mt1 on mt.parent_id = mt1.type_id
                 left join ma_type mt2 on mt1.parent_id = mt2.type_id
                 right join wh_house_info mhi on mhs.house_id = mhi.house_id
        where mhi.del_flag=0
    </select>
</mapper>