<?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.kcim.dao.mapper.AllocationMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kcim.dao.model.Allocation" id="allocationMap">
        <result property="id" column="id"/>
        <result property="dateYear" column="date_year"/>
        <result property="dateMonth" column="date_month"/>
        <result property="levelSort" column="level_sort"/>
        <result property="levelName" column="level_name"/>
        <result property="hospId" column="hosp_id"/>
        <result property="responsibilityCode" column="responsibility_code"/>
        <result property="responsibilityName" column="responsibility_name"/>
        <result property="accountShareId" column="account_share_id"/>
        <result property="amount" column="amount"/>
        <result property="targetResponsibilityCode" column="target_responsibility_code"/>
        <result property="targetResponsibilityName" column="target_responsibility_name"/>
        <result property="shareParamCode" column="share_param_code"/>
        <result property="shareParamName" column="share_param_name"/>
        <result property="totalAmount" column="total_amount"/>
        <result property="shareParamValueNum" column="share_param_value_num"/>
        <result property="shareParamRate" column="share_param_rate"/>
        <result property="isBaseCost" column="is_base_cost"/>
        <result property="levelSort" column="level_sort"/>
        <result property="levelName" column="level_name"/>
        <result property="shareLevelId" column="share_level_id"/>
        <result property="targetShareLevelId" column="target_share_level_id"/>
        <result property="createTime" column="create_time"/>
        <result property="deleteTime" column="delete_time"/>
    </resultMap>
    <select id="queryAfterAllocationList" resultType="com.kcim.vo.AfterAllocationVO">
        select id as id , date_year as dateYear,date_month as dateMonth, level_sort as levelSort, level_name as
        levelName,
        target_responsibility_code as responsibilityCode, target_responsibility_name as responsibilityName,
        accounting_code as accountingCode, accounting_name as accountingName,
        sum(amount) as amount from cost_allocation_query where delete_time = 0 and hosp_id = #{hospId}
        <if test="dateYear != null">
            and date_year = #{dateYear} and date_month = #{dateMonth}
        </if>
        <if test="responsibilityCode != null and responsibilityCode != ''">
            and target_responsibility_code = #{responsibilityCode}
        </if>
        group by accounting_code , target_responsibility_code ,share_level_id
        limit #{startIndex},#{pageSize}
    </select>
    <select id="queryAfterAllocationListCount" resultType="java.lang.Integer">
        select count(*) from
        (select id from cost_allocation_query where delete_time = 0 and hosp_id = #{hospId}
        <if test="dateYear != null">
            and date_year = #{dateYear} and date_month = #{dateMonth}
        </if>
        <if test="responsibilityCode != null and responsibilityCode != ''">
            and responsibility_code = #{responsibilityCode}
        </if>
        group by accounting_code, target_responsibility_code,share_level_id)t
    </select>
    <select id="queryAfterAllocationListSum" resultType="java.math.BigDecimal">
        select sum(amount) from cost_allocation_query where delete_time = 0 and hosp_id = #{hospId}
        <if test="dateYear != null">
            and date_year = #{dateYear} and date_month = #{dateMonth}
        </if>
        <if test="responsibilityCode != null and responsibilityCode != ''">
            and responsibility_code = #{responsibilityCode}
        </if>
    </select>

    <select id="getAllSortLevel" resultType="com.kcim.dao.model.Allocation">
        select distinct date_year,
                        date_month,
                        level_sort,
                        share_param_code,
                        share_param_name,
                        create_time,
                        id,
                        share_level_id
        from cost_allocation
        where date_year = #{year}
          and date_month = #{month}
          and hosp_id = #{hospId}
          and delete_time=0
        group by share_level_id
    </select>

    <select id="getLevelSortAllocation" resultType="com.kcim.dao.model.Allocation">
        SELECT
            date_year,
            date_month,
            level_sort,
            level_name,
            responsibility_code,
            responsibility_name,
            account_share_id,
            account_code,
            account_name,
            is_base_cost,
            SUM( amount ) AS amount,
            target_responsibility_code,
            target_responsibility_name,
            share_param_code,
            share_param_name,
            SUM( total_amount ) AS total_amount,
            share_param_value_num,
            share_param_rate,
            share_level_id,
            target_share_level_id
        FROM
            cost_allocation
        WHERE
            date_year = #{year}
          AND date_month = #{month}
          AND hosp_id = #{hospId}
          AND delete_time = 0
          AND share_level_id = #{shareLevelId}
        GROUP BY
            responsibility_code,
            target_responsibility_code,
            account_share_id,
            share_param_code
    </select>


</mapper>