<?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.ruoyi.project.business.mapper.TcProjectEquipmentMapper">
    
    <resultMap type="com.ruoyi.project.business.domain.TcProjectEquipment" id="TcProjectEquipmentResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="equipmentQuantity"    column="equipment_quantity"    />
        <result property="equipmentType"    column="equipment_type"    />
        <result property="elevatorModel"    column="elevator_model"    />
        <result property="amount"    column="amount"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
        <result property="quotationNo"    column="quotation_no"    />
        <result property="version"    column="version"    />
        <result property="origin"    column="origin"    />

    </resultMap>

    <sql id="selectTcProjectEquipmentVo">
        select id, project_id, equipment_quantity, equipment_type, elevator_model, amount, del_flag, create_time, create_by, update_time, update_by, remark from tc_project_equipment
    </sql>

    <select id="selectTcProjectEquipmentList" parameterType="com.ruoyi.project.business.domain.vo.project.ProjectEquipmentReq" >
        <include refid="selectTcProjectEquipmentVo"/>
        <where>
            del_flag = '0'
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="equipmentQuantity != null "> and equipment_quantity = #{equipmentQuantity}</if>
            <if test="equipmentType != null  and equipmentType != ''"> and equipment_type = #{equipmentType}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="removeIds != null and removeIds.size() > 0">
                and id not in
                <foreach collection="removeIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
    
    <select id="selectTcProjectEquipmentById" parameterType="Long" resultMap="TcProjectEquipmentResult">
        <include refid="selectTcProjectEquipmentVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTcProjectEquipment" parameterType="com.ruoyi.project.business.domain.TcProjectEquipment">
        insert into tc_project_equipment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="equipmentQuantity != null">equipment_quantity,</if>
            <if test="equipmentType != null">equipment_type,</if>
            <if test="amount != null">amount,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="equipmentQuantity != null">#{equipmentQuantity},</if>
            <if test="equipmentType != null">#{equipmentType},</if>
            <if test="amount != null">#{amount},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTcProjectEquipment" parameterType="com.ruoyi.project.business.domain.TcProjectEquipment">
        update tc_project_equipment
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="equipmentQuantity != null">equipment_quantity = #{equipmentQuantity},</if>
            <if test="equipmentType != null">equipment_type = #{equipmentType},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTcProjectEquipmentById" parameterType="Long">
        delete from tc_project_equipment where id = #{id}
    </delete>

    <delete id="deleteTcProjectEquipmentByIds" parameterType="String">
        delete from tc_project_equipment where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="InsertBySQL" parameterType="java.util.List"   useGeneratedKeys="true"
            keyProperty="id">
        insert into tc_project_equipment (
         project_id,
         equipment_quantity,
         equipment_type,
         amount,
         create_time, create_by,
         update_time, update_by,
         remark
        )values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.projectId},
            #{item.equipmentQuantity},
            #{item.equipmentType},
            #{item.amount},

            #{item.createTime},
            #{item.createBy},
            #{item.updateTime},
            #{item.updateBy},

            #{item.remark}
            )
        </foreach>
    </insert>

    <update id="deleteTcProjectEquipmentByProjectId" parameterType="Long">
        update tc_project_equipment set  del_flag = '1' where project_id = #{projectId}
    </update>

    <select id="selectTcProjectEquipmentListProject" parameterType="com.ruoyi.project.business.domain.vo.project.ProjectEquipmentReq"
            resultMap="TcProjectEquipmentResult">
        <!--SELECT-->
        <!--quo.id,-->
        <!--project.project_id,-->
        <!--ts.elevator_number AS equipment_quantity,-->
        <!--elv.elevator_type AS equipment_type,-->
        <!--elv.elevator_model,-->
        <!--IFNULL( ts.final_price, ts.price ) AS amount,-->
        <!--quo.quotation_no,-->
        <!--quo.version,-->
        <!--'quotation' as origin,-->
        <!--ts.specification as standard-->
        <!--FROM-->
        <!--tq_quotation quo-->
        <!--LEFT JOIN tc_project project ON quo.project_id = project.project_id-->
        <!--AND project.del_flag = '0'-->
        <!--LEFT JOIN tq_specification ts ON ts.quotation_id = quo.id-->
        <!--AND ts.del_flag = '0'-->
        <!--LEFT JOIN tp_elevator_model elv ON elv.id = ts.elevator_model_id-->
        <!--AND elv.del_flag = '0'-->
        <!--INNER JOIN ( SELECT MAX( id ) AS id FROM tq_quotation-->
        <!--<where>-->
            <!--del_flag = '0'-->
            <!--<if test="projectId != null "> and project_id = #{projectId}</if>-->
        <!--</where>-->
        <!--GROUP BY quotation_no) t2 on t2.id = quo.id-->
        <!--<where>-->
            <!--quo.del_flag = '0'-->
            <!--<if test="projectId != null "> and quo.project_id = #{projectId}</if>-->
        <!--</where>-->
        SELECT
        main.id,
        main.project_id,
        main.quotation_no,
        main.version,
        main.origin,
        ts.elevator_number AS equipment_quantity,
        elv.elevator_type AS equipment_type,
        elv.elevator_model,
        case
        WHEN main.quotation_status &lt; 6 THEN	 0
        ELSE IFNULL( ts.final_price, ts.price )
        END AS amount,
        ts.specification AS standard
        FROM
        (
        SELECT DISTINCT
        quo.id,
        project.project_id,
        quo.quotation_no,
        quo.version,
        'quotation' AS origin,
        quo.quotation_status
        FROM
        tq_quotation quo
        LEFT JOIN tc_project project ON quo.project_id = project.project_id
        AND project.del_flag = '0'
        <where>
            quo.del_flag = '0'
            <if test="projectId != null "> and quo.project_id = #{projectId}</if>
        </where>
        ORDER BY
        FIELD( quo.quotation_status, '9' ) DESC,
        id DESC
        ) main
        LEFT JOIN tq_specification ts ON ts.quotation_id = main.id
        AND ts.del_flag = '0'
        LEFT JOIN tp_elevator_model elv ON elv.id = ts.elevator_model_id
        AND elv.del_flag = '0'
        GROUP BY main.quotation_no
        union
        SELECT
        id,
        project_id,
        '' AS quotation_no,
        '' as version,
        'project' as origin,
        equipment_quantity,
        equipment_type,
        elevator_model,
        amount,
        standard
        FROM
        tc_project_equipment
        <where>
        del_flag = '0'
        <if test="projectId != null "> and project_id = #{projectId}</if>
        </where>


    </select>
    <select id="selectTotalProject" parameterType="com.ruoyi.project.business.domain.vo.project.ProjectEquipmentReq"
            resultType="map">
        select
        SUM(IFNULL(equipment_quantity,0)) as totalCount,
        SUM(IFNULL(equipment_quantity,0) * IFNULL(amount,0)) as totalAmount
        from(
            <!--SELECT-->
            <!--ts.elevator_number AS equipment_quantity,-->
            <!--IFNULL( ts.final_price, ts.price ) AS amount,-->
            <!--quo.quotation_no-->
            <!--FROM-->
            <!--tq_quotation quo-->
            <!--LEFT JOIN tc_project project ON quo.project_id = project.project_id-->
            <!--AND project.del_flag = '0'-->
            <!--LEFT JOIN tq_specification ts ON ts.quotation_id = quo.id-->
            <!--AND ts.del_flag = '0'-->
            <!--INNER JOIN ( SELECT MAX( id ) AS id FROM tq_quotation-->
            <!--<where>-->
                <!--del_flag = '0'-->
                <!--<if test="projectId != null "> and project_id = #{projectId}</if>-->
            <!--</where>-->
            <!--GROUP BY quotation_no) t2 on t2.id = quo.id-->
            <!--<where>-->
                <!--quo.del_flag = '0'-->
                <!--<if test="projectId != null "> and quo.project_id = #{projectId}</if>-->
            <!--</where>-->



            SELECT
            main.quotation_no,
            ts.elevator_number AS equipment_quantity,
            case
            WHEN main.quotation_status &lt; 6 THEN	 0
            ELSE IFNULL( ts.final_price, ts.price )
            END
             AS amount
            FROM
            (
            SELECT DISTINCT
            quo.id,
            quo.quotation_no,
            quo.quotation_status
            FROM
            tq_quotation quo
            LEFT JOIN tc_project project ON quo.project_id = project.project_id
            AND project.del_flag = '0'
            <where>
                quo.del_flag = '0'
                <if test="projectId != null "> and quo.project_id = #{projectId}</if>
            </where>
            ORDER BY
            FIELD( quo.quotation_status, '9' ) DESC,
            id DESC
            ) main
            LEFT JOIN tq_specification ts ON ts.quotation_id = main.id
            AND ts.del_flag = '0'
            GROUP BY
            main.quotation_no
            union
            SELECT
            '' AS quotation_no,
            equipment_quantity,
            amount
            FROM
            tc_project_equipment
            <where>
                del_flag = '0'
                <if test="projectId != null "> and project_id = #{projectId}</if>
            </where>
        ) total
    </select>
</mapper>