<?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.yd.csf.service.dao.SalaryMapper">

    <select id="page" resultType="com.yd.csf.feign.response.salary.ApiSalaryPageResponse">
        SELECT
        s.salary_no,
        s.salary_biz_id,
        s.broker_biz_id,
        s.broker_name,
        s.internal_number,
        s.paid_amount,
        s.month,
        s.status,
        s.creator_name,
        s.create_time,
        GROUP_CONCAT(DISTINCT sr.currency ORDER BY sr.currency SEPARATOR ';') AS currencyList
        FROM salary s
        LEFT JOIN salary_remittance sr ON s.salary_biz_id = sr.salary_biz_id AND sr.is_deleted = 0

        <where>
            <if test="request.brokerName != null and request.brokerName != ''">
                AND s.broker_name LIKE CONCAT('%', #{request.brokerName}, '%')
            </if>
            <if test="request.month != null and request.month != ''">
                AND s.month = #{request.month}
            </if>
            <if test="request.status != null and request.status != ''">
                AND s.status = #{request.status}
            </if>
            AND s.is_deleted = 0
        </where>
        GROUP BY s.salary_biz_id
        ORDER BY s.create_time DESC
    </select>

    <select id="pushPage" resultType="com.yd.csf.feign.response.salary.ApiSalaryPushPageResponse">
        select
        s.salary_biz_id,
        s.broker_name,
        s.month,
        s.paid_amount,
        s.gross_amount,
        (s.gross_amount - s.paid_amount) as totalDeductions,
        s.other_remark,
        s.status,
        s.creator_name,
        s.create_time
        from salary s
        <where>
            <if test="request.brokerName != null and request.brokerName != ''">
                AND s.broker_name LIKE CONCAT('%', #{request.brokerName}, '%')
            </if>
            <if test="request.month != null and request.month != ''">
                AND s.month = #{request.month}
            </if>
            <if test="request.status != null and request.status != ''">
                AND s.status = #{request.status}
            </if>
            AND s.is_deleted = 0
        </where>
    </select>
</mapper>
