<?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.material.mapper.MaWholeSetMapper">
    <insert id="insertPlanNeedInfo" useGeneratedKeys="true" keyProperty="planId">
        insert into plan_need_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unitId != null">unit_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unitId != null">#{unitId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            now()
        </trim>
    </insert>

    <insert id="insertPlanBorrow" useGeneratedKeys="true" keyProperty="borrowId">
        insert into plan_borrow_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="needUnitId != null">need_unit_id,</if>
            <if test="borrowUnitId != null">borrow_unit_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="needUnitId != null">#{needUnitId},</if>
            <if test="borrowUnitId != null">#{borrowUnitId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            now()
        </trim>
    </insert>

    <insert id="insert">
        <foreach item="item" index="index" collection="wholeList" separator=";">
            insert into ma_whole_set
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.id != null">type_id,</if>
                <if test="item.parentId != null">parent_id,</if>
                <if test="item.totalNum != null">part_num,</if>
                status
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="item.id != null">#{item.id},</if>
                <if test="item.parentId != null">#{item.parentId},</if>
                <if test="item.totalNum != null">#{item.totalNum},</if>
                1
            </trim>
        </foreach>
    </insert>
    <delete id="deleteById" parameterType="java.lang.Integer">
        delete from ma_whole_set
        where parent_id = #{id}
    </delete>

    <select id="selectByParentId" resultType="java.lang.Integer">
        select COUNT(*) from ma_whole_set
        where parent_id = #{parentId}
    </select>
    <select id="selectList" resultType="com.bonus.sgzb.material.domain.MaWholeVo">
        SELECT
            mws.parent_id AS id,
            mt2.type_name AS typeName,
            mt.type_name AS typeModelName,
            SUM(mws.part_num) AS totalNum
        FROM ma_whole_set mws
        JOIN ma_type mt ON mws.parent_id = mt.type_id
        JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
        mt.`status` = '0'
        AND mt.del_flag = '0'
        <if test="keyWord != null  and keyWord != ''">
            and (
            mt2.type_name like  concat('%', #{keyWord}, '%') or
            mt.type_name like  concat('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY mws.parent_id
    </select>
    <select id="selectListById" resultType="com.bonus.sgzb.material.domain.MaWholeVo">
        SELECT
            mws.parent_id AS id,
            mt2.type_name AS typeName,
            mt.type_name AS typeModelName,
            mws.part_num AS totalNum
        FROM
            ma_whole_set mws
            JOIN ma_type mt ON mws.type_id = mt.type_id
            JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
            JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
            JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
        mt.`status` = '0'
        AND mt.del_flag = '0'
        <if test="id != null  and id != ''">
             and mws.parent_id = #{id}
        </if>
        <if test="keyWord != null  and keyWord != ''">
            and (
            mt2.type_name like  concat('%', #{keyWord}, '%') or
            mt.type_name like  concat('%', #{keyWord}, '%')
          )
        </if>
    </select>

    <select id="selectTypeTree" resultType="com.bonus.sgzb.material.domain.TreeSelectId">
        SELECT
          mt4.type_id AS firstId,
          mt3.type_id AS secondId,
          mt2.type_id AS threeId,
          mt.type_id AS fourId
        FROM
          ma_type mt
        JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
          mt.`status` = '0'
          AND mt.del_flag = '0'
          <if test="id != null and id !=''">
            AND mt.type_id = #{id}
          </if>
    </select>
    <select id="selectTypeId" resultType="java.lang.Integer">
        select type_id from ma_whole_set
        where parent_id = #{id}
    </select>
    <select id="select" resultType="com.bonus.sgzb.material.domain.MaWholeVo">
        SELECT
          mt.type_id AS id,
          mt2.type_name AS typeName,
          mt.type_name AS typeModelName,
          mws.part_num AS totalNum
        FROM
          ma_whole_set mws
        JOIN ma_type mt ON mws.type_id = mt.type_id
        JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
        mt.`status` = '0'
        AND mt.del_flag = '0'
          <if test="id != null  and id != ''">
             and  mws.parent_id = #{id}
          </if>
    </select>
    <select id="selectId" resultType="com.bonus.sgzb.material.domain.TreeSelectId">
        SELECT
          mt4.type_id AS firstId,
          mt3.type_id AS secondId,
          mt2.type_id AS threeId,
          mt.type_id AS fourId
        FROM
          ma_type mt
        JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
          mt.`status` = '0'
          AND mt.del_flag = '0'
          AND mt.type_id in
          <foreach item="item" collection="typeId" open="(" separator="," close=")">
            #{item}
          </foreach>
    </select>

</mapper>