<?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.bonus.material.basic.mapper.BmAssetAttributesMapper">

    <insert id="insertBmUnitType">
        insert into bm_unit_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="companyId != null">company_id,</if>
            del_flag,
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="companyId != null">#{companyId},</if>
            0,
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <insert id="insert">
        insert into bm_asset_attributes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="assetCode != null and assetCode != ''">asset_code,</if>
            <if test="assetName != null and assetName != ''">asset_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            del_flag
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="assetCode != null and assetCode != ''">#{assetCode},</if>
            <if test="assetName != null and assetName != ''">#{assetName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            0
        </trim>
    </insert>

    <update id="update">
        update bm_asset_attributes
        <trim prefix="SET" suffixOverrides=",">
            <if test="assetCode != null and assetCode != ''">asset_code = #{assetCode},</if>
            <if test="assetName != null and assetName != ''">asset_name = #{assetName},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime !=null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteById">
        update bm_asset_attributes set del_flag = 2 where id = #{id}
    </delete>

    <select id="queryAllByLimit" resultType="com.bonus.material.basic.domain.BmAssetAttributes">
        select bma.id as id, bma.asset_code as assetCode, bma.asset_name as assetName, bma.create_by as createBy,
        bma.create_time as createTime, bma.del_flag as delFlag, bma.update_by as updateBy, bma.update_time as updateTime
        from bm_asset_attributes bma
        where bma.del_flag = '0'
            <if test="assetName != null and assetName!= ''">
                and bma.asset_name like concat('%', #{assetName}, '%')
            </if>
    </select>

    <select id="queryById" resultType="com.bonus.material.basic.domain.BmAssetAttributes">
        select bma.id as id, bma.asset_code as assetCode, bma.asset_name as assetName, bma.create_by as createBy,
        bma.create_time as createTime, bma.del_flag as delFlag, bma.update_by as updateBy, bma.update_time as updateTime
        from bm_asset_attributes bma
        where bma.del_flag = '0' and bma.id = #{id}
    </select>

    <select id="selectBmAssetAttributesByAssetCode"
            resultType="com.bonus.material.basic.domain.BmAssetAttributes">
        select bma.id as id, bma.asset_code as assetCode, bma.asset_name as assetName, bma.create_by as createBy,
        bma.create_time as createTime, bma.del_flag as delFlag, bma.update_by as updateBy, bma.update_time as updateTime
        from bm_asset_attributes bma
        where bma.del_flag = '0'
        <if test="assetName != null and assetName != ''">
            <if test="assetCode != null and assetCode != ''">
                and (bma.asset_name = #{assetName} or bma.asset_code = #{assetCode})
            </if>
            <if test="assetCode == null or assetCode == ''">
                and bma.asset_code = #{assetCode}
            </if>
        </if>
        <if test="assetName == null or assetName == ''">
            <if test="assetCode != null and assetCode != ''">
                and bma.asset_code  = #{assetCode}
            </if>
        </if>
    </select>
</mapper>