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

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kcim.dao.model.CostDepartmentProfit" id="costDepartmentProfitMap">
        <result property="id" column="id"/>
        <result property="year" column="year"/>
        <result property="month" column="month"/>
        <result property="responsibilityCode" column="responsibility_code"/>
        <result property="responsibilityName" column="responsibility_name"/>
        <result property="reportParentId" column="report_parent_id"/>
        <result property="reportId" column="report_id"/>
        <result property="reportNum" column="report_num"/>
        <result property="reportName" column="report_name"/>
        <result property="calcFormula" column="calc_formula"/>
        <result property="calcType" column="calc_type"/>
        <result property="amount" column="amount"/>
        <result property="hospId" column="hosp_id"/>
        <result property="costType" column="cost_type"/>
        <result property="incomeType" column="income_type"/>
        <result property="shareType" column="share_type"/>
        <result property="percent" column="percent"/>
        <result property="createTime" column="create_time"/>
        <result property="deleteTime" column="delete_time"/>
    </resultMap>
    <select id="getMaxYear" resultType="java.lang.Integer">
        select max(`year`)
        from cost_department_profit
        where delete_time = 0
          and hosp_id = #{hospId}

    </select>
    <select id="getMaxMonth" resultType="java.lang.Integer">
        select  MAX(`month`)
        from cost_department_profit
        where delete_time = 0
          and hosp_id = #{hospId}
          and `year` = #{year}
    </select>
    <select id="getAnnualSummaryByYear" parameterType="map" resultType="com.kcim.vo.CostProfitVo">
        SELECT
            `year`,
            `month`,
            report_parent_id as reportParentId,
            report_id as reportId,
            report_num as reportNum,
            report_name as reportName,
            calc_formula as calcFormula,
            calc_type as calcType,
            responsibility_code as responsibilityCode,
            responsibility_name as responsibilityName,
            cost_type as costType,
            income_type as incomeType,
            share_type as shareType,
            percent,
            SUM(budget_amount) AS budget_amount,
            SUM(amount) AS amount,
            SUM(prev_period_amount) AS prev_period_amount,
            SUM(same_period_amount) AS same_period_amount
        FROM cost_department_profit
        WHERE year = #{year}
          AND hosp_id = #{hospId}
          AND share_type = #{reportType}
          AND responsibility_code = #{responsibilityCode}
          AND delete_time = 0
        GROUP BY report_id
        ORDER BY report_num
    </select>

    <select id="getAnnualSummaryByYearRange" parameterType="map" resultType="com.kcim.vo.CostProfitVo">
        SELECT
            `year`,
            report_parent_id as reportParentId,
            report_id as reportId,
            report_num as reportNum,
            report_name as reportName,
            calc_formula as calcFormula,
            calc_type as calcType,
            responsibility_code as responsibilityCode,
            responsibility_name as responsibilityName,
            cost_type as costType,
            income_type as incomeType,
            share_type as shareType,
            percent,
            SUM(budget_amount) AS budget_amount,
            SUM(amount) AS amount,
            SUM(prev_period_amount) AS prev_period_amount,
            SUM(same_period_amount) AS same_period_amount
        FROM cost_department_profit
        WHERE year BETWEEN #{startYear} AND #{endYear}
          AND hosp_id = #{hospId}
          AND share_type = #{reportType}
          AND responsibility_code = #{responsibilityCode}
          AND delete_time = 0
        GROUP BY `year`, report_id
        ORDER BY `year`, report_num
    </select>
</mapper>