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

    <select id="getIsMaterialsLoanExceptionPage"
            resultType="com.ktg.iscs.domain.vo.exception.IsMaterialsLoanExceptionPageVO">
        SELECT
            e.*,
            m.materials_name,
            c1.cabinet_name AS loan_from_name,
            c2.cabinet_name AS restitution_to_name,
            u.nick_name AS restitution_user_name,
            mt.materials_type_icon,
            mt.materials_type_picture,
            m.materials_rfid
        FROM
            is_exception_misplace e
                LEFT JOIN is_materials m ON e.materials_id = m.materials_id
                LEFT JOIN is_materials_type mt ON m.materials_type_id = mt.materials_type_id
                LEFT JOIN is_materials_cabinet c1 ON c1.cabinet_id = e.loan_from_id
                LEFT JOIN is_materials_cabinet c2 ON c2.cabinet_id = e.restitution_to_id
                LEFT JOIN sys_user u ON u.user_id = e.restitution_user_id
        <where>
            <if test="dto.materialsName != null and dto.materialsName.trim != ''">
                and m.materials_name like concat('%',#{dto.materialsName},'%')
            </if>
            <if test="dto.restitutionUserName != null and dto.restitutionUserName.trim != ''">
                and u.nick_name like concat('%',#{dto.restitutionUserName},'%')
            </if>
            <if test="dto.loanFromId != null">
                and (e.loan_from_id = #{dto.loanFromId} or e.restitution_to_id = #{dto.loanFromId})
            </if>
            <if test="dto.startTime != null and dto.startTime.trim != ''">
                and e.occur_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime.trim != ''">
                and e.occur_time &lt;= #{dto.endTime}
            </if>
            <if test="dto.handleStartTime != null and dto.handleStartTime.trim != ''">
                and e.handle_time &gt;= #{dto.handleStartTime}
            </if>
            <if test="dto.handleEndTime != null and dto.handleEndTime.trim != ''">
                and e.handle_time &lt;= #{dto.handleEndTime}
            </if>
            <if test="dto.status != null and dto.status.trim != ''">
                and e.status = #{dto.status}
            </if>
            <if test="dto.exceptionType != null and dto.exceptionType.trim != ''">
                and e.exception_type = #{dto.exceptionType}
            </if>
        </where>
        order by e.misplace_id desc
    </select>
</mapper>
