<?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.CbSummaryMapper">

    <resultMap type="com.dsk.cscec.domain.CbSummary" id="CbSummaryMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="parentId" column="parent_id" jdbcType="INTEGER"/>
        <result property="level" column="level" jdbcType="INTEGER"/>
        <result property="sort" column="sort" jdbcType="INTEGER"/>
        <result property="projectId" column="project_id" jdbcType="INTEGER"/>
        <result property="cbStage" column="cb_stage" jdbcType="INTEGER"/>
        <result property="cbProjectFileId" column="cb_project_file_id" jdbcType="INTEGER"/>
        <result property="number" column="number" jdbcType="VARCHAR"/>
        <result property="cbName" column="cb_name" jdbcType="VARCHAR"/>
        <result property="taxExclusiveTotal" column="tax_exclusive_total" jdbcType="VARCHAR"/>
        <result property="cbTaxesTotal" column="cb_taxes_total" jdbcType="VARCHAR"/>
        <result property="taxInclusiveTotal" column="tax_inclusive_total" jdbcType="VARCHAR"/>
        <result property="cbProportion" column="cb_proportion" jdbcType="VARCHAR"/>
        <result property="taxInclusivePmTarget" column="tax_inclusive_pm_target" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
        <result property="cbType" column="cb_type" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="baseColumn">
        csu.id, csu.parent_id, csu.level, csu.sort, csu.project_id, csu.cb_stage, csu.cb_project_file_id, csu.number, csu.cb_name, csu.tax_exclusive_total,
        csu.cb_taxes_total, csu.tax_inclusive_total, csu.cb_proportion, csu.tax_inclusive_pm_target, csu.remark, csu.create_time, csu.del_flag,
        csu.cb_type
    </sql>

    <select id="getByParentId" resultType="com.dsk.cscec.domain.vo.CbSummaryActualListVo">
        select csu.*,csa.id actualId,csa.tax_inclusive_expense,csa.tax_exclusive_expense,csa.expense_date,
        ifnull(sum(csa1.tax_inclusive_expense),0) taxInclusiveExpenseTotal,ifnull(sum(csa1.tax_exclusive_expense),0) taxExclusiveExpenseTotal
        from cb_summary csu
        left join cb_summary_actual csa on csu.id = csa.cb_summary_id and csa.expense_date = #{expenseDate} AND csa.del_flag = 0
        left join cb_summary_actual csa1 on csu.id = csa1.cb_summary_id and csa1.expense_date &lt;= #{expenseDate} AND csa.del_flag = 0
        <where>
            csu.parent_id = #{parentId}
            and csu.del_flag = 0
        </where>
        group by csu.id
        order by csu.sort
    </select>

    <select id="getById" resultType="com.dsk.cscec.domain.vo.CbSummaryActualListVo">
        select csu.*,csa.id actualId,csa.tax_inclusive_expense,csa.tax_exclusive_expense,csa.expense_date,
        ifnull(sum(csa1.tax_inclusive_expense),0) taxInclusiveExpenseTotal,ifnull(sum(csa1.tax_exclusive_expense),0) taxExclusiveExpenseTotal
        from cb_summary csu
        left join cb_summary_actual csa on csu.id = csa.cb_summary_id and csa.expense_date = #{expenseDate} AND csa.del_flag = 0
        left join cb_summary_actual csa1 on csu.id = csa1.cb_summary_id and csa1.expense_date &lt;= #{expenseDate} AND csa.del_flag = 0
        <where>
            csu.id = #{id}
            and csu.del_flag = 0
        </where>
    </select>

    <select id="getByIds" resultType="com.dsk.cscec.domain.vo.CbSummaryActualListVo">
        select csu.*,csa.id actualId,csa.tax_inclusive_expense,csa.tax_exclusive_expense,csa.expense_date
        from cb_summary csu
        left join cb_summary_actual csa on csu.id = csa.cb_summary_id
        <where>
            csa.expense_date = #{expenseDate}
            and csa.del_flag = 0
            and csu.del_flag = 0
            and csu.id in
            <foreach collection="entities" item="entity" separator="," open="(" close=")">
                #{entity.id}
            </foreach>
        </where>
    </select>

    <select id="getProjectAll" resultType="com.dsk.cscec.domain.vo.CbSummaryActualListVo">
        select
            <include refid="baseColumn"></include>,
            csa.id actualId,csa.tax_inclusive_expense,csa.tax_exclusive_expense
        from cb_summary csu
        left join cb_summary_actual csa on csu.id = csa.cb_summary_id and csa.expense_date = #{bo.expenseDate} AND csa.del_flag = 0
        <where>
            csu.project_id = #{bo.projectId}
            <if test="bo.cbStage != null">
                and csu.cb_stage = #{bo.cbStage}
            </if>
            and csu.cb_type = #{bo.cbType}
            and csu.del_flag = 0
            and csu.level != 0
        </where>
        order by csu.sort
    </select>

    <select id="getCostAccountAll" resultType="com.dsk.cscec.domain.vo.CbSummaryActualListVo">
        select
        <include refid="baseColumn"></include>,
        csa.id actualId,csa.tax_inclusive_expense,csa.tax_exclusive_expense
        from cb_summary csu
        left join cb_summary_actual csa on csu.id = csa.cb_summary_id and csa.expense_date = #{bo.expenseDate} AND csa.del_flag = 0
        <where>
            csu.project_id = #{bo.projectId}
            <if test="bo.cbStage != null">
                and csu.cb_stage = #{bo.cbStage}
            </if>
            and csu.cb_type = #{bo.cbType}
            and csu.del_flag = 0
        </where>
        order by csu.sort
    </select>

    <select id="getExpenseDateList" resultType="map">
        select csa.expense_date expenseDate,
            if(csal.id is null,0,1) as isLock
        from cb_summary_actual csa
        left join cb_summary csu on csu.id = csa.cb_summary_id
        left join cb_summary_actual_lock csal on csal.project_id = csu.project_id and csal.expense_date = csa.expense_date and csal.del_flag = 0
        where
          csu.project_id = #{bo.projectId}
          and csu.cb_type = #{bo.cbType}
          and csu.del_flag = 0
          and csa.del_flag = 0
        group by csa.expense_date
        order by csa.expense_date desc
    </select>

    <select id="selectByLevel" resultType="map">
        select csu.id,csu.cb_name as cbName
        from cb_summary csu
        where
            csu.del_flag = 0
            and csu.project_id = #{bo.projectId}

            and csu.cb_type = #{bo.cbType}
            and csu.level = #{bo.level}
        order by csu.sort
    </select>

    <select id="getGainLossAnalysisById" resultType="com.dsk.cscec.domain.vo.CbGainLossAnalysisListVo">
        select csu.id, csu.cb_name, csu.tax_exclusive_total, csu.cb_taxes_total, csu.tax_inclusive_total, csa.tax_inclusive_expense, csa.tax_exclusive_expense,
