<?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.yifu.cloud.plus.v1.business.mapper.salary.TSalaryStatisticsDepartMapper">

    <resultMap id="TSalaryStatisticsDepartVOMap"
               type="com.yifu.cloud.plus.v1.business.vo.salary.TSalaryStatisticsDepartVO">
        <result property="id" column="ID" jdbcType="VARCHAR"/>
        <result property="departName" column="DEPART_NAME" jdbcType="VARCHAR"/>
        <result property="departId" column="DEPART_ID" jdbcType="INTEGER"/>
        <result property="treeLogo" column="TREE_LOGO" jdbcType="VARCHAR"/>
        <result property="salaryMonth" column="SALARY_MONTH" jdbcType="CHAR"/>
        <result property="lastSalaryMonth" column="LAST_SALARY_MONTH" jdbcType="CHAR"/>
        <result property="peopleNumber" column="PEOPLE_NUMBER" jdbcType="INTEGER"/>
        <result property="peopleNumberOffset" column="PEOPLE_NUMBER_OFFSET" jdbcType="DECIMAL"/>
        <result property="peopleNumberFlag" column="PEOPLE_NUMBER_FLAG" jdbcType="CHAR"/>
        <result property="payableSalary" column="PAYABLE_SALARY" jdbcType="DECIMAL"/>
        <result property="payableSalaryOffset" column="PAYABLE_SALARY_OFFSET" jdbcType="DECIMAL"/>
        <result property="payableSalaryFlag" column="PAYABLE_SALARY_FLAG" jdbcType="CHAR"/>
        <result property="payableIncentiveSalary" column="PAYABLE_INCENTIVE_SALARY" jdbcType="DECIMAL"/>
        <result property="payableIncentiveSalaryOffset" column="PAYABLE_INCENTIVE_SALARY_OFFSET" jdbcType="DECIMAL"/>
        <result property="payableIncentiveSalaryFlag" column="PAYABLE_INCENTIVE_SALARY_FLAG" jdbcType="CHAR"/>
        <result property="payableOtherSalary" column="PAYABLE_OTHER_SALARY" jdbcType="DECIMAL"/>
        <result property="payableOtherSalaryOffset" column="PAYABLE_OTHER_SALARY_OFFSET" jdbcType="DECIMAL"/>
        <result property="payableOtherSalaryFlag" column="PAYABLE_OTHER_SALARY_FLAG" jdbcType="CHAR"/>
        <result property="payableTotal" column="PAYABLE_TOTAL" jdbcType="DECIMAL"/>
        <result property="payableTotalOffset" column="PAYABLE_TOTAL_OFFSET" jdbcType="DECIMAL"/>
        <result property="payableTotalFlag" column="PAYABLE_TOTAL_FLAG" jdbcType="CHAR"/>
        <result property="companySocial" column="COMPANY_SOCIAL" jdbcType="DECIMAL"/>
        <result property="companySocialOffset" column="COMPANY_SOCIAL_OFFSET" jdbcType="DECIMAL"/>
        <result property="companySocialFlag" column="COMPANY_SOCIAL_FLAG" jdbcType="CHAR"/>
        <result property="companyAccFund" column="COMPANY_ACC_FUND" jdbcType="DECIMAL"/>
        <result property="companyAccFundOffset" column="COMPANY_ACC_FUND_OFFSET" jdbcType="DECIMAL"/>
        <result property="companyAccFundFlag" column="COMPANY_ACC_FUND_FLAG" jdbcType="CHAR"/>
        <result property="personalSocial" column="PERSONAL_SOCIAL" jdbcType="DECIMAL"/>
        <result property="personalSocialOffset" column="PERSONAL_SOCIAL_OFFSET" jdbcType="DECIMAL"/>
        <result property="personalSocialFlag" column="PERSONAL_SOCIAL_FLAG" jdbcType="CHAR"/>
        <result property="personalAccFund" column="PERSONAL_ACC_FUND" jdbcType="DECIMAL"/>
        <result property="personalAccFundOffset" column="PERSONAL_ACC_FUND_OFFSET" jdbcType="DECIMAL"/>
        <result property="personalAccFundFlag" column="PERSONAL_ACC_FUND_FLAG" jdbcType="CHAR"/>
        <result property="indIncomeTax" column="IND_INCOME_TAX" jdbcType="DECIMAL"/>
        <result property="indIncomeTaxOffset" column="IND_INCOME_TAX_OFFSET" jdbcType="DECIMAL"/>
        <result property="indIncomeTaxFlag" column="IND_INCOME_TAX_FLAG" jdbcType="CHAR"/>
        <result property="otherDedutions" column="OTHER_DEDUTIONS" jdbcType="DECIMAL"/>
        <result property="otherDedutionOffset" column="OTHER_DEDUTION_OFFSET" jdbcType="DECIMAL"/>
        <result property="otherDedutionFalg" column="OTHER_DEDUTION_FALG" jdbcType="CHAR"/>
        <result property="dedutionTotal" column="DEDUTION_TOTAL" jdbcType="DECIMAL"/>
        <result property="dedutionTotalOffset" column="DEDUTION_TOTAL_OFFSET" jdbcType="DECIMAL"/>
        <result property="dedutionTotalFlag" column="DEDUTION_TOTAL_FLAG" jdbcType="CHAR"/>
        <result property="actualSalary" column="ACTUAL_SALARY" jdbcType="DECIMAL"/>
        <result property="actualSalaryOffset" column="ACTUAL_SALARY_OFFSET" jdbcType="DECIMAL"/>
        <result property="actualSalaryFlag" column="ACTUAL_SALARY_FLAG" jdbcType="CHAR"/>
        <result property="perCapita" column="PER_CAPITA" jdbcType="DECIMAL"/>
        <result property="perCapitaOffset" column="PER_CAPITA_OFFSET" jdbcType="DECIMAL"/>
        <result property="perCapitaFlag" column="PER_CAPITA_FLAG" jdbcType="CHAR"/>
        <collection property="children"
                    ofType="com.yifu.cloud.plus.v1.business.vo.salary.TSalaryStatisticsDepartVO"
                    column="{salaryMonth=SALARY_MONTH, treeLogo=TREE_LOGO, lastSalaryMonth=LAST_SALARY_MONTH}"
                    select="sumChildSelect"
                    javaType="java.util.ArrayList"/>
    </resultMap>

    <resultMap id="TSalaryDepartTotalVO" type="com.yifu.cloud.plus.v1.business.vo.salary.TSalaryTotalVO">

    </resultMap>

    <sql id="Where_TSalaryStatisticsDepartVO">
        <if test="query.treeLogo != null">
            and temp.TREE_LOGO like concat(#{query.treeLogo}, '%')
        </if>
        <if test="query.salaryMonth != null and query.salaryMonth.trim() != ''">
            and temp.SALARY_MONTH = #{query.salaryMonth}
        </if>
        <if test="query.departLevel != null">
            and temp.DEPART_LEVEL = #{query.departLevel}
        </if>
    </sql>
    <sql id="Where_ChangeType">
        <if test="query.salaryChangeType != null and query.salaryChangeType.trim() != ''">
            <!-- 0应发增加  -->
            <if test="query.salaryChangeType == '0'.toString()">
                and PAYABLE_TOTAL_FLAG = '2'
            </if>
            <!-- 1应发减少  -->
            <if test="query.salaryChangeType == '1'.toString()">
                and PAYABLE_TOTAL_FLAG = '0'
            </if>
            <!-- 2应发不变  -->
            <if test="query.salaryChangeType == '2'.toString()">
                and PAYABLE_TOTAL_FLAG = '1'
            </if>
            <!-- 3实发增加  -->
            <if test="query.salaryChangeType == '3'.toString()">
                and ACTUAL_SALARY_FLAG = '2'
            </if>
            <!-- 4实发减少  -->
            <if test="query.salaryChangeType == '4'.toString()">
                and ACTUAL_SALARY_FLAG = '0'
            </if>
            <!-- 5实发不变  -->
            <if test="query.salaryChangeType == '5'.toString()">
                and ACTUAL_SALARY_FLAG = '1'
            </if>
            <!-- 6人均成本增加  -->
            <if test="query.salaryChangeType == '6'.toString()">
                and PER_CAPITA_FLAG = '2'
            </if>
            <!-- 7人均成本减少  -->
            <if test="query.salaryChangeType == '7'.toString()">
                and PER_CAPITA_FLAG = '0'
            </if>
            <!-- 8人均成本不变  -->
            <if test="query.salaryChangeType == '8'.toString()">
                and PER_CAPITA_FLAG = '1'
            </if>
            <!-- 9扣款增加  -->
            <if test="query.salaryChangeType == '9'.toString()">
                and DEDUTION_TOTAL_FLAG = '2'
            </if>
            <!-- 10扣款减少  -->
            <if test="query.salaryChangeType == '10'.toString()">
                and DEDUTION_TOTAL_FLAG = '0'
            </if>
            <!-- 11扣款不变  -->
            <if test="query.salaryChangeType == '11'.toString()">
                and DEDUTION_TOTAL_FLAG = '1'
            </if>
        </if>
    </sql>
    <select id="sumChildSelect" resultType="com.yifu.cloud.plus.v1.business.vo.salary.TSalaryStatisticsDepartVO">
        select ID, DEPART_ID, DEPART_NAME, DEPART_LEVEL, SALARY_MONTH, LAST_SALARY_MONTH, PEOPLE_NUMBER,
        PEOPLE_NUMBER_OFFSET,PEOPLE_NUMBER_FLAG, PAYABLE_SALARY, TREE_LOGO,
        PAYABLE_SALARY_OFFSET, PAYABLE_SALARY_FLAG, PAYABLE_INCENTIVE_SALARY, PAYABLE_INCENTIVE_SALARY_OFFSET,
        PAYABLE_INCENTIVE_SALARY_FLAG, PAYABLE_OTHER_SALARY, PAYABLE_OTHER_SALARY_OFFSET, PAYABLE_OTHER_SALARY_FLAG,
        PAYABLE_TOTAL, PAYABLE_TOTAL_OFFSET, PAYABLE_TOTAL_FLAG, COMPANY_SOCIAL, COMPANY_SOCIAL_OFFSET,
        COMPANY_SOCIAL_FLAG, COMPANY_ACC_FUND, COMPANY_ACC_FUND_OFFSET, COMPANY_ACC_FUND_FLAG, PERSONAL_SOCIAL,
        PERSONAL_SOCIAL_OFFSET, PERSONAL_SOCIAL_FLAG, PERSONAL_ACC_FUND, PERSONAL_ACC_FUND_OFFSET,
        PERSONAL_ACC_FUND_FLAG, IND_INCOME_TAX, IND_INCOME_TAX_OFFSET, IND_INCOME_TAX_FLAG, OTHER_DEDUTIONS,
        OTHER_DEDUTION_OFFSET, OTHER_DEDUTION_FALG, DEDUTION_TOTAL, DEDUTION_TOTAL_OFFSET, DEDUTION_TOTAL_FLAG,
        ACTUAL_SALARY, ACTUAL_SALARY_OFFSET, ACTUAL_SALARY_FLAG, round(sum(PER_CAPITA), 2) AS PER_CAPITA,
        round(sum(PER_CAPITA_OFFSET), 2) AS PER_CAPITA_OFFSET, PER_CAPITA_FLAG
        from (
        select
        `a`.`ID` AS `ID`,
        `a`.`DEPART_ID` AS `DEPART_ID`,
        `a`.`DEPART_NAME` AS `DEPART_NAME`,
        `a`.`DEPART_LEVEL` AS `DEPART_LEVEL`,
        `a`.`SALARY_MONTH` AS `SALARY_MONTH`,
        `b`.`SALARY_MONTH` AS `LAST_SALARY_MONTH`,
        `a`.`PAYABLE_SALARY` AS `PAYABLE_SALARY`,
        `a`.`TREE_LOGO` AS `TREE_LOGO`,
        `a`.`PEOPLE_NUMBER` AS `PEOPLE_NUMBER`,
        abs((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`)) AS `PEOPLE_NUMBER_OFFSET`,
        (case
        when ((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`) > 0) then '2'
        when ((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`) = 0) then '1'
        when ((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`) <![CDATA[<]]> 0)
        then '0' end) AS `PEOPLE_NUMBER_FLAG`,
        abs((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`)) AS `PAYABLE_SALARY_OFFSET`,
        (case
        when ((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`) > 0) then '2'
        when ((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`) = 0) then '1'
        when ((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`) <![CDATA[<]]> 0)
        then '0' end) AS `PAYABLE_SALARY_FLAG`,
        `a`.`PAYABLE_INCENTIVE_SALARY` AS `PAYABLE_INCENTIVE_SALARY`,
        abs((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`)) AS `PAYABLE_INCENTIVE_SALARY_OFFSET`,
        (case
        when ((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`) > 0) then '2'
        when ((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`) = 0) then '1'
        when ((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`) <![CDATA[<]]> 0)
        then '0' end) AS `PAYABLE_INCENTIVE_SALARY_FLAG`,
        `a`.`PAYABLE_OTHER_SALARY` AS `PAYABLE_OTHER_SALARY`,
        abs((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`)) AS `PAYABLE_OTHER_SALARY_OFFSET`,
        (case
        when ((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`) > 0) then '2'
        when ((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`) = 0) then '1'
        when ((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`) <![CDATA[<]]> 0)
        then '0' end) AS `PAYABLE_OTHER_SALARY_FLAG`,
        `a`.`PAYABLE_TOTAL` AS `PAYABLE_TOTAL`,
        abs((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`)) AS `PAYABLE_TOTAL_OFFSET`,
        (case
        when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) > 0) then '2'
        when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) = 0) then '1'
        when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) <![CDATA[<]]> 0)
        then '0' end) AS `PAYABLE_TOTAL_FLAG`,
        `a`.`COMPANY_SOCIAL` AS `COMPANY_SOCIAL`,
        abs((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`)) AS `COMPANY_SOCIAL_OFFSET`,
        (case
        when ((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`) > 0) then '2'
        when ((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`) = 0) then '1'
        when ((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`) <![CDATA[<]]> 0)
        then '0' end) AS `COMPANY_SOCIAL_FLAG`,
        `a`.`COMPANY_ACC_FUND` AS `COMPANY_ACC_FUND`,
        abs((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`)) AS `COMPANY_ACC_FUND_OFFSET`,
        (case
        when ((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`) > 0) then '2'
        when ((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`) = 0) then '1'
        when ((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`) <![CDATA[<]]> 0)
        then '0' end) AS `COMPANY_ACC_FUND_FLAG`,
        `a`.`PERSONAL_SOCIAL` AS `PERSONAL_SOCIAL`,
        abs((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`)) AS `PERSONAL_SOCIAL_OFFSET`,
        (case
        when ((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`) > 0) then '2'
        when ((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`) = 0) then '1'
        when ((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`) <![CDATA[<]]> 0)
        then '0' end) AS `PERSONAL_SOCIAL_FLAG`,
        `a`.`PERSONAL_ACC_FUND` AS `PERSONAL_ACC_FUND`,
        abs((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`)) AS `PERSONAL_ACC_FUND_OFFSET`,
        (case
        when ((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`) > 0) then '2'
        when ((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`) = 0) then '1'
        when ((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`) <![CDATA[<]]> 0)
        then '0' end) AS `PERSONAL_ACC_FUND_FLAG`,
        `a`.`IND_INCOME_TAX` AS `IND_INCOME_TAX`,
        abs((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`)) AS `IND_INCOME_TAX_OFFSET`,
        (case
        when ((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`) > 0) then '2'
        when ((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`) = 0) then '1'
        when ((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`) <![CDATA[<]]> 0)
        then '0' end) AS `IND_INCOME_TAX_FLAG`,
        `a`.`OTHER_DEDUTIONS` AS `OTHER_DEDUTIONS`,
        abs((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`)) AS `OTHER_DEDUTION_OFFSET`,
        (case
        when ((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`) > 0) then '2'
        when ((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`) = 0) then '1'
        when ((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`) <![CDATA[<]]> 0)
        then '0' end) AS `OTHER_DEDUTION_FALG`,
        `a`.`DEDUTION_TOTAL` AS `DEDUTION_TOTAL`,
        abs((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`)) AS `DEDUTION_TOTAL_OFFSET`,
        (case
        when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) > 0) then '2'
        when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) = 0) then '1'
        when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) <![CDATA[<]]> 0)
        then '0' end) AS `DEDUTION_TOTAL_FLAG`,
        `a`.`ACTUAL_SALARY` AS `ACTUAL_SALARY`,
        abs((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`)) AS `ACTUAL_SALARY_OFFSET`,
        (case
        when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) > 0) then '2'
        when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) = 0) then '1'
        when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) <![CDATA[<]]> 0)
        then '0' end) AS `ACTUAL_SALARY_FLAG`,
        `a`.`PER_CAPITA` AS `PER_CAPITA`,
        abs((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`)) AS `PER_CAPITA_OFFSET`,
        (case
        when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) > 0) then '2'
        when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) = 0) then '1'
        when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) <![CDATA[<]]> 0)
        then '0' end) AS `PER_CAPITA_FLAG`
        from (
        SELECT `hr_business`.`t_bus_salary`.`id` AS `ID`,
        `hr_business`.`t_bus_salary`.`dept_id` AS `DEPART_ID`,
        `hr_business`.`t_bus_salary`.`dept_name` AS `DEPART_NAME`,
        `hr_business`.`t_bus_salary`.`dept_level` AS `DEPART_LEVEL`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `TREE_LOGO`,
        concat(#{treeLogo}, '') AS `DOMAIN`,
        `hr_business`.`t_bus_salary`.`salary_month` AS `SALARY_MONTH`,
        sum(`hr_business`.`t_bus_salary`.`relay_salary`) AS `PAYABLE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_incent`) AS `PAYABLE_INCENTIVE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_other`) AS `PAYABLE_OTHER_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) AS `PAYABLE_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_social`) AS `COMPANY_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_fund`) AS `COMPANY_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_social`) AS `PERSONAL_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`) AS `PERSONAL_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`) AS `IND_INCOME_TAX`,
        sum(`hr_business`.`t_bus_salary`.`withholid_other`) AS `OTHER_DEDUTIONS`,
        sum(`hr_business`.`t_bus_salary`.`withholid_sum`) AS `DEDUTION_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`actual_salary`) AS `ACTUAL_SALARY`,
        if(`hr_business`.`t_bus_salary`.`dept_tree_logo` <![CDATA[<>]]> '', count(DISTINCT
        `hr_business`.`t_bus_salary`.`emp_id_card`), null) AS `PEOPLE_NUMBER`,
        (
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
        count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`)) AS `PER_CAPITA`
        FROM `hr_business`.`t_bus_salary`
        WHERE
        `salary_month` = #{salaryMonth}
        and `dept_tree_logo` like concat(#{treeLogo}, '%')
        ) `a`
        LEFT JOIN (
        SELECT
        `hr_business`.`t_bus_salary`.`id` AS `ID`,
        `hr_business`.`t_bus_salary`.`dept_id` AS `DEPART_ID`,
        `hr_business`.`t_bus_salary`.`dept_name` AS `DEPART_NAME`,
        `hr_business`.`t_bus_salary`.`dept_level` AS `DEPART_LEVEL`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `TREE_LOGO`,
        concat(#{treeLogo}, '') AS `DOMAIN`,
        `hr_business`.`t_bus_salary`.`salary_month` AS `SALARY_MONTH`,
        sum(`hr_business`.`t_bus_salary`.`relay_salary`) AS `PAYABLE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_incent`) AS `PAYABLE_INCENTIVE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_other`) AS `PAYABLE_OTHER_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) AS `PAYABLE_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_social`) AS `COMPANY_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_fund`) AS `COMPANY_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_social`) AS `PERSONAL_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`) AS `PERSONAL_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`) AS `IND_INCOME_TAX`,
        sum(`hr_business`.`t_bus_salary`.`withholid_other`) AS `OTHER_DEDUTIONS`,
        sum(`hr_business`.`t_bus_salary`.`withholid_sum`) AS `DEDUTION_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`actual_salary`) AS `ACTUAL_SALARY`,
        if(`hr_business`.`t_bus_salary`.`dept_tree_logo` <![CDATA[<>]]> '', count(DISTINCT
        `hr_business`.`t_bus_salary`.`emp_id_card`), null) AS `PEOPLE_NUMBER`,
        (
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
        count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`)) AS `PER_CAPITA`
        FROM
        `hr_business`.`t_bus_salary`
        WHERE
        salary_month = #{lastSalaryMonth}
        and `dept_tree_logo` like concat(#{treeLogo}, '%')
        ) `b` ON `a`.DOMAIN = `b`.DOMAIN
        ) `temp`
        <where>
            1=1
            and temp.TREE_LOGO like concat(#{treeLogo}, '%')
            and temp.SALARY_MONTH = #{salaryMonth}
        </where>
    </select>
    <select id="getListByPage" resultMap="TSalaryStatisticsDepartVOMap">
        select distinct substring_index(a.dept_tree_logo, '-', (#{query.departLevel} + 2)) AS TREE_LOGO,
        #{query.salaryMonth} AS SALARY_MONTH,
        #{query.lastSalaryMonth} AS LAST_SALARY_MONTH,
        b.ID AS ID,
        b.NAME AS DEPART_NAME,
        b.ID AS DEPART_ID
        from hr_business.t_bus_salary a
        left join hr_business.t_bus_dept b
        on substring_index(a.dept_tree_logo, '-', (#{query.departLevel} + 2)) = b.tree_logo
        <where>
            1 = 1
            and a.salary_month = #{query.salaryMonth}
            and length(a.dept_tree_logo) - length(replace(a.dept_tree_logo, '-', '')) >= (#{query.departLevel} + 1)
            and substring_index(a.dept_tree_logo, '-', (#{query.departLevel} + 2)) like concat(#{query.treeLogo}, '%')
        </where>
    </select>

    <select id="getCurDepartData" resultType="com.yifu.cloud.plus.v1.business.vo.salary.TSalaryStatisticsDepartVO">
        select ID,
               DEPART_ID,
               DEPART_NAME,
               DEPART_LEVEL,
               SALARY_MONTH,
               LAST_SALARY_MONTH,
               PEOPLE_NUMBER,
               PEOPLE_NUMBER_OFFSET,
               PEOPLE_NUMBER_FLAG,
               PAYABLE_SALARY,
               TREE_LOGO,
               PAYABLE_SALARY_OFFSET,
               PAYABLE_SALARY_FLAG,
               PAYABLE_INCENTIVE_SALARY,
               PAYABLE_INCENTIVE_SALARY_OFFSET,
               PAYABLE_INCENTIVE_SALARY_FLAG,
               PAYABLE_OTHER_SALARY,
               PAYABLE_OTHER_SALARY_OFFSET,
               PAYABLE_OTHER_SALARY_FLAG,
               PAYABLE_TOTAL,
               PAYABLE_TOTAL_OFFSET,
               PAYABLE_TOTAL_FLAG,
               COMPANY_SOCIAL,
               COMPANY_SOCIAL_OFFSET,
               COMPANY_SOCIAL_FLAG,
               COMPANY_ACC_FUND,
               COMPANY_ACC_FUND_OFFSET,
               COMPANY_ACC_FUND_FLAG,
               PERSONAL_SOCIAL,
               PERSONAL_SOCIAL_OFFSET,
               PERSONAL_SOCIAL_FLAG,
               PERSONAL_ACC_FUND,
               PERSONAL_ACC_FUND_OFFSET,
               PERSONAL_ACC_FUND_FLAG,
               IND_INCOME_TAX,
               IND_INCOME_TAX_OFFSET,
               IND_INCOME_TAX_FLAG,
               OTHER_DEDUTIONS,
               OTHER_DEDUTION_OFFSET,
               OTHER_DEDUTION_FALG,
               DEDUTION_TOTAL,
               DEDUTION_TOTAL_OFFSET,
               DEDUTION_TOTAL_FLAG,
               ACTUAL_SALARY,
               ACTUAL_SALARY_OFFSET,
               ACTUAL_SALARY_FLAG,
               round(sum(PER_CAPITA), 2)        AS PER_CAPITA,
               round(sum(PER_CAPITA_OFFSET), 2) AS PER_CAPITA_OFFSET,
               PER_CAPITA_FLAG
        from (
                 select `a`.`ID`                                                               AS `ID`,
                        `a`.`DEPART_ID`                                                        AS `DEPART_ID`,
                        `a`.`DEPART_NAME`                                                      AS `DEPART_NAME`,
                        `a`.`DEPART_LEVEL`                                                     AS `DEPART_LEVEL`,
                        `a`.`SALARY_MONTH`                                                     AS `SALARY_MONTH`,
                        `b`.`SALARY_MONTH`                                                     AS `LAST_SALARY_MONTH`,
                        `a`.`PAYABLE_SALARY`                                                   AS `PAYABLE_SALARY`,
                        `a`.`TREE_LOGO`                                                        AS `TREE_LOGO`,
                        `a`.`PEOPLE_NUMBER`                                                    AS `PEOPLE_NUMBER`,
                        abs((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`))                       AS `PEOPLE_NUMBER_OFFSET`,
                        (case
                             when ((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`) > 0) then '2'
                             when ((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`) = 0) then '1'
                             when ((`a`.`PEOPLE_NUMBER` - `b`.`PEOPLE_NUMBER`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PEOPLE_NUMBER_FLAG`,
                        abs((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`))                     AS `PAYABLE_SALARY_OFFSET`,
                        (case
                             when ((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`) > 0) then '2'
                             when ((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`) = 0) then '1'
                             when ((`a`.`PAYABLE_SALARY` - `b`.`PAYABLE_SALARY`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PAYABLE_SALARY_FLAG`,
                        `a`.`PAYABLE_INCENTIVE_SALARY`                                         AS `PAYABLE_INCENTIVE_SALARY`,
                        abs((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`)) AS `PAYABLE_INCENTIVE_SALARY_OFFSET`,
                        (case
                             when ((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`) > 0) then '2'
                             when ((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`) = 0) then '1'
                             when ((`a`.`PAYABLE_INCENTIVE_SALARY` - `b`.`PAYABLE_INCENTIVE_SALARY`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PAYABLE_INCENTIVE_SALARY_FLAG`,
                        `a`.`PAYABLE_OTHER_SALARY`                                             AS `PAYABLE_OTHER_SALARY`,
                        abs((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`))         AS `PAYABLE_OTHER_SALARY_OFFSET`,
                        (case
                             when ((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`) > 0) then '2'
                             when ((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`) = 0) then '1'
                             when ((`a`.`PAYABLE_OTHER_SALARY` - `b`.`PAYABLE_OTHER_SALARY`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PAYABLE_OTHER_SALARY_FLAG`,
                        `a`.`PAYABLE_TOTAL`                                                    AS `PAYABLE_TOTAL`,
                        abs((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`))                       AS `PAYABLE_TOTAL_OFFSET`,
                        (case
                             when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) > 0) then '2'
                             when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) = 0) then '1'
                             when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PAYABLE_TOTAL_FLAG`,
                        `a`.`COMPANY_SOCIAL`                                                   AS `COMPANY_SOCIAL`,
                        abs((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`))                     AS `COMPANY_SOCIAL_OFFSET`,
                        (case
                             when ((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`) > 0) then '2'
                             when ((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`) = 0) then '1'
                             when ((`a`.`COMPANY_SOCIAL` - `b`.`COMPANY_SOCIAL`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `COMPANY_SOCIAL_FLAG`,
                        `a`.`COMPANY_ACC_FUND`                                                 AS `COMPANY_ACC_FUND`,
                        abs((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`))                 AS `COMPANY_ACC_FUND_OFFSET`,
                        (case
                             when ((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`) > 0) then '2'
                             when ((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`) = 0) then '1'
                             when ((`a`.`COMPANY_ACC_FUND` - `b`.`COMPANY_ACC_FUND`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `COMPANY_ACC_FUND_FLAG`,
                        `a`.`PERSONAL_SOCIAL`                                                  AS `PERSONAL_SOCIAL`,
                        abs((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`))                   AS `PERSONAL_SOCIAL_OFFSET`,
                        (case
                             when ((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`) > 0) then '2'
                             when ((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`) = 0) then '1'
                             when ((`a`.`PERSONAL_SOCIAL` - `b`.`PERSONAL_SOCIAL`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PERSONAL_SOCIAL_FLAG`,
                        `a`.`PERSONAL_ACC_FUND`                                                AS `PERSONAL_ACC_FUND`,
                        abs((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`))               AS `PERSONAL_ACC_FUND_OFFSET`,
                        (case
                             when ((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`) > 0) then '2'
                             when ((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`) = 0) then '1'
                             when ((`a`.`PERSONAL_ACC_FUND` - `b`.`PERSONAL_ACC_FUND`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PERSONAL_ACC_FUND_FLAG`,
                        `a`.`IND_INCOME_TAX`                                                   AS `IND_INCOME_TAX`,
                        abs((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`))                     AS `IND_INCOME_TAX_OFFSET`,
                        (case
                             when ((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`) > 0) then '2'
                             when ((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`) = 0) then '1'
                             when ((`a`.`IND_INCOME_TAX` - `b`.`IND_INCOME_TAX`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `IND_INCOME_TAX_FLAG`,
                        `a`.`OTHER_DEDUTIONS`                                                  AS `OTHER_DEDUTIONS`,
                        abs((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`))                   AS `OTHER_DEDUTION_OFFSET`,
                        (case
                             when ((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`) > 0) then '2'
                             when ((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`) = 0) then '1'
                             when ((`a`.`OTHER_DEDUTIONS` - `b`.`OTHER_DEDUTIONS`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `OTHER_DEDUTION_FALG`,
                        `a`.`DEDUTION_TOTAL`                                                   AS `DEDUTION_TOTAL`,
                        abs((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`))                     AS `DEDUTION_TOTAL_OFFSET`,
                        (case
                             when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) > 0) then '2'
                             when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) = 0) then '1'
                             when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `DEDUTION_TOTAL_FLAG`,
                        `a`.`ACTUAL_SALARY`                                                    AS `ACTUAL_SALARY`,
                        abs((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`))                       AS `ACTUAL_SALARY_OFFSET`,
                        (case
                             when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) > 0) then '2'
                             when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) = 0) then '1'
                             when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `ACTUAL_SALARY_FLAG`,
                        `a`.`PER_CAPITA`                                                       AS `PER_CAPITA`,
                        abs((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`))                             AS `PER_CAPITA_OFFSET`,
                        (case
                             when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) > 0) then '2'
                             when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) = 0) then '1'
                             when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) <![CDATA[<]]> 0)
                                 then '0' end)                                                 AS `PER_CAPITA_FLAG`
                 from (
                          SELECT `hr_business`.`t_bus_salary`.`id`                               AS `ID`,
                                 `hr_business`.`t_bus_salary`.`dept_id`                          AS `DEPART_ID`,
                                 `hr_business`.`t_bus_salary`.`dept_name`                        AS `DEPART_NAME`,
                                 `hr_business`.`t_bus_salary`.`dept_level`                       AS `DEPART_LEVEL`,
                                 `hr_business`.`t_bus_salary`.`dept_tree_logo`                   AS `TREE_LOGO`,
                                 concat('domain', '')                                            AS `DOMAIN`,
                                 `hr_business`.`t_bus_salary`.`salary_month`                     AS `SALARY_MONTH`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_salary`)                AS `PAYABLE_SALARY`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_incent`)                AS `PAYABLE_INCENTIVE_SALARY`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_other`)                 AS `PAYABLE_OTHER_SALARY`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_sum`)                   AS `PAYABLE_TOTAL`,
                                 sum(`hr_business`.`t_bus_salary`.`unit_social`)                 AS `COMPANY_SOCIAL`,
                                 sum(`hr_business`.`t_bus_salary`.`unit_fund`)                   AS `COMPANY_ACC_FUND`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_person_social`)     AS `PERSONAL_SOCIAL`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`)       AS `PERSONAL_ACC_FUND`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`)        AS `IND_INCOME_TAX`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_other`)             AS `OTHER_DEDUTIONS`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_sum`)               AS `DEDUTION_TOTAL`,
                                 sum(`hr_business`.`t_bus_salary`.`actual_salary`)               AS `ACTUAL_SALARY`,
                                 if(`hr_business`.`t_bus_salary`.`dept_tree_logo` <![CDATA[<>]]> '', count(DISTINCT
                                                                                                  `hr_business`.`t_bus_salary`.`emp_id_card`),
                                    null)                                                        AS `PEOPLE_NUMBER`,
                                 (
                                         sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
                                         count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`)) AS `PER_CAPITA`
                          FROM `hr_business`.`t_bus_salary`
                          WHERE 1 = 1
                            and salary_month = #{query.salaryMonth}
                            and `dept_id` = #{query.departId}
                      ) `a`
                          LEFT JOIN (
                     SELECT `hr_business`.`t_bus_salary`.`id`                           AS `ID`,
                            `hr_business`.`t_bus_salary`.`dept_id`                      AS `DEPART_ID`,
                            `hr_business`.`t_bus_salary`.`dept_name`                    AS `DEPART_NAME`,
                            `hr_business`.`t_bus_salary`.`dept_level`                   AS `DEPART_LEVEL`,
                            `hr_business`.`t_bus_salary`.`dept_tree_logo`               AS `TREE_LOGO`,
                            concat('domain', '')                                        AS `DOMAIN`,
                            `hr_business`.`t_bus_salary`.`salary_month`                 AS `SALARY_MONTH`,
                            sum(`hr_business`.`t_bus_salary`.`relay_salary`)            AS `PAYABLE_SALARY`,
                            sum(`hr_business`.`t_bus_salary`.`relay_incent`)            AS `PAYABLE_INCENTIVE_SALARY`,
                            sum(`hr_business`.`t_bus_salary`.`relay_other`)             AS `PAYABLE_OTHER_SALARY`,
                            sum(`hr_business`.`t_bus_salary`.`relay_sum`)               AS `PAYABLE_TOTAL`,
                            sum(`hr_business`.`t_bus_salary`.`unit_social`)             AS `COMPANY_SOCIAL`,
                            sum(`hr_business`.`t_bus_salary`.`unit_fund`)               AS `COMPANY_ACC_FUND`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_person_social`) AS `PERSONAL_SOCIAL`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`)   AS `PERSONAL_ACC_FUND`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`)    AS `IND_INCOME_TAX`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_other`)         AS `OTHER_DEDUTIONS`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_sum`)           AS `DEDUTION_TOTAL`,
                            sum(`hr_business`.`t_bus_salary`.`actual_salary`)           AS `ACTUAL_SALARY`,
                            if(`hr_business`.`t_bus_salary`.`dept_tree_logo` <![CDATA[<>]]> '', count(DISTINCT
                                                                                             `hr_business`.`t_bus_salary`.`emp_id_card`),
                               null)                                                    AS `PEOPLE_NUMBER`,
                            (sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
                             count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`))    AS `PER_CAPITA`
                     FROM `hr_business`.`t_bus_salary`
                     WHERE 1 = 1
                       and salary_month = #{query.lastSalaryMonth}
                       and `dept_id` = #{query.departId}
                 ) `b` ON `a`.DOMAIN = `b`.DOMAIN
             ) `temp`
    </select>

    <select id="sumSalaryDepart" resultMap="TSalaryDepartTotalVO">
        select
        sum(PAYABLE_TOTAL) AS PAYABLE_TOTAL,
        sum(PAYABLE_TOTAL_OFFSET) AS PAYABLE_TOTAL_OFFSET,
        PAYABLE_TOTAL_FLAG,
        sum(DEDUTION_TOTAL) AS DEDUTION_TOTAL,
        sum(DEDUTION_TOTAL_OFFSET) AS DEDUTION_TOTAL_OFFSET,
        DEDUTION_TOTAL_FLAG,
        sum(ACTUAL_SALARY) AS ACTUAL_SALARY,
        sum(ACTUAL_SALARY_OFFSET) AS ACTUAL_SALARY_OFFSET,
        ACTUAL_SALARY_FLAG,
        round(sum(PER_CAPITA), 2) AS PER_CAPITA,
        round(sum(PER_CAPITA_OFFSET), 2) AS PER_CAPITA_OFFSET,
        PER_CAPITA_FLAG
        from (
        select
        `a`.`PAYABLE_TOTAL` AS `PAYABLE_TOTAL`,
        abs((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`)) AS `PAYABLE_TOTAL_OFFSET`,
        (case
        when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) > 0) then '2'
        when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) = 0) then '1'
        when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) <![CDATA[<]]> 0)
        then '0' end) AS `PAYABLE_TOTAL_FLAG`,
        `a`.`DEDUTION_TOTAL` AS `DEDUTION_TOTAL`,
        abs((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`)) AS `DEDUTION_TOTAL_OFFSET`,
        (case
        when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) > 0) then '2'
        when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) = 0) then '1'
        when ((`a`.`DEDUTION_TOTAL` - `b`.`DEDUTION_TOTAL`) <![CDATA[<]]> 0)
        then '0' end) AS `DEDUTION_TOTAL_FLAG`,
        `a`.`ACTUAL_SALARY` AS `ACTUAL_SALARY`,
        abs((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`)) AS `ACTUAL_SALARY_OFFSET`,
        (case
        when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) > 0) then '2'
        when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) = 0) then '1'
        when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) <![CDATA[<]]> 0)
        then '0' end) AS `ACTUAL_SALARY_FLAG`,
        `a`.`PER_CAPITA` AS `PER_CAPITA`,
        abs((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`)) AS `PER_CAPITA_OFFSET`,
        (case
        when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) > 0) then '2'
        when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) = 0) then '1'
        when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) <![CDATA[<]]> 0)
        then '0' end) AS `PER_CAPITA_FLAG`
        from (
        SELECT `hr_business`.`t_bus_salary`.`id` AS `ID`,
        `hr_business`.`t_bus_salary`.`dept_id` AS `DEPART_ID`,
        `hr_business`.`t_bus_salary`.`dept_name` AS `DEPART_NAME`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `TREE_LOGO`,
        `hr_business`.`t_bus_salary`.`salary_month` AS `SALARY_MONTH`,
        #{query.treeLogo} AS `DOMAIN`,
        sum(`hr_business`.`t_bus_salary`.`relay_salary`) AS `PAYABLE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_incent`) AS `PAYABLE_INCENTIVE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_other`) AS `PAYABLE_OTHER_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) AS `PAYABLE_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_social`) AS `COMPANY_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_fund`) AS `COMPANY_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_social`) AS `PERSONAL_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`) AS `PERSONAL_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`) AS `IND_INCOME_TAX`,
        sum(`hr_business`.`t_bus_salary`.`withholid_other`) AS `OTHER_DEDUTIONS`,
        sum(`hr_business`.`t_bus_salary`.`withholid_sum`) AS `DEDUTION_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`actual_salary`) AS `ACTUAL_SALARY`,
        count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`) AS `PEOPLE_NUMBER`,
        round(
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
        count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`), 2) AS `PER_CAPITA`
        FROM `hr_business`.`t_bus_salary`
        WHERE
        salary_month = #{query.salaryMonth}
        and dept_tree_logo like concat(#{query.treeLogo}, '%')
        ) `a`
        LEFT JOIN (
        SELECT
        `hr_business`.`t_bus_salary`.`id` AS `ID`,
        `hr_business`.`t_bus_salary`.`dept_id` AS `DEPART_ID`,
        `hr_business`.`t_bus_salary`.`dept_name` AS `DEPART_NAME`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `TREE_LOGO`,
        `hr_business`.`t_bus_salary`.`salary_month` AS `salary_month`,
        #{query.treeLogo} AS `DOMAIN`,
        sum(`hr_business`.`t_bus_salary`.`relay_salary`) AS `PAYABLE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_incent`) AS `PAYABLE_INCENTIVE_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_other`) AS `PAYABLE_OTHER_SALARY`,
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) AS `PAYABLE_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_social`) AS `COMPANY_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`unit_fund`) AS `COMPANY_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_social`) AS `PERSONAL_SOCIAL`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`) AS `PERSONAL_ACC_FUND`,
        sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`) AS `IND_INCOME_TAX`,
        sum(`hr_business`.`t_bus_salary`.`withholid_other`) AS `OTHER_DEDUTIONS`,
        sum(`hr_business`.`t_bus_salary`.`withholid_sum`) AS `DEDUTION_TOTAL`,
        sum(`hr_business`.`t_bus_salary`.`actual_salary`) AS `ACTUAL_SALARY`,
        count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`) AS `PEOPLE_NUMBER`,
        round(
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
        count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`), 2) AS `PER_CAPITA`
        FROM
        `hr_business`.`t_bus_salary`
        WHERE
        salary_month = #{query.lastSalaryMonth}
        and dept_tree_logo like concat(#{query.treeLogo}, '%')
        ) `b` ON `a`.DOMAIN = `b`.DOMAIN
        ) `temp`
    </select>

    <select id="getCurDepartSumData" resultMap="TSalaryDepartTotalVO">
        select PAYABLE_TOTAL,
               PAYABLE_TOTAL_OFFSET,
               PAYABLE_TOTAL_FLAG,
               ACTUAL_SALARY,
               ACTUAL_SALARY_OFFSET,
               ACTUAL_SALARY_FLAG,
               round(sum(PER_CAPITA), 2)        AS PER_CAPITA,
               round(sum(PER_CAPITA_OFFSET), 2) AS PER_CAPITA_OFFSET,
               PER_CAPITA_FLAG
        from (
                 select `a`.`PAYABLE_TOTAL`                              AS `PAYABLE_TOTAL`,
                        abs((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`)) AS `PAYABLE_TOTAL_OFFSET`,
                        (case
                             when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) > 0) then '2'
                             when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) = 0) then '1'
                             when ((`a`.`PAYABLE_TOTAL` - `b`.`PAYABLE_TOTAL`) <![CDATA[<]]> 0)
                                 then '0' end)                           AS `PAYABLE_TOTAL_FLAG`,
                        `a`.`ACTUAL_SALARY`                              AS `ACTUAL_SALARY`,
                        abs((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`)) AS `ACTUAL_SALARY_OFFSET`,
                        (case
                             when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) > 0) then '2'
                             when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) = 0) then '1'
                             when ((`a`.`ACTUAL_SALARY` - `b`.`ACTUAL_SALARY`) <![CDATA[<]]> 0)
                                 then '0' end)                           AS `ACTUAL_SALARY_FLAG`,
                        `a`.`PER_CAPITA`                                 AS `PER_CAPITA`,
                        abs((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`))       AS `PER_CAPITA_OFFSET`,
                        (case
                             when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) > 0) then '2'
                             when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) = 0) then '1'
                             when ((`a`.`PER_CAPITA` - `b`.`PER_CAPITA`) <![CDATA[<]]> 0)
                                 then '0' end)                           AS `PER_CAPITA_FLAG`
                 from (
                          SELECT `hr_business`.`t_bus_salary`.`id`                               AS `ID`,
                                 `hr_business`.`t_bus_salary`.`dept_id`                          AS `DEPART_ID`,
                                 `hr_business`.`t_bus_salary`.`dept_name`                        AS `DEPART_NAME`,
                                 `hr_business`.`t_bus_salary`.`dept_level`                       AS `DEPART_LEVEL`,
                                 `hr_business`.`t_bus_salary`.`dept_tree_logo`                   AS `TREE_LOGO`,
                                 concat('domain', '')                                            AS `DOMAIN`,
                                 `hr_business`.`t_bus_salary`.`salary_month`                     AS `SALARY_MONTH`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_salary`)                AS `PAYABLE_SALARY`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_incent`)                AS `PAYABLE_INCENTIVE_SALARY`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_other`)                 AS `PAYABLE_OTHER_SALARY`,
                                 sum(`hr_business`.`t_bus_salary`.`relay_sum`)                   AS `PAYABLE_TOTAL`,
                                 sum(`hr_business`.`t_bus_salary`.`unit_social`)                 AS `COMPANY_SOCIAL`,
                                 sum(`hr_business`.`t_bus_salary`.`unit_fund`)                   AS `COMPANY_ACC_FUND`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_person_social`)     AS `PERSONAL_SOCIAL`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`)       AS `PERSONAL_ACC_FUND`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`)        AS `IND_INCOME_TAX`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_other`)             AS `OTHER_DEDUTIONS`,
                                 sum(`hr_business`.`t_bus_salary`.`withholid_sum`)               AS `DEDUTION_TOTAL`,
                                 sum(`hr_business`.`t_bus_salary`.`actual_salary`)               AS `ACTUAL_SALARY`,
                                 if(`hr_business`.`t_bus_salary`.`dept_tree_logo` <![CDATA[<>]]> '', count(DISTINCT
                                                                                                  `hr_business`.`t_bus_salary`.`emp_id_card`),
                                    null)                                                        AS `PEOPLE_NUMBER`,
                                 (
                                         sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
                                         count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`)) AS `PER_CAPITA`
                          FROM `hr_business`.`t_bus_salary`
                          WHERE 1 = 1
                            and salary_month = #{query.salaryMonth}
                            and `dept_id` = #{query.departId}
                      ) `a`
                          LEFT JOIN (
                     SELECT `hr_business`.`t_bus_salary`.`id`                               AS `ID`,
                            `hr_business`.`t_bus_salary`.`dept_id`                          AS `DEPART_ID`,
                            `hr_business`.`t_bus_salary`.`dept_name`                        AS `DEPART_NAME`,
                            `hr_business`.`t_bus_salary`.`dept_level`                       AS `DEPART_LEVEL`,
                            `hr_business`.`t_bus_salary`.`dept_tree_logo`                   AS `TREE_LOGO`,
                            concat('domain', '')                                            AS `DOMAIN`,
                            `hr_business`.`t_bus_salary`.`salary_month`                     AS `SALARY_MONTH`,
                            sum(`hr_business`.`t_bus_salary`.`relay_salary`)                AS `PAYABLE_SALARY`,
                            sum(`hr_business`.`t_bus_salary`.`relay_incent`)                AS `PAYABLE_INCENTIVE_SALARY`,
                            sum(`hr_business`.`t_bus_salary`.`relay_other`)                 AS `PAYABLE_OTHER_SALARY`,
                            sum(`hr_business`.`t_bus_salary`.`relay_sum`)                   AS `PAYABLE_TOTAL`,
                            sum(`hr_business`.`t_bus_salary`.`unit_social`)                 AS `COMPANY_SOCIAL`,
                            sum(`hr_business`.`t_bus_salary`.`unit_fund`)                   AS `COMPANY_ACC_FUND`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_person_social`)     AS `PERSONAL_SOCIAL`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_person_fund`)       AS `PERSONAL_ACC_FUND`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_person_tax`)        AS `IND_INCOME_TAX`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_other`)             AS `OTHER_DEDUTIONS`,
                            sum(`hr_business`.`t_bus_salary`.`withholid_sum`)               AS `DEDUTION_TOTAL`,
                            sum(`hr_business`.`t_bus_salary`.`actual_salary`)               AS `ACTUAL_SALARY`,
                            if(`hr_business`.`t_bus_salary`.`dept_tree_logo` <![CDATA[<>]]> '', count(DISTINCT
                                                                                             `hr_business`.`t_bus_salary`.`emp_id_card`),
                               null)                                                        AS `PEOPLE_NUMBER`,
                            (
                                    sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
                                    count(DISTINCT `hr_business`.`t_bus_salary`.`emp_id_card`)) AS `PER_CAPITA`
                     FROM `hr_business`.`t_bus_salary`
                     WHERE 1 = 1
                       and salary_month = #{query.lastSalaryMonth}
                       and `dept_id` = #{query.departId}
                 ) `b` ON `a`.DOMAIN = `b`.DOMAIN
             ) `temp`
    </select>
</mapper>
