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

    <resultMap id="TSalaryStatisticsPersonVOMap"
               type="com.yifu.cloud.plus.v1.business.vo.salary.TSalaryStatisticsPersonalVO">
        <result property="id" column="ID" jdbcType="VARCHAR"/>
        <result property="teleNo" column="TELE_NO" jdbcType="VARCHAR"/>
        <result property="empIdCard" column="emp_id_card"/>
        <result property="empName" column="EMP_NAME" jdbcType="VARCHAR"/>
        <result property="salaryMonth" column="SALARY_MONTH" 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"/>
    </resultMap>


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

    </resultMap>

    <sql id="Where_TSalaryStatisticsPersonVO">
        <if test="query.treeLogo != null">
            and temp.TREE_LOGO like concat(#{query.treeLogo}, '%')
        </if>
        <if test="query.teleNo != null and query.teleNo.trim() != ''">
            and temp.TELE_NO like concat('%', #{query.teleNo}, '%')
        </if>
        <if test="query.empName != null and query.empName.trim() != ''">
            and temp.EMP_NAME like concat('%', #{query.empName}, '%')
        </if>
        <if test="query.empIdCard != null and query.empIdCard.trim() != ''">
            AND temp.emp_id_card like concat(#{query.empIdCard}, '%')
        </if>
        <if test="query.salaryMonth != null and query.salaryMonth.trim() != ''">
            and temp.SALARY_MONTH = #{query.salaryMonth}
        </if>
        <if test="query.salaryChangeType != null and query.salaryChangeType.trim() != ''">
            <!-- 0应发增加  -->
            <if test="query.salaryChangeType == '0'.toString()">
                and temp.PAYABLE_TOTAL_FLAG = '2'
            </if>
            <!-- 1应发减少  -->
            <if test="query.salaryChangeType == '1'.toString()">
                and temp.PAYABLE_TOTAL_FLAG = '0'
            </if>
            <!-- 2应发不变  -->
            <if test="query.salaryChangeType == '2'.toString()">
                and temp.PAYABLE_TOTAL_FLAG = '1'
            </if>
            <!-- 3实发增加  -->
            <if test="query.salaryChangeType == '3'.toString()">
                and temp.ACTUAL_SALARY_FLAG = '2'
            </if>
            <!-- 4实发减少  -->
            <if test="query.salaryChangeType == '4'.toString()">
                and temp.ACTUAL_SALARY_FLAG = '0'
            </if>
            <!-- 5实发不变  -->
            <if test="query.salaryChangeType == '5'.toString()">
                and temp.ACTUAL_SALARY_FLAG = '1'
            </if>
            <!-- 6人均成本增加  -->
            <if test="query.salaryChangeType == '6'.toString()">
                and temp.PER_CAPITA_FLAG = '2'
            </if>
            <!-- 7人均成本减少  -->
            <if test="query.salaryChangeType == '7'.toString()">
                and temp.PER_CAPITA_FLAG = '0'
            </if>
            <!-- 8人均成本不变  -->
            <if test="query.salaryChangeType == '8'.toString()">
                and temp.PER_CAPITA_FLAG = '1'
            </if>
            <!-- 9扣款增加  -->
            <if test="query.salaryChangeType == '9'.toString()">
                and temp.DEDUTION_TOTAL_FLAG = '2'
            </if>
            <!-- 10扣款减少  -->
            <if test="query.salaryChangeType == '10'.toString()">
                and temp.DEDUTION_TOTAL_FLAG = '0'
            </if>
            <!-- 11扣款不变  -->
            <if test="query.salaryChangeType == '11'.toString()">
                and temp.DEDUTION_TOTAL_FLAG = '1'
            </if>
        </if>
    </sql>
    <select id="getListByPage" resultMap="TSalaryStatisticsPersonVOMap">
        select ID, TELE_NO,EMP_ID_CARD, EMP_NAME, SALARY_MONTH, 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
        from (
        select
        `a`.`ID` AS `ID`,
        `a`.`TELE_NO` AS `TELE_NO`,
        `a`.`EMP_NAME` AS `EMP_NAME`,
        `a`.`EMP_ID_CARD` AS `EMP_ID_CARD`,
        `a`.`TREE_LOGO` AS `TREE_LOGO`,
        `a`.`SALARY_MONTH` AS `SALARY_MONTH`,
        `a`.`PAYABLE_SALARY` AS `PAYABLE_SALARY`,
        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`
        from (
        SELECT
        `hr_business`.`t_bus_salary`.`id` AS `ID`,
        `hr_business`.`t_bus_salary`.`tele_no` AS `TELE_NO`,
        `hr_business`.`t_bus_salary`.`emp_id_card` AS `EMP_ID_CARD`,
        `hr_business`.`t_bus_salary`.`emp_name` AS `EMP_NAME`,
        `hr_business`.`t_bus_salary`.`salary_month` AS `SALARY_MONTH`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `TREE_LOGO`,
        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`
        FROM `hr_business`.`t_bus_salary`
        <where>
            1=1
            and salary_month = #{query.salaryMonth}
            <if test="query.selectType == '0'.toString()">
                and dept_tree_logo like concat(#{query.treeLogo}, '%')
            </if>
            <if test="query.selectType == '1'.toString()">
                and dept_tree_logo = #{query.treeLogo}
            </if>
        </where>
        GROUP BY EMP_ID_CARD
        ) `a`
        LEFT JOIN (
        SELECT
        `hr_business`.`t_bus_salary`.`id` AS `ID`,
        `hr_business`.`t_bus_salary`.`tele_no` AS `TELE_NO`,
        `hr_business`.`t_bus_salary`.`emp_name` AS `EMP_NAME`,
        `hr_business`.`t_bus_salary`.`emp_id_card` AS `EMP_ID_CARD`,
        `hr_business`.`t_bus_salary`.`salary_month` AS `salary_month`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `TREE_LOGO`,
        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`
        FROM
        `hr_business`.`t_bus_salary`
        <where>
            1=1
            and salary_month = #{query.lastSalaryMonth}
            <if test="query.selectType == '0'.toString()">
                and dept_tree_logo like concat(#{query.treeLogo}, '%')
            </if>
            <if test="query.selectType == '1'.toString()">
                and dept_tree_logo = #{query.treeLogo}
            </if>
        </where>
        GROUP BY emp_id_card
        ) `b` ON `a`.emp_id_card = `b`.emp_id_card
        ) `temp`
        <where>
            1=1
            <include refid="Where_TSalaryStatisticsPersonVO"/>
        </where>
    </select>

    <select id="sumSalaryPerson" resultMap="TSalaryDepartTotalVO">
        select TREE_LOGO,
        EMP_NAME,
        TELE_NO,
        SALARY_MONTH,
        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,
        convert(sum(PER_CAPITA), DECIMAL(10,2)) AS PER_CAPITA,
        convert(sum(PER_CAPITA_OFFSET), DECIMAL(10,2)) AS PER_CAPITA_OFFSET,
        PER_CAPITA_FLAG
        from (
        select
        `a`.`TELE_NO` AS `TELE_NO`,
        `a`.`EMP_NAME` AS `EMP_NAME`,
        `a`.`SALARY_MONTH` AS `SALARY_MONTH`,
        `a`.`DEPART_TREE_LOGO` AS `TREE_LOGO`,
        `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`.`tele_no` AS `TELE_NO`,
        `hr_business`.`t_bus_salary`.`emp_name` AS `EMP_NAME`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `DEPART_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`,
        (
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
        count(distinct emp_id_card)) AS `PER_CAPITA`
        FROM `hr_business`.`t_bus_salary`
        <where>
            1=1
            and salary_month = #{query.salaryMonth}
            <if test="query.selectType == '0'.toString()">
                and dept_tree_logo like concat(#{query.treeLogo}, '%')
            </if>
            <if test="query.selectType == '1'.toString()">
                and dept_tree_logo = #{query.treeLogo}
            </if>
            <if test="query.teleNo != null and query.teleNo.trim() != ''">
                and TELE_NO like concat('%', #{query.teleNo}, '%')
            </if>
            <if test="query.empName != null and query.empName.trim() != ''">
                and EMP_NAME like concat('%', #{query.empName}, '%')
            </if>
            <if test="query.empIdCard != null and query.empIdCard.trim() != ''">
                AND EMP_ID_CARD like concat(#{query.empIdCard}, '%')
            </if>
        </where>
        ) `a`
        LEFT JOIN
        (
        SELECT
        `hr_business`.`t_bus_salary`.`tele_no` AS `TELE_NO`,
        `hr_business`.`t_bus_salary`.`emp_name` AS `EMP_NAME`,
        `hr_business`.`t_bus_salary`.`dept_tree_logo` AS `DEPART_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`,
        (
        sum(`hr_business`.`t_bus_salary`.`relay_sum`) /
        count(distinct emp_id_card)) AS `PER_CAPITA`
        FROM
        `hr_business`.`t_bus_salary`
        <where>
            1=1
            and salary_month = #{query.lastSalaryMonth}
            <if test="query.selectType == '0'.toString()">
                and dept_tree_logo like concat(#{query.treeLogo}, '%')
            </if>
            <if test="query.selectType == '1'.toString()">
                and dept_tree_logo = #{query.treeLogo}
            </if>
            <if test="query.teleNo != null and query.teleNo.trim() != ''">
                and TELE_NO like concat('%', #{query.teleNo}, '%')
            </if>
            <if test="query.empName != null and query.empName.trim() != ''">
                and EMP_NAME like concat('%', #{query.empName}, '%')
            </if>
            <if test="query.empIdCard != null and query.empIdCard.trim() != ''">
                AND EMP_ID_CARD like concat(#{query.empIdCard}, '%')
            </if>
        </where>

        ) `b` ON `a`.DOMAIN = `b`.DOMAIN
        ) `temp`
    </select>
</mapper>
