<?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.ktg.iscs.mapper.IsMaterialsReminderMapper">

    <resultMap type="IsMaterialsReminder" id="IsMaterialsReminderResult">
        <result property="recordId"    column="record_id"    />
        <result property="materialsLoanId"    column="materials_loan_id"    />
        <result property="reminderType"    column="reminder_type"    />
        <result property="readFlag"    column="read_flag"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectIsMaterialsReminderVo">
        select record_id, materials_loan_id, reminder_type, read_flag, remark, del_flag, create_by, create_time, update_time, update_by from is_materials_reminder
    </sql>

    <select id="selectIsMaterialsReminderList" parameterType="IsMaterialsReminder" resultMap="IsMaterialsReminderResult">
        <include refid="selectIsMaterialsReminderVo"/>
        <where>
            <if test="materialsLoanId != null "> and materials_loan_id = #{materialsLoanId}</if>
            <if test="reminderType != null "> and reminder_type = #{reminderType}</if>
            <if test="readFlag != null "> and read_flag = #{readFlag}</if>
        </where>
    </select>

    <select id="selectIsMaterialsReminderByRecordId" parameterType="Long" resultMap="IsMaterialsReminderResult">
        <include refid="selectIsMaterialsReminderVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertIsMaterialsReminder" parameterType="IsMaterialsReminder" useGeneratedKeys="true" keyProperty="recordId">
        insert into is_materials_reminder
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialsLoanId != null">materials_loan_id,</if>
            <if test="reminderType != null">reminder_type,</if>
            <if test="readFlag != null">read_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialsLoanId != null">#{materialsLoanId},</if>
            <if test="reminderType != null">#{reminderType},</if>
            <if test="readFlag != null">#{readFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateIsMaterialsReminder" parameterType="IsMaterialsReminder">
        update is_materials_reminder
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialsLoanId != null">materials_loan_id = #{materialsLoanId},</if>
            <if test="reminderType != null">reminder_type = #{reminderType},</if>
            <if test="readFlag != null">read_flag = #{readFlag},</if>
            <if test="remark != null">remark = #{remark},</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="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteIsMaterialsReminderByRecordId" parameterType="Long">
        delete from is_materials_reminder where record_id = #{recordId}
    </delete>

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

    <select id="getIsMaterialsReminderPage"
            resultType="com.ktg.iscs.domain.vo.materialsReminder.MaterialsReminderVO">
        SELECT
        r.*,
        l.*,
        m.materials_code,
        m.materials_name,
        c1.cabinet_name as loan_from_name,
        c2.cabinet_name as restitution_to_name,
        u.user_name as loan_user_name
        FROM
        is_materials_reminder r
        LEFT JOIN is_materials_loan l ON r.materials_loan_id = l.materials_loan_id
        LEFT JOIN is_materials m ON m.materials_id = l.materials_id
        LEFT JOIN is_materials_cabinet c1 ON c1.cabinet_id = l.loan_from_id
        LEFT JOIN is_materials_cabinet c2 ON c2.cabinet_id = l.restitution_to_id
        LEFT JOIN sys_user u ON u.user_id = l.loan_user_id
        <where>
            r.reminder_type = #{dto.reminderType}
            <if test="dto.readFlag != null">
                and r.read_flag = #{dto.readFlag}
            </if>
            <if test="dto.materialsCode != null and dto.materialsCode.trim != ''">
                and m.materials_code like concat('%',#{dto.materialsCode},'%')
            </if>
            <if test="dto.materialsName != null and dto.materialsName.trim != ''">
                and m.materials_name like concat('%',#{dto.materialsName},'%')
            </if>
            <if test="dto.loanUserName != null and dto.loanUserName.trim != ''">
                and u.user_name like concat('%',#{dto.loanUserName},'%')
            </if>
            <if test="dto.loanFromId != null">
                and l.loan_from_id = #{dto.loanFromId}
            </if>
            <if test="dto.loanFromName != null and dto.loanFromName.trim != ''">
                and c1.cabinet_name like concat('%',#{dto.loanFromName},'%')
            </if>
            <if test="dto.startTime != null and dto.startTime.trim != ''">
                and l.loan_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime.trim != ''">
                and l.loan_time &lt;= #{dto.endTime}
            </if>
        </where>
        ORDER BY r.record_id DESC




    </select>

</mapper>
