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

    <resultMap type="IsMaterialsType" id="IsMaterialsTypeResult">
        <result property="materialsTypeId"    column="materials_type_id"    />
        <result property="materialsTypeCode"    column="materials_type_code"    />
        <result property="materialsTypeName"    column="materials_type_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="enableFlag"    column="enable_flag"    />
        <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="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="status"    column="status"    />
        <result property="materialsTypeIcon"    column="materials_type_icon"    />
        <result property="materialsTypePicture"    column="materials_type_picture"    />
        <result property="propertyIds"    column="property_ids"    />
    </resultMap>

    <sql id="selectIsMaterialsTypeVo">
        select * from is_materials_type
    </sql>

    <select id="selectIsMaterialsTypeList" parameterType="IsMaterialsType" resultMap="IsMaterialsTypeResult">
        <include refid="selectIsMaterialsTypeVo"/>
        <where>
            <if test="materialsTypeCode != null  and materialsTypeCode != ''"> and materials_type_code = #{materialsTypeCode}</if>
            <if test="materialsTypeName != null  and materialsTypeName != ''"> and materials_type_name like concat('%', #{materialsTypeName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="enableFlag != null  and enableFlag != ''"> and enable_flag = #{enableFlag}</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>
        </where>
    </select>


    <insert id="insertIsMaterialsType" parameterType="IsMaterialsType" useGeneratedKeys="true" keyProperty="materialsTypeId">
        insert into is_materials_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialsTypeCode != null and materialsTypeCode != ''">materials_type_code,</if>
            <if test="materialsTypeName != null and materialsTypeName != ''">materials_type_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
            <if test="enableFlag != null and enableFlag != ''">enable_flag,</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="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="status != null and status != ''">status,</if>
            <if test="materialsTypeIcon != null and materialsTypeIcon != ''">materials_type_icon,</if>
            <if test="materialsTypePicture != null and materialsTypePicture != ''">materials_type_picture,</if>
            <if test="checkStandard != null and checkStandard != ''">check_standard,</if>
            <if test="propertyIds != null and propertyIds != ''">property_ids,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialsTypeCode != null and materialsTypeCode != ''">#{materialsTypeCode},</if>
            <if test="materialsTypeName != null and materialsTypeName != ''">#{materialsTypeName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
            <if test="enableFlag != null and enableFlag != ''">#{enableFlag},</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="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="status != null and status != ''">#{status},</if>
            <if test="materialsTypeIcon != null and materialsTypeIcon != ''">#{materialsTypeIcon},</if>
            <if test="materialsTypePicture != null and materialsTypePicture != ''">#{materialsTypePicture},</if>
            <if test="checkStandard != null and checkStandard != ''">#{checkStandard},</if>
            <if test="propertyIds != null and propertyIds != ''">#{propertyIds},</if>
         </trim>
    </insert>

    <update id="updateIsMaterialsType" parameterType="IsMaterialsType">
        update is_materials_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialsTypeCode != null and materialsTypeCode != ''">materials_type_code = #{materialsTypeCode},</if>
            <if test="materialsTypeName != null and materialsTypeName != ''">materials_type_name = #{materialsTypeName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="enableFlag != null and enableFlag != ''">enable_flag = #{enableFlag},</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="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="status != null and status != ''">status = #{status},</if>
            <if test="materialsTypeIcon != null and materialsTypeIcon != ''">materials_type_icon = #{materialsTypeIcon},</if>
            <if test="materialsTypePicture != null and materialsTypePicture != ''">materials_type_picture = #{materialsTypePicture},</if>
            <if test="checkStandard != null and checkStandard != ''">check_standard = #{checkStandard},</if>
            <if test="propertyIds != null and propertyIds != ''">property_ids = #{propertyIds},</if>
        </trim>
        where materials_type_id = #{materialsTypeId}
    </update>

    <delete id="deleteIsMaterialsTypeByMaterialsTypeId" parameterType="Long">
        delete from is_materials_type where materials_type_id = #{materialsTypeId}
    </delete>

    <delete id="deleteIsMaterialsTypeByMaterialsTypeIds" parameterType="String">
        delete from is_materials_type where materials_type_id in
        <foreach item="materialsTypeId" collection="array" open="(" separator="," close=")">
            #{materialsTypeId}
        </foreach>
    </delete>

    <select id="getIsMaterialsTypePage" resultType="com.ktg.iscs.domain.vo.materialsType.MaterialsTypePageVO">
        SELECT
            t.*,
            r.rule_id,
            r.restitution_required,
            r.restoration_required,
            r.loan_duration,
            r.reminder_time,
            r.timeout_alarm
        FROM
            is_materials_type t
            LEFT JOIN is_materials_restitution_rules r ON r.materials_type_id = t.materials_type_id
        <where>
            <if test="isMaterialsType.materialsTypeCode != null and isMaterialsType.materialsTypeCode.trim != ''">
                and t.materials_type_code like concat('%',#{isMaterialsType.materialsTypeCode},'%')
            </if>
            <if test="isMaterialsType.materialsTypeName != null and isMaterialsType.materialsTypeName.trim != ''">
                and t.materials_type_name like concat('%',#{isMaterialsType.materialsTypeName},'%')
            </if>
            <if test="isMaterialsType.status != null and isMaterialsType.status.trim != ''">
                and t.status like concat('%',#{isMaterialsType.status},'%')
            </if>
        </where>
            order by t.materials_type_id asc
    </select>
    <select id="selectIsMaterialsTypeByMaterialsTypeId"
            resultType="com.ktg.iscs.domain.vo.materialsType.MaterialsTypePageVO">
        SELECT
            t.*,
            r.rule_id,
            r.restitution_required,
            r.restoration_required,
            r.loan_duration,
            r.reminder_time,
            r.timeout_alarm
        FROM
            is_materials_type t
            LEFT JOIN is_materials_restitution_rules r ON r.materials_type_id = t.materials_type_id
        WHERE t.materials_type_id = #{materialsTypeId}

    </select>

</mapper>
