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

    <resultMap type="IsMaterials" id="IsMaterialsResult">
        <result property="materialsId"    column="materials_id"    />
        <result property="materialsCode"    column="materials_code"    />
        <result property="materialsName"    column="materials_name"    />
        <result property="workareaId"    column="workarea_id"    />
        <result property="materialsTypeId"    column="materials_type_id"    />
        <result property="materialsCabinetId"    column="materials_cabinet_id"    />
        <result property="serviceLife"    column="service_life"    />
        <result property="availableLife"    column="available_life"    />
        <result property="serviceTimes"    column="service_times"    />
        <result property="availableTimes"    column="available_times"    />
        <result property="startTime"    column="start_time"    />
        <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"    />
        <result property="loanState"    column="loan_state"    />
        <result property="materialsRfid"    column="materials_rfid"    />
        <result property="supplier"    column="supplier"    />
        <result property="expirationDate"    column="expiration_date"    />
        <result property="properties"    column="properties"    />
    </resultMap>

    <sql id="selectIsMaterialsVo">
        select * from is_materials
    </sql>

    <select id="selectIsMaterialsList" parameterType="IsMaterials" resultMap="IsMaterialsResult">
        <include refid="selectIsMaterialsVo"/>
        <where>
            <if test="materialsCode != null  and materialsCode != ''"> and materials_code = #{materialsCode}</if>
            <if test="materialsName != null  and materialsName != ''"> and materials_name like concat('%', #{materialsName}, '%')</if>
            <if test="materialsTypeId != null "> and materials_type_id = #{materialsTypeId}</if>
            <if test="workareaId != null "> and workarea_id = #{workareaId}</if>
            <if test="materialsCabinetId != null "> and materials_cabinet_id = #{materialsCabinetId}</if>
            <if test="serviceLife != null  and serviceLife != ''"> and service_life = #{serviceLife}</if>
            <if test="availableLife != null  and availableLife != ''"> and available_life = #{availableLife}</if>
            <if test="serviceTimes != null "> and service_times = #{serviceTimes}</if>
            <if test="availableTimes != null "> and available_times = #{availableTimes}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
        </where>
    </select>

    <select id="selectIsMaterialsByMaterialsId" parameterType="Long" resultMap="IsMaterialsResult">
        <include refid="selectIsMaterialsVo"/>
        where materials_id = #{materialsId}
    </select>

    <insert id="insertIsMaterials" parameterType="IsMaterials" useGeneratedKeys="true" keyProperty="materialsId">
        insert into is_materials
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialsCode != null and materialsCode != ''">materials_code,</if>
            <if test="materialsName != null and materialsName != ''">materials_name,</if>
            <if test="materialsTypeId != null">materials_type_id,</if>
            <if test="workareaId != null">workarea_id,</if>
            <if test="materialsCabinetId != null">materials_cabinet_id,</if>
            <if test="serviceLife != null">service_life,</if>
            <if test="availableLife != null">available_life,</if>
            <if test="serviceTimes != null">service_times,</if>
            <if test="availableTimes != null">available_times,</if>
            <if test="startTime != null">start_time,</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>
            <if test="loanState != null and loanState != ''">loan_state,</if>
            <if test="materialsRfid != null">materials_rfid,</if>
            <if test="supplier != null and supplier != ''">supplier,</if>
            <if test="expirationDate != null">expiration_date,</if>
            <if test="properties != null and properties != ''">properties,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialsCode != null and materialsCode != ''">#{materialsCode},</if>
            <if test="materialsName != null and materialsName != ''">#{materialsName},</if>
            <if test="materialsTypeId != null">#{materialsTypeId},</if>
            <if test="workareaId != null">#{workareaId},</if>
            <if test="materialsCabinetId != null">#{materialsCabinetId},</if>
            <if test="serviceLife != null">#{serviceLife},</if>
            <if test="availableLife != null">#{availableLife},</if>
            <if test="serviceTimes != null">#{serviceTimes},</if>
            <if test="availableTimes != null">#{availableTimes},</if>
            <if test="startTime != null">#{startTime},</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>
            <if test="loanState != null and loanState != ''">#{loanState},</if>
            <if test="materialsRfid != null">#{materialsRfid},</if>
            <if test="supplier != null and supplier != ''">#{supplier},</if>
            <if test="expirationDate != null">#{expirationDate},</if>
            <if test="properties != null and properties != ''">#{properties},</if>
         </trim>
    </insert>

    <update id="updateIsMaterials" parameterType="IsMaterials">
        update is_materials
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialsCode != null and materialsCode != ''">materials_code = #{materialsCode},</if>
            <if test="materialsName != null and materialsName != ''">materials_name = #{materialsName},</if>
            <if test="materialsTypeId != null">materials_type_id = #{materialsTypeId},</if>
            <if test="workareaId != null">workarea_id = #{workareaId},</if>
            <if test="materialsCabinetId != null">materials_cabinet_id = #{materialsCabinetId},</if>
            <if test="serviceLife != null">service_life = #{serviceLife},</if>
            <if test="availableLife != null">available_life = #{availableLife},</if>
            <if test="serviceTimes != null">service_times = #{serviceTimes},</if>
            <if test="availableTimes != null">available_times = #{availableTimes},</if>
            <if test="startTime != null">start_time = #{startTime},</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>
            <if test="loanState != null and loanState != ''">loan_state = #{loanState},</if>
            <if test="materialsRfid != null">materials_rfid = #{materialsRfid},</if>
            <if test="materialsRfid == null || materialsName == ''">materials_rfid = null,</if>
            <if test="supplier != null and supplier != ''">supplier = #{supplier},</if>
            <if test="expirationDate != null">expiration_date = #{expirationDate},</if>
            <if test="expirationDate == null">expiration_date = null,</if>
            <if test="properties != null and properties != ''">properties = #{properties},</if>
        </trim>
        where materials_id = #{materialsId}
    </update>

    <delete id="deleteIsMaterialsByMaterialsId" parameterType="Long">
        delete from is_materials where materials_id = #{materialsId}
    </delete>

    <delete id="deleteIsMaterialsByMaterialsIds" parameterType="String">
        delete from is_materials where materials_id in
        <foreach item="materialsId" collection="array" open="(" separator="," close=")">
            #{materialsId}
        </foreach>
    </delete>

    <select id="getIsMaterialsPage" resultType="com.ktg.iscs.domain.vo.materials.MaterialsPageVO">
        SELECT
        m.*,
        t.materials_type_name,
        c.cabinet_name,
        t.materials_type_icon,
        t.materials_type_picture
        FROM
        is_materials m
        LEFT JOIN is_materials_type t ON t.materials_type_id = m.materials_type_id
        LEFT JOIN is_materials_cabinet c ON c.cabinet_id = m.materials_cabinet_id
        <where>
            <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.loanState != null and dto.loanState.trim != ''">
                and m.loan_state = #{dto.loanState}
            </if>
            <if test="dto.status != null and dto.status.trim != ''">
                and m.status = #{dto.status}
            </if>
            <if test="dto.materialsCabinetId != null and dto.materialsCabinetId > 0">
                and m.materials_cabinet_id = #{dto.materialsCabinetId}
            </if>
            <if test="dto.materialsCabinetId != null and dto.materialsCabinetId == 0">
                and (m.materials_cabinet_id is null or m.materials_cabinet_id = 0)
            </if>
            <if test="dto.materialsTypeId != null">
                and m.materials_type_id = #{dto.materialsTypeId}
            </if>
            <if test="dto.startTime != null and dto.startTime.trim != ''">
                and m.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime.trim != ''">
                and m.create_time &lt;= #{dto.endTime}
            </if>
            <if test="dto.startExpirationDate != null and dto.startExpirationDate.trim != ''">
                and m.expiration_date &gt;= #{dto.startExpirationDate}
            </if>
            <if test="dto.endExpirationDate != null and dto.endExpirationDate.trim != ''">
                and m.expiration_date &lt;= #{dto.endExpirationDate}
            </if>
            <if test="dto.supplier != null and dto.supplier.trim != ''">
                and m.supplier like concat('%',#{dto.supplier},'%')
            </if>
            <if test="dto.materialsRfid != null and dto.materialsRfid.trim != ''">
                and m.materials_rfid like concat('%',#{dto.materialsRfid},'%')
            </if>
            <if test="dto.propertyId != null and dto.propertyId.trim != ''">
                and m.properties like concat('%',#{dto.propertyId},'%')
            </if>
            <if test="dto.recordId != null and dto.recordId.trim != ''">
                and m.properties like concat('%',#{dto.recordId},'%')
            </if>
        </where>
        ORDER BY m.materials_id DESC
    </select>
    <select id="getExMaterials" resultType="com.ktg.iscs.domain.vo.materials.MaterialsPageVO">
        SELECT
            m.*,
            t.materials_type_name,
            c.cabinet_name,
            t.materials_type_icon,
            t.materials_type_picture
        FROM
            is_materials m
                LEFT JOIN is_materials_type t ON t.materials_type_id = m.materials_type_id
                LEFT JOIN is_materials_cabinet c ON c.cabinet_id = m.materials_cabinet_id
        <where>
            m.materials_id in
            <foreach collection="materialsIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>
    <select id="getCheckMaterialsByCabinetId"
            resultType="com.ktg.iscs.domain.vo.hardwareMaterialApi.CheckMaterialsDateVO">
        SELECT
            m.materials_id,
            m.materials_name,
            m.status,
            m.materials_cabinet_id,
            m.materials_type_id,
            m.materials_rfid,
            t.materials_type_name,
            t.materials_type_icon,
            t.materials_type_picture,
            t.check_standard
        FROM
            is_materials m
                LEFT JOIN is_materials_type t ON t.materials_type_id = m.materials_type_id
        where m.materials_cabinet_id = #{cabinetId} and m.loan_state = '1'
    </select>
    <select id="getMaterialListByRfid" resultType="com.ktg.iscs.domain.IsMaterials">
        SELECT
            m.*,
            t.materials_type_icon,
            t.materials_type_picture
        FROM
            is_materials m
                LEFT JOIN is_materials_type t ON t.materials_type_id = m.materials_type_id
        where m.materials_rfid in
        <foreach collection="rfidList" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>
