<?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.PlanManagementMapper">
    <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="insertPlanNeedDetail">
        insert into plan_need_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="purchaseNum != null">plan_num,</if>
            <if test="purchasePrice != null">plan_price,</if>
            <if test="totalPrice != null">plan_cost,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="purchaseNum != null">#{purchaseNum},</if>
            <if test="purchasePrice != null">#{purchasePrice},</if>
            <if test="totalPrice != null">#{totalPrice},</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="insertPlanBorrowDetail">
        <foreach item="item" index="index" collection="checkDetailsList" separator=";">
            insert into plan_borrow_details
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.planId != null">plan_id,</if>
                <if test="item.typeId != null">type_id,</if>
                <if test="item.purchaseNum != null">plan_num,</if>
                <if test="item.borrowNum != null">borrow_num,</if>
                create_time,
                <if test="item.remark != null">remark,</if>
                <if test="item.companyId != null">company_id,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="item.planId != null">#{item.planId},</if>
                <if test="item.typeId != null">#{item.typeId},</if>
                <if test="item.purchaseNum != null">#{item.purchaseNum},</if>
                <if test="item.borrowNum != null">#{item.borrowNum},</if>
                now(),
                <if test="item.remark != null">#{item.remark},</if>
                <if test="item.companyId != null">#{item.companyId},</if>
            </trim>
        </foreach>
    </insert>
    <select id="selectPlanInfo" resultType="com.bonus.sgzb.material.domain.PlanVO">
        SELECT
               pn.plan_id     AS planId,
               bu.unit_name   AS lenderCompany,
               bu.unit_id AS unitId,
               pnd.plan_num   AS lenderOrigPlanNum,
               pnd.plan_price AS planPrice,
               pnd.plan_cost  AS planCost,
               pnd.type_id AS typeId,
               pnd.company_id AS companyId,
               pn.create_by   AS createBy,
               pn.create_time AS createTime,
               pn.remark      AS remark,
               mt2.type_name  AS typeName,
               mt.type_name   AS typeModelName,
               mt.unit_name   AS unitName
        FROM
             plan_need_info pn
                 LEFT JOIN plan_need_details pnd ON pn.plan_id = pnd.plan_id
                 LEFT JOIN bm_unit_info bu ON pn.unit_id = bu.unit_id
                 LEFT JOIN ma_type mt ON pnd.type_id = mt.type_id
                 LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                 LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
                 LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
        1=1
        <if test="name != null  and name != ''">
            and (bu.unit_name like concat('%', #{name}, '%') or
                 mt2.type_name like concat('%',#{name},'%') or
                 mt.type_name like concat('%',#{name},'%'))
        </if>
        <if test="borrowUnitId != null  and borrowUnitId != ''">
            and bu.unit_id = #{borrowUnitId}
        </if>
    </select>
    <select id="selectBorrow" resultType="com.bonus.sgzb.material.domain.PlanManagementVO">
        SELECT
        pnd.id AS id,
        pb.borrow_id   AS borrowId,
        bu.unit_name   AS borrowerCompany,
        pnd.plan_num   AS borrowerOrigPlanNum,
        pnd.borrow_num AS borrowNum,
        (pnd.plan_num - pnd.borrow_num) AS borrowerNowPlanNum,
        aa.lenderCompany AS  lenderCompany,
        aa.lenderOrigPlanNum AS lenderOrigPlanNum,
        (aa.lenderOrigPlanNum + pnd.borrow_num) AS lenderNowPlanNum,
        CASE
        WHEN COALESCE(pb.update_by, '') = '' THEN pb.create_by
        ELSE pb.update_by
        END AS createBy,
        COALESCE(pb.update_time, pb.create_time) AS createTime,
        pb.remark      AS remark,
        mt2.type_name  AS typeName,
        mt.type_name   AS typeModelName,
        mt.unit_name   AS unitName
        FROM
             ( SELECT
        bu.unit_name AS lenderCompany,
        pnd.plan_num AS lenderOrigPlanNum,
        pbd.plan_id AS planId
        FROM
        plan_borrow_details pbd
        LEFT JOIN plan_borrow_info pb ON pb.borrow_id = pbd.plan_id
        LEFT JOIN bm_unit_info bu ON pb.borrow_unit_id = bu.unit_id
        LEFT JOIN plan_need_info pn ON pb.need_unit_id = pn.unit_id
        LEFT JOIN plan_need_details pnd ON pn.plan_id = pnd.plan_id ) AS aa
        LEFT JOIN plan_borrow_details pnd ON pnd.plan_id = aa.planId
        LEFT JOIN plan_borrow_info pb ON pb.borrow_id = pnd.plan_id
        LEFT JOIN bm_unit_info bu ON pb.need_unit_id = bu.unit_id
        LEFT JOIN ma_type mt ON pnd.type_id = mt.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
        1=1
        <if test="name != null  and name != ''">
            and (
            aa.lenderCompany like concat('%', #{name}, '%') or
            bu.unit_name like concat('%', #{name}, '%') or
            mt2.type_name like concat('%',#{name},'%') or
            mt.type_name like concat('%',#{name},'%'))
        </if>
        GROUP BY pnd.id,aa.lenderCompany,aa.lenderOrigPlanNum
    </select>

</mapper>