<?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.malk.jinlun.mapper.JinlunMapper">


    <select id="getCustomerDzmx" resultType="java.util.Map">
        SELECT
            客户, 单据编号, 业务日期, 性能, 规格, 数量, 重量, 计价单价, 税率, 价税合计, 回款金额, NULL AS "往期欠款", 备注
        FROM (
                 -- 暂估应收数据
                 SELECT
                     t1.ShortText1760602119316 AS "客户",
                     t1.ShortText1760602123650 AS "单据编号",
                     t1.Date1760602205733 AS "业务日期",
                     t2.Text1760602314150 AS "性能",
                     t2.Text1760602292846 AS "规格",
                     t2.Number1762915279108 AS "数量",
                     t2.Number1762915289424 AS "重量",
                     t2.Number1760602330651 AS "计价单价",
                     t2.Number1760602343876 AS "税率",
                     t2.Number1760602359012 AS "价税合计",
                     NULL AS "回款金额",
                     t2.Text1760602391849 AS "备注"
                 FROM i940k_ysd t1
                          LEFT JOIN i940k_sheet1760602277185 t2 ON t1.id = t2.parentId
                 WHERE t1.ShortText1760602147441 = '暂估应收'
                   AND t1.ShortText1760602123650 NOT LIKE '%-%'
                   AND t1.Date1760602205733 between #{start_date} and #{end_date}
                   AND t1.ShortText1760602119316 = #{customer}

                 UNION ALL

                 -- 回款数据
                 SELECT
                     t1.ShortText1760585132200 AS "客户",
                     t1.ShortText1760585125989 AS "单据编号",
                     t1.Date1760585187315 AS "业务日期",
                     NULL AS "性能",
                     NULL AS "规格",
                     NULL AS "数量",
                     NULL AS "重量",
                     NULL AS "计价单价",
                     NULL AS "税率",
                     NULL AS "价税合计",
                     t2.Number1760585242849 AS "回款金额",
                     t2.ShortText1760585258640 AS "备注"
                 FROM i940k_skd t1
                          LEFT JOIN i940k_sheet1760585205663 t2 ON t1.id = t2.parentId
                 WHERE t1.Date1760585187315 between #{start_date} and #{end_date}
                   AND t1.ShortText1760585132200 = #{customer}
                 -- 多个客户时用：AND FIND_IN_SET(t1.ShortText1760585132200, @target_customers) > 0
             ) data
        ORDER BY 业务日期,单据编号,性能  -- 同一天内应收行在前，回款行在后
    </select>
    <select id="getCustomerWqqk" resultType="java.util.Map">
        SELECT
        COALESCE(receivable.amount, 0) AS '往期应付',
        COALESCE(paid.amount, 0) AS '往期已付',
        COALESCE(receivable.amount, 0) - COALESCE(paid.amount, 0) AS '往期欠款'
        FROM
        (SELECT 1 AS dummy) AS base
        LEFT JOIN (
        SELECT SUM(t2.Number1760602359012) AS amount
        FROM i940k_ysd t1
        LEFT JOIN i940k_sheet1760602277185 t2 ON t1.id = t2.parentId
        WHERE t1.ShortText1760602147441 = '暂估应收'
        AND t1.ShortText1760602123650 NOT LIKE '%-%'
        AND t1.Date1760602205733 &lt; #{start_date}
        AND t1.ShortText1760602119316 = #{customer}
        ) AS receivable ON 1=1
        LEFT JOIN (
        SELECT SUM(t2.Number1760585242849) AS amount
        FROM i940k_skd t1
        LEFT JOIN i940k_sheet1760585205663 t2 ON t1.id = t2.parentId
        WHERE t1.Date1760585187315 &lt; #{start_date}
        AND t1.ShortText1760585132200 = #{customer}
        ) AS paid ON 1=1
    </select>
</mapper>

