<?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.MaPartTypeMapper">
    <resultMap type="com.bonus.sgzb.base.domain.MaPartType" id="MaPartTypeResult">
        <result property="paId"    column="pa_id"    />
        <result property="paName"    column="pa_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="status"    column="status"    />
        <result property="num"    column="num"    />
        <result property="unitId"    column="unit_id"    />
        <result property="buyPrice"    column="buy_price"    />
        <result property="level"    column="level"    />
        <result property="warnNum"    column="warn_num"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="year"    column="year"    />
    </resultMap>

    <sql id="selectMaPartTypeVo">
        select pa_id, pa_name, parent_id, status, num, unit_id, buy_price, level, warn_num, del_flag, create_by, create_time, remark, company_id
        from ma_part_type
    </sql>


    <insert id="insertMaPartType" parameterType="com.bonus.sgzb.base.mapper.MaPartTypeMapper" useGeneratedKeys="true" keyProperty="paId">
        insert into ma_part_type (
        <if test="paId != null and paId != 0">pa_id,</if>
        <if test="paName != null and paName != ''">pa_name,</if>
        parent_id,  <!-- 始终插入 parentId -->
        <if test="status != null and status != ''">status,</if>
        <if test="num != null and num != 0">num,</if>
        <if test="unitId != null and unitId != ''">unit_id,</if>
        <if test="buyPrice != null and buyPrice != 0">buy_price,</if>
        level,  <!-- 始终插入 level -->
        <if test="warnNum != null and warnNum != 0">warn_num,</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,
        year
        ) values (
        <if test="paId != null and paId != 0">#{paId},</if>
        <if test="paName != null and paName != ''">#{paName},</if>
        COALESCE(#{parentId}, 0),  <!-- 如果 parentId 为空，则设置为 0 -->
        <if test="status != null and status != ''">#{status},</if>
        <if test="num != null and num != 0">#{num},</if>
        <if test="unitId != null and unitId != ''">#{unitId},</if>
        <if test="buyPrice != null and buyPrice != 0">#{buyPrice},</if>
        COALESCE(#{level}, 1),  <!-- 如果 level 为空，则设置为 1 -->
        <if test="warnNum != null and warnNum != 0">#{warnNum},</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(),
        YEAR(CURDATE())
        )
    </insert>
    <update id="updateById">
        update ma_part_type
        set pa_name = #{paName},
            num = #{num},
            unit_id = #{unitId},
            buy_price = #{buyPrice},
            update_by = #{updateBy},
            update_time = now(),
            remark = #{remark},
            company_id = #{companyId}
        where pa_id = #{paId}
    </update>

    <delete id="deletePaById">
        delete from ma_part_type where pa_id = #{paId}
    </delete>

    <select id="checkPartNameUnique" resultMap="MaPartTypeResult">
        <include refid="selectMaPartTypeVo"/>
        where pa_name = #{paName} limit 1
    </select>

    <select id="selectMaPartType" parameterType="com.bonus.sgzb.base.domain.MaPartType" resultMap="MaPartTypeResult">
        SELECT
        t1.pa_id,
        t1.pa_name,
        t1.parent_id,
        t1.status,
        t1.num,
        t1.unit_id,
        t1.buy_price,
        t1.level,
        t1.warn_num,
        t1.del_flag,
        t1.create_by,
        t1.create_time,
        t1.remark,
        t1.company_id,
        t1.year
        FROM
        ma_part_type t1
        LEFT JOIN
        ma_part_type t2 ON t1.parent_id = t2.pa_id
        LEFT JOIN
        ma_part_type t3 ON t2.parent_id = t3.pa_id
        WHERE
        t1.del_flag = '0'
        <if test="paName != null and paName != ''">
            AND (t1.pa_name LIKE CONCAT('%', #{paName}, '%')
            OR t2.pa_name LIKE CONCAT('%', #{paName}, '%')
            OR t3.pa_name LIKE CONCAT('%', #{paName}, '%'))
        </if>
        <if test="year != null and year != ''">
            AND t1.year = #{year}
        </if>


    </select>

    <select id="hasChildByPaId" resultType="java.lang.Integer">
        select count(1) from ma_part_type
        where del_flag = '0' and parent_id = #{paId} limit 1
    </select>
    <select id="getById" resultType="com.bonus.sgzb.base.domain.MaPartType">
        select pa_id, pa_name, parent_id, status, num, unit_id, buy_price, level, warn_num, del_flag, create_by, create_time, remark, company_id
        from ma_part_type
        where pa_id = #{paId}
    </select>
    <select id="checkPartName" resultType="java.lang.Integer">
        select count(*) from ma_part_type where pa_name = #{paName}
    </select>
    <select id="selectPartName" resultType="com.bonus.sgzb.base.domain.MaPartType">
        select pa_id, pa_name, parent_id, status, num, unit_id, buy_price, level, warn_num, del_flag, create_by, create_time, remark, company_id
        from ma_part_type
        where pa_name = #{paName}
    </select>

</mapper>
