<?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.dsk.cscec.mapper.CbQuantitySummaryMapper">
    <insert id="batchInsert" parameterType="com.dsk.cscec.domain.CbQuantitySummary">
        INSERT INTO cscec_bms.cb_quantity_summary
        (project_id, `number`, cb_stage, cb_subject_name, cb_subject_no, company_no, org_no, cb_name, job_content,
        calculation_rule, unit, material_description, guide_price, bid_unit_price, unit_price_difference, quantity,
        combined_price, combined_price_tax, brand_name, bid_source, remark, cb_project_file_id)
        VALUES
        <foreach collection="list" item="item" separator="," >
            (#{item.projectId}, #{item.number}, #{item.cbStage}, #{item.cbSubjectName}, #{item.cbSubjectNo}, #{item.companyNo}, #{item.orgNo}, #{item.cbName},
            #{item.jobContent},#{item.calculationRule},#{item.unit},#{item.materialDescription},#{item.guidePrice},
            #{item.bidUnitPrice},#{item.unitPriceDifference},#{item.quantity},#{item.combinedPrice},#{item.combinedPriceTax},
            #{item.brandName},#{item.bidSource},#{item.remark},#{item.cbProjectFileId})
        </foreach>
     </insert>

    <select id="selectSubject" resultType="java.util.Map">
        select
            cs1.cb_subject_name as one, cs2.cb_subject_name as two, cs3.cb_subject_name as three , '' as four
        from cb_subject cs1
        join cb_subject cs2 on (cs2.cb_subject_no like concat(cs1.cb_subject_no,'%') and cs2.`level` = 2 )
        join cb_subject cs3 on (cs3.cb_subject_no like concat(cs2.cb_subject_no,'%') and cs3.`level` = 3 )
        join cb_quantity_summary cqs on (cqs.cb_subject_name = cs3.cb_subject_name and cqs.del_falg = 0 and cqs.project_id = #{projectId} and cqs.cb_stage = #{cbStage} )
        where cs1.`level` = 1 and cs1.cb_subject_no != 'CL'
        group by cs1.cb_subject_name,cs2.cb_subject_name,cs3.cb_subject_name

            union all

        select
            cs1.cb_subject_name as one, cs2.cb_subject_name as two, '' as three, '' as four
        from cb_subject cs1
        join cb_subject cs2 on (cs2.cb_subject_no like concat(cs1.cb_subject_no,'%') and cs2.`level` = 2 )
        join cb_quantity_summary cqs on (cqs.cb_subject_no = cs2.cb_subject_no and cqs.del_falg = 0 and cqs.project_id = #{projectId} and cqs.cb_stage = #{cbStage} )
        where cs1.`level` = 1 and cs1.cb_subject_no = 'CL'
        group by cs1.cb_subject_name,cs2.cb_subject_name

			union all

		select
            cs1.cb_subject_name as one, cs2.cb_subject_name as two, cs3.cb_subject_name as three, '' as four
        from cb_subject cs1
        join cb_subject cs2 on (cs2.cb_subject_no like concat(cs1.cb_subject_no,'%') and cs2.`level` = 2 )
        join cb_subject cs3 on (cs3.cb_subject_no like concat(cs2.cb_subject_no,'%') and cs3.`level` = 3 )
        join cb_quantity_summary cqs on (cqs.cb_subject_no = cs3.cb_subject_no and cqs.del_falg = 0 and cqs.project_id = #{projectId} and cqs.cb_stage = #{cbStage} )
        where cs1.`level` = 1 and cs1.cb_subject_no = 'CL'
        group by cs1.cb_subject_name,cs2.cb_subject_name,cs3.cb_subject_name

				union all

		select
            cs1.cb_subject_name as one, cs2.cb_subject_name as two, cs3.cb_subject_name as three, cs4.cb_subject_name as four
        from cb_subject cs1
        join cb_subject cs2 on (cs2.cb_subject_no like concat(cs1.cb_subject_no,'%') and cs2.`level` = 2 )
        join cb_subject cs3 on (cs3.cb_subject_no like concat(cs2.cb_subject_no,'%') and cs3.`level` = 3 )
        join cb_subject cs4 on (cs4.cb_subject_no like concat(cs3.cb_subject_no,'%') and cs4.`level` = 4 )
        join cb_quantity_summary cqs on (cqs.cb_subject_no = cs4.cb_subject_no and cqs.del_falg = 0 and cqs.project_id = #{projectId} and cqs.cb_stage = #{cbStage} )
        where cs1.`level` = 1 and cs1.cb_subject_no = 'CL'
        group by cs1.cb_subject_name,cs2.cb_subject_name,cs3.cb_subject_name,cs4.cb_subject_name
    </select>

    <select id="selectOtherSubjectCount" resultType="java.lang.Integer">
        select
            count(cqs.id)
        from cb_quantity_summary cqs
        left join cb_subject cs1 on cqs.cb_subject_name = cs1.cb_subject_name
        where cqs.del_falg = 0 and cqs.project_id = #{projectId} and cqs.cb_stage = #{cbStage} and cs1.id is null
    </select>

    <select id="selectMonthList" resultType="java.lang.String">
        select
            cqsa.record_date
        from cb_quantity_summary cqs
        join cb_quantity_summary_actual cqsa on cqs.id = cqsa.cb_quantity_summary_id
        left join cb_subject cs1 on cqs.cb_subject_name = cs1.cb_subject_name
        where cqs.del_falg = 0 and cqs.project_id = #{projectId} and cqs.cb_stage = #{cbStage}
        <if test="cbSubjectName != null and cbSubjectName !=''">
            <choose>
                <when test="cbSubjectName == '未归类项目'"> and cs1.id is null </when>
                <otherwise>  and cqs.cb_subject_name = #{cbSubjectName} </otherwise>
            </choose>
        </if>
        group by cqsa.record_date
        order by cqsa.record_date
    </select>

    <select id="selectListBySubject"  resultType="com.dsk.cscec.domain.vo.CbQuantitySummaryListVo">
        select
            cqs.id, cqs.cb_subject_name, cqs.company_no, cqs.org_no, cqs.cb_name, cqs.job_content, cqs.calculation_rule,
            cqs.unit, cqs.material_description, cqs.guide_price, cqs.bid_unit_price, cqs.unit_price_difference, cqs.quantity,
            cqs.combined_price, cqs.combined_price_tax, cqs.brand_name, cqs.bid_source, cqs.remark, cqs.`number`, cqsa.quantities,
            cqsa.quantities_unit, cqsa.conversion_quantities, cqsa.conversion_unit, cqsa.purchase_unit_price, cqsa.create_time,
            cqsa.id actualId, cqsa.ipm_project_code, cqsa.ipm_contract_code, cqsa.ipm_biz_code, cqsa.push_quantities,
            ifnull(sum(cqsa1.quantities),0) totalQuantities,ifnull(sum(cqsa1.push_quantities),0) cumulativeDifference
        from cb_quantity_summary cqs
        left join cb_quantity_summary_actual cqsa on (cqs.id = cqsa.cb_quantity_summary_id and cqsa.record_date = #{recordDate} )
        left join cb_quantity_summary_actual cqsa1 on (cqs.id = cqsa1.cb_quantity_summary_id and cqsa1.record_date &lt;= #{recordDate} )
        left join cb_subject cs1 on cqs.cb_subject_name = cs1.cb_subject_name
        where cqs.del_falg = 0 and cqs.project_id = #{projectId} and cqs.cb_stage = #{cbStage}
        <if test="cbSubjectName != null and cbSubjectName !=''">
            <choose>
                <when test="cbSubjectName == '未归类项目'"> and cs1.id is null </when>
                <otherwise> and cs1.id is not null and cqs.cb_subject_name = #{cbSubjectName} </otherwise>
            </choose>
        </if>
        group by cqs.id
        order by cs1.sort asc, cs1.cb_subject_no asc, cqs.`number` asc
    </select>

    <select id="unconvertedList"  resultType="com.dsk.cscec.domain.vo.CbQuantitySummaryListVo">
        select
        cqs.id, cqs.cb_subject_name, cqs.company_no, cqs.org_no, cqs.cb_name, cqs.job_content, cqs.calculation_rule,
        cqs.unit, cqs.material_description, cqs.guide_price, cqs.bid_unit_price, cqs.unit_price_difference, cqs.quantity,
        cqs.combined_price, cqs.combined_price_tax, cqs.brand_name, cqs.bid_source, cqs.remark, cqs.`number`, cqsa.quantities,
        cqsa.quantities_unit, cqsa.conversion_quantities, cqsa.conversion_unit, cqsa.purchase_unit_price, cqsa.create_time,
        cqsa.id actualId, cqsa.ipm_project_code, cqsa.ipm_contract_code, cqsa.ipm_biz_code, cqsa.push_quantities
        from cb_quantity_summary cqs
        left join cb_quantity_summary_actual cqsa on cqs.id = cqsa.cb_quantity_summary_id
        where cqs.del_falg = 0
        and cqsa.quantities is not null
        and (cqsa.conversion_unit is null or cqsa.conversion_unit = '')
        and cqs.project_id = #{projectId}
        and cqs.cb_stage = #{cbStage}
        and cqsa.record_date = #{recordDate}
        and cqs.cb_subject_name = #{cbSubjectName}
    </select>
</mapper>