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

    <select id="getIsMaterialsChangeRecordPage" resultType="com.ktg.iscs.domain.vo.change.ChangeRecordVO">
        SELECT
            c.*,
            t.materials_type_name,
            t.materials_type_icon,
            t.materials_type_picture,
            u.nick_name as change_user_name,
            mc.cabinet_id,
            mc.cabinet_name,
            m.materials_name as new_materials_name,
            m2.materials_name as old_materials_name
        FROM
            is_materials_change_record c
            LEFT JOIN is_materials m ON m.materials_id = c.new_materials_id
            LEFT JOIN is_materials m2 ON m2.materials_id = c.old_materials_id
            LEFT JOIN is_materials_cabinet mc ON mc.cabinet_id = m.materials_cabinet_id
            LEFT JOIN is_materials_type t ON t.materials_type_id = m.materials_type_id
            LEFT JOIN sys_user u ON u.user_id = c.change_user_id
        <where>
            <if test="dto.recordId != null">
                and c.check_record_id = #{dto.recordId}
            </if>
            <if test="dto.cabinetId != null">
                and m.materials_cabinet_id = #{dto.cabinetId}
            </if>
            <if test="dto.materialsTypeId != null">
                and m.materials_type_id = #{dto.materialsTypeId}
            </if>
            <if test="dto.changeUserId != null">
                and c.change_user_id = #{dto.changeUserId}
            </if>
            <if test="dto.changeUserName != null and dto.changeUserName.trim != ''">
                and u.nick_name like concat('%',#{dto.changeUserName},'%')
            </if>
            <if test="dto.startTime != null and dto.startTime.trim != ''">
                and c.change_date &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime.trim != ''">
                and c.change_date &lt;= #{dto.endTime}
            </if>
            <if test="dto.oldMaterialsId != null">
                and c.old_materials_id = #{dto.oldMaterialsId}
            </if>
            <if test="dto.oldMaterialsRfid != null and dto.oldMaterialsRfid.trim != ''">
                and c.old_materials_rfid like concat('%',#{dto.oldMaterialsRfid},'%')
            </if>
            <if test="dto.newMaterialsId != null">
                and c.new_materials_id = #{dto.newMaterialsId}
            </if>
            <if test="dto.newMaterialsRfid != null and dto.newMaterialsRfid.trim != ''">
                and c.new_materials_rfid like concat('%',#{dto.newMaterialsRfid},'%')
            </if>
            <if test="dto.operateType != null and dto.operateType.trim != '' and dto.operateType == 3">
                and c.operate_type = '3'
            </if>
            <if test="dto.operateType != null and dto.operateType.trim != '' and dto.operateType == 4">
                and c.operate_type != '3'
            </if>
        </where>
        order by c.change_id desc
    </select>
</mapper>