--             sum(cde.tender_control_sum_price) tenderSumPrice, sum(cde.exclude_tax_cb_sum_price) taxExcludeTenderSumPrice, sum(cde.include_tax_cb_sum_price) taxIncludeTenderSumPrice,
            sum(csa1.tax_inclusive_expense) sumTaxInclusiveExpense, sum(csa1.tax_exclusive_expense) sumTaxExclusiveExpense
        from cb_summary csu
        left join cb_summary_actual csa on (csu.id = csa.cb_summary_id and csa.expense_date = #{expenseDate}  AND csa.del_flag = 0)
        left join cb_summary_actual csa1 on (csu.id = csa1.cb_summary_id and csa1.expense_date &lt;= #{expenseDate}  AND csa1.del_flag = 0)
--         left join cb_direct_expense cde on (csu.project_id = cde.project_id and cde.del_flag = 0)
        where csu.id = #{id} and csu.del_flag = 0
        group by csu.id
        order by csu.sort
    </select>

    <select id="getGainLossAnalysisByParentId" resultType="com.dsk.cscec.domain.vo.CbGainLossAnalysisListVo">
        select csu.id, csu.cb_name, csu.tax_exclusive_total, csu.cb_taxes_total, csu.tax_inclusive_total, csa.tax_inclusive_expense, csa.tax_exclusive_expense,
--             sum(cde.tender_control_sum_price) tenderSumPrice, sum(cde.exclude_tax_cb_sum_price) taxExcludeTenderSumPrice, sum(cde.include_tax_cb_sum_price) taxIncludeTenderSumPrice,
            sum(csa1.tax_inclusive_expense) sumTaxInclusiveExpense, sum(csa1.tax_exclusive_expense) sumTaxExclusiveExpense
        from cb_summary csu
        left join cb_summary_actual csa on (csu.id = csa.cb_summary_id and csa.expense_date = #{expenseDate}  AND csa.del_flag = 0)
        left join cb_summary_actual csa1 on (csu.id = csa1.cb_summary_id and csa1.expense_date &lt;= #{expenseDate}  AND csa1.del_flag = 0)
--         left join cb_direct_expense cde on (csu.project_id = cde.project_id  and cde.del_flag = 0)
        where csu.parent_id = #{parentId} and csu.del_flag = 0
        group by csu.id
        order by csu.sort
    </select>

</mapper>

