<?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.jeethink.dataCenter.mapper.WarehouseMapper">
    
    <resultMap type="Warehouse" id="warehouseResult">
        <id     property="id"               column="id"    />
        <result property="name"             column="name"    />
        <result property="ancestors"        column="ancestors"    />
        <result property="level"            column="level"    />
        <result property="parentId"         column="parent_id"    />
        <result property="description"      column="description"    />
        <result property="orderNum"         column="order_num"/>
        <result property="status"           column="status"    />
        <result property="createBy"         column="create_by"    />
        <result property="updateBy"         column="update_by"    />
        <result property="createTime"       column="create_time"    />
        <result property="updateTime"       column="update_time"    />
        <result property="relaUser"         column="rela_user" />
        <result property="relaUserName"     column="rela_user_name" />
        <result property="wlh"     column="wlh" />
        <result property="lwz"     column="lwz" />
        <result property="ch"     column="ch" />
        <result property="jh"     column="jh" />
        <result property="archivesId"     column="archives_id" />
    </resultMap>

    <sql id="selectWarehouseVo">
        select w.id, w.name, w.ancestors, w.level, w.parent_id, w.description, w.status, w.order_num,
               w.create_by, w.create_time, w.update_by, w.update_time, w.rela_user,w.wlh,w.lwz,w.ch,w.jh,w.archives_id,
               u.nick_name as rela_user_name
        from warehouse w
        left join sys_user u on w.rela_user = u.user_id
    </sql>
    <insert id="insertWarehouse" parameterType="Warehouse">
        insert warehouse (name, parent_id, ancestors, level, description, status, order_num, rela_user, create_by, create_time, update_by, update_time,wlh,lwz,jh,ch,archives_id
        ) values (
        #{name}, #{parentId}, #{ancestors}, #{level}, #{description}, #{status}, #{orderNum}, #{relaUser}, #{createBy}, now(), #{updateBy}, now(),#{wlh},#{lwz},#{jh},#{ch},#{archivesId}
        )
    </insert>
    <update id="updateWarehouse" parameterType="Warehouse">
        update warehouse set name = #{name}, parent_id = #{parentId}, ancestors = #{ancestors}, level = #{level}, description = #{description},
            status = #{status}, order_num = #{orderNum}, rela_user = #{relaUser}, update_by = #{updateBy}, update_time = now(),wlh=#{wlh},lwz=#{lwz},jh=#{jh},ch=#{ch},
            archives_id = #{archivesId}
        where id = #{id}
    </update>
    <delete id="deleteWarehouseById">
        delete from warehouse where id = #{0}
    </delete>
    <select id="selectWarehouseList" parameterType="Warehouse" resultMap="warehouseResult">
        <include refid="selectWarehouseVo"/>
        <where>
            <if test="name != null and name != ''"> w.name like concat('%', #{name}, '%') </if>
            <if test="status != null and status != ''"> and w.status = #{status}</if>
             <if test="parentId != null">and w.parent_id = #{parentId}</if>
        </where>
        order by w.order_num 
    </select>
    <select id="selectWarehouseById" resultMap="warehouseResult">
        <include refid="selectWarehouseVo"/>
        where id = #{0}
    </select>
    <select id="checkNameUnique" resultMap="warehouseResult">
        <include refid="selectWarehouseVo"/>
        where w.name = #{name} and w.parent_id = #{parentId}
    </select>
    <select id="selectNormalChildrenById" resultType="java.lang.Integer">
        select count(*) from warehouse where status = '0' and parent_id = #{0}
    </select>
    <select id="getChildList" resultMap="warehouseResult">
        <include refid="selectWarehouseVo"/>
        where w.parent_id = #{0}
    </select>
    <select id="selectWarehouseByRelaUser" resultMap="warehouseResult">
        <include refid="selectWarehouseVo"/>
        where w.rela_user = #{0}
    </select>
    <delete id="deleteArchives" parameterType="Long" >
        delete  from warehouse where parent_id=#{id}
    </delete>
<select id="selectArchivesById" parameterType="String" resultType="String">
    select name from archives_master where archives_id=#{archivesId}
</select>
    <select id="selectArchivesByParentId" parameterType="Long" resultType="java.lang.String">
        select archives_id from warehouse where parent_id=#{parentId}
    </select>
</mapper>