<?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.zehong.system.mapper.material.TRackMaterialRelationMapper">
    
    <resultMap type="TRackMaterialRelation" id="TRackMaterialRelationResult">
        <result property="relationId"    column="relation_id"    />
        <result property="rackCode"    column="rack_code"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialSpecifications"    column="material_specifications"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTRackMaterialRelationVo">
        SELECT
            relation.relation_id,
            relation.rack_code,
            info.material_code,
            info.material_name,
            info.material_specifications,
            relation.create_by,
            relation.create_time,
            relation.update_by,
            relation.update_time,
            shelfInfo.f_shelf as shelf
        FROM
            t_rack_material_relation relation
        LEFT JOIN t_rack_material_info info ON info.material_id = relation.material_id
        LEFT JOIN t_shelf_storage_location location ON location.f_rid = relation.rack_code
        LEFT JOIN t_shelf_info shelfInfo ON shelfInfo.f_shelf_id = location.f_shelf_id
    </sql>

    <select id="selectTRackMaterialRelationList" parameterType="TRackMaterialRelation" resultMap="TRackMaterialRelationResult">
        <include refid="selectTRackMaterialRelationVo"/>
        <where>  
            <if test="rackCode != null  and rackCode != ''"> and relation.rack_code like concat('%', #{rackCode}, '%')</if>
            <if test="materialCode != null  and materialCode != ''"> and info.material_code like concat('%', #{materialCode}, '%')</if>
            <if test="materialName != null  and materialName != ''"> and info.material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialSpecifications != null  and materialSpecifications != ''"> and info.material_specifications = #{materialSpecifications}</if>
            <if test="shelf != null  and shelf != ''"> and shelfInfo.f_shelf like concat('%', #{shelf}, '%')</if>
        </where>
    </select>
    
    <select id="selectTRackMaterialRelationById" parameterType="Long" resultMap="TRackMaterialRelationResult">
        <include refid="selectTRackMaterialRelationVo"/>
        where relation.relation_id = #{relationId}
    </select>

    <select id="selectTRackMaterialRelationByRackCode" parameterType="String" resultMap="TRackMaterialRelationResult">
        <include refid="selectTRackMaterialRelationVo"/>
        where relation.rack_code = #{rackCode}
    </select>
        
    <insert id="insertTRackMaterialRelation" parameterType="TRackMaterialRelation" useGeneratedKeys="true" keyProperty="relationId">
        insert into t_rack_material_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="rackCode != null and rackCode != ''">rack_code,</if>
            <if test="materialId != null and materialId != ''">material_id,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="rackCode != null and rackCode != ''">#{rackCode},</if>
            <if test="materialId != null and materialId != ''">#{materialId},</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>
         </trim>
    </insert>

    <update id="updateTRackMaterialRelation" parameterType="TRackMaterialRelation">
        update t_rack_material_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="rackCode != null and rackCode != ''">rack_code = #{rackCode},</if>
            <if test="materialId != null and materialId != ''">material_id = #{materialId},</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>
        </trim>
        where relation_id = #{relationId}
    </update>

    <delete id="deleteTRackMaterialRelationById" parameterType="Long">
        delete from t_rack_material_relation where relation_id = #{relationId}
    </delete>

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

    <delete id="deleteTRackMaterialRelationByRackCodes" parameterType="String">
        delete from t_rack_material_relation where rack_code in
        <foreach item="rackCode" collection="array" open="(" separator="," close=")">
            #{rackCode}
        </foreach>
    </delete>
</mapper>