<?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.TMaterialInfoMapper">

    <resultMap type="TMaterialInfo" id="TMaterialInfoResult">
        <result property="id"    column="id"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialType"    column="material_type"    />
        <result property="num"    column="num"    />
        <result property="performance"    column="performance"    />
        <result property="purpose"    column="purpose"    />
        <result property="validityTime"    column="validity_time"    />
        <result property="deptId"    column="dept_id"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="address"    column="address"    />
        <result property="contacts"    column="contacts"    />
        <result property="phone"    column="phone"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <sql id="selectTMaterialInfoVo">
        select id, material_name, material_type, num, performance, purpose, validity_time, dept_id, longitude, latitude, address, contacts, phone, create_time, update_time, is_delete from t_material_info
    </sql>

    <select id="selectTMaterialInfoList" parameterType="TMaterialInfo" resultMap="TMaterialInfoResult">
        select d.dept_name,a.id, a.material_name, a.material_type, a.num, a.performance, a.purpose, a.validity_time, a.dept_id, a.longitude, a.latitude, a.address, a.contacts, a.phone, a.create_time, a.update_time, a.is_delete
        from t_material_info a
        LEFT JOIN sys_dept d ON d.`dept_id` = a.`dept_id`
        <where>
            <if test="materialName != null  and materialName != ''"> and a.material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialType != null "> and a.material_type = #{materialType}</if>
        </where>
    </select>

    <select id="selectTMaterialInfoById" parameterType="Long" resultMap="TMaterialInfoResult">
        <include refid="selectTMaterialInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertTMaterialInfo" parameterType="TMaterialInfo" useGeneratedKeys="true" keyProperty="id">
        insert into t_material_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="materialType != null">material_type,</if>
            <if test="num != null">num,</if>
            <if test="performance != null">performance,</if>
            <if test="purpose != null">purpose,</if>
            <if test="validityTime != null">validity_time,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="longitude != null and longitude != ''">longitude,</if>
            <if test="latitude != null and latitude != ''">latitude,</if>
            <if test="address != null">address,</if>
            <if test="contacts != null and contacts != ''">contacts,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDelete != null">is_delete,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="num != null">#{num},</if>
            <if test="performance != null">#{performance},</if>
            <if test="purpose != null">#{purpose},</if>
            <if test="validityTime != null">#{validityTime},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="longitude != null and longitude != ''">#{longitude},</if>
            <if test="latitude != null and latitude != ''">#{latitude},</if>
            <if test="address != null">#{address},</if>
            <if test="contacts != null and contacts != ''">#{contacts},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
         </trim>
    </insert>

    <update id="updateTMaterialInfo" parameterType="TMaterialInfo">
        update t_material_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="num != null">num = #{num},</if>
            <if test="performance != null">performance = #{performance},</if>
            <if test="purpose != null">purpose = #{purpose},</if>
            <if test="validityTime != null">validity_time = #{validityTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="longitude != null and longitude != ''">longitude = #{longitude},</if>
            <if test="latitude != null and latitude != ''">latitude = #{latitude},</if>
            <if test="address != null">address = #{address},</if>
            <if test="contacts != null and contacts != ''">contacts = #{contacts},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTMaterialInfoById" parameterType="Long">
        delete from t_material_info where id = #{id}
    </delete>

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