<?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.sgzb.base.mapper.MaTypeMapper">

    <sql id="selectMaTypeVo">
        select type_id, type_name, parent_id, status, num, unit_id, manage_type, lease_price, buy_price, pay_price, level, rated_load, test_load, holding_time, warn_num, del_flag, create_by, create_time, remark, company_id
        from ma_type
    </sql>

    <insert id="insertType" parameterType="com.bonus.sgzb.base.domain.MaType" useGeneratedKeys="true" keyProperty="typeId">
        insert into ma_type(
        <if test="typeId != null and typeId != 0">type_id,</if>
        <if test="typeName != null and typeName != ''">type_name,</if>
        <if test="parentId != null and parentId != 0">parent_id,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="num != null and num != 0">num,</if>
        <if test="unitId != null and unitId != 0">unit_id,</if>
        <if test="manageType != null and manageType != 0">manage_type,</if>
        <if test="leasePrice != null and leasePrice != 0">lease_price,</if>
        <if test="buyPrice != null and buyPrice != 0">buy_price,</if>
        <if test="payPrice != null and payPrice != 0">pay_price,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="companyId != null and companyId != ''">company_id,</if>
        create_time
        )values(
        <if test="typeId != null and typeId != 0">#{typeId},</if>
        <if test="typeName != null and typeName != ''">#{typeName},</if>
        <if test="parentId != null and parentId != 0">#{parentId},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="num != null and num != ''">#{num},</if>
        <if test="unitId != null and unitId != 0">#{unitId},</if>
        <if test="manageType != null and manageType != ''">#{manageType},</if>
        <if test="leasePrice != null and leasePrice != ''">#{leasePrice},</if>
        <if test="buyPrice != null and buyPrice != ''">#{buyPrice},</if>
        <if test="payPrice != null and payPrice != ''">#{payPrice},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="companyId != null and companyId != ''">#{companyId},</if>
        sysdate()
        )
    </insert>

    <insert id="insertTypeFile" parameterType="com.bonus.sgzb.base.domain.MaTypeFile" useGeneratedKeys="true" keyProperty="typeId">
        insert into ma_type_file(
        <if test="typeId != null and typeId != 0">type_id,</if>
        <if test="maId != null and maId != ''">ma_id,</if>
        <if test="fileName != null and fileName != 0">file_name,</if>
        <if test="fileUrl != null and fileUrl != ''">file_url,</if>
        <if test="fileType != null and fileType != 0">file_type,</if>
        <if test="userId != null and userId != 0">user_id,</if>
        <if test="status != null and status != 0">status,</if>
        <if test="companyId != null and companyId != ''">company_id,</if>
        time
        )values(
        <if test="typeId != null and typeId != 0">#{typeId},</if>
        <if test="maId != null and maId != ''">#{maId},</if>
        <if test="fileName != null and fileName != 0">#{fileName},</if>
        <if test="fileUrl != null and fileUrl != ''">#{fileUrl},</if>
        <if test="fileType != null and fileType != ''">#{fileType},</if>
        <if test="userId != null and userId != 0">#{userId},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="companyId != null and companyId != ''">#{companyId},</if>
        sysdate()
        )
    </insert>

    <update id="updateType">
        update ma_type
        <set>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="parentId != null and parentId != ''">parent_id = #{parentId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="num != null and num != ''">num = #{num},</if>
            <if test="unitId != null and unitId != ''">unit_id = #{unitId},</if>
            <if test="manageType != null and manageType != ''">manage_type = #{manageType},</if>
            <if test="leasePrice != null and leasePrice != ''">lease_price = #{leasePrice},</if>
            <if test="buyPrice != null and buyPrice != ''">buy_price = #{buyPrice},</if>
            <if test="payPrice != null and payPrice != ''">pay_price = #{payPrice},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="companyId != null and companyId != ''">company_id = #{companyId},</if>
            update_time = sysdate()
        </set>
        where type_id = #{typeId}
    </update>

    <delete id="deleteTypeById" parameterType="Long">
        update ma_type set del_flag = '2' where type_id = #{typeId}
    </delete>

    <select id="selectMaTypeList" resultType="com.bonus.sgzb.base.domain.MaType">
        select m.type_id, m.type_name, m.parent_id, m.status, m.num, m.unit_id, m.manage_type, m.lease_price, m.buy_price, m.pay_price, m.level, m.rated_load, m.test_load, m.holding_time, m.warn_num, mtf.file_name, mtf.file_url, su.user_name, mpi.prop_name, m.del_flag, m.create_by, m.create_time, m.remark, m.company_id
        from ma_type m
        left join ma_prop_set mps on m.type_id = mps.type_id
        left join ma_prop_info mpi on mps.prop_id = mpi.prop_id
        left join ma_type_file mtf on m.type_id = mtf.type_id
        left join ma_type_keeper mtk on mtf.type_id = mtk.type_id
        left join sys_user su on mtk.user_id = su.user_id
    </select>

    <select id="selectTypeList" resultType="com.bonus.sgzb.base.domain.MaType">
        <include refid="selectMaTypeVo"/>
        <where>
            <if test="typeName != null and typeName != ''">
                AND type_name like concat('%', #{typeName}, '%')
            </if>
        </where>
    </select>

    <select id="selectTypeById" resultType="java.lang.Integer">
        select count(1) from ma_type
        where del_flag = '0' and parent_id = #{typeId} limit 1
    </select>

    <select id="checkTypeNameUnique" resultType="com.bonus.sgzb.base.domain.MaType">
        <include refid="selectMaTypeVo"/>
        where type_name = #{typeName} limit 1
    </select>
</mapper>