<?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="vip.xiaonuo.money.mapper.MoneyBusinessMapper">


    <select id="dsBusAll" resultType="vip.xiaonuo.money.param.DsBusAllParam">
      SELECT
    id,
    org_id,
    ROUND(IFNULL(SUM(business_income) , 0)/10000, 2) AS business_income,
--     ROUND(IFNULL(SUM(business_cost), 0), 2) AS business_cost,
    ROUND(IFNULL(SUM(profit), 0)/10000, 2) AS profit,
    ROUND(IFNULL(SUM(taxes), 0)/10000, 2) AS taxes,
    ROUND(IFNULL(SUM(balance), 0)/10000, 2) AS balance,
   ROUND(IFNULL( (SELECT SUM(borrow_price)
     FROM `money_borrow_warning`  ${ew.customSqlSegment} ), 0)/10000, 2) AS borrow_price
FROM
    `money_business`
     ${ew.customSqlSegment}
    </select>
    <select id="getDsBusYyJlYjDetailMonth" resultType="vip.xiaonuo.money.param.DsBusAllParam">
        SELECT
    DATE_FORMAT(create_time, '%Y-%m') AS ymonth, -- 提取年份和月份
    org_id,
    ROUND(IFNULL(SUM(business_income), 0)/10000, 2) AS business_income,
   -- ROUND(IFNULL(SUM(business_cost), 0), 2) AS business_cost,
    ROUND(IFNULL(SUM(profit), 0)/10000, 2) AS profit,
    ROUND(IFNULL(SUM(taxes), 0)/10000, 2) AS taxes
    -- ROUND(IFNULL(SUM(balance), 0), 2) AS balance,
FROM
    `money_business`
     ${ew.customSqlSegment}
-- 根据月份和组织ID进行分组

    </select>
    <select id="getDsBusYyJlYjDetailYear" resultType="vip.xiaonuo.money.param.DsBusAllParam">
         SELECT
    DATE_FORMAT(create_time, '%Y') AS ymonth, -- 提取年份和月份
    org_id,
    ROUND(IFNULL(SUM(business_income), 0)/10000, 2) AS business_income,
   -- ROUND(IFNULL(SUM(business_cost), 0), 2) AS business_cost,
    ROUND(IFNULL(SUM(profit), 0)/10000, 2) AS profit,
    ROUND(IFNULL(SUM(taxes), 0)/10000, 2) AS taxes
    -- ROUND(IFNULL(SUM(balance), 0), 2) AS balance,
FROM
    `money_business`
-- 根据月份和组织ID进行分组
     ${ew.customSqlSegment}
    </select>
    <select id="getBalanceDetailMonth" resultType="vip.xiaonuo.money.param.DsBusAllParam">
         SELECT
    DATE_FORMAT(create_time, '%Y-%m') AS ymonth,
     ROUND(IFNULL(SUM(balance), 0)/10000, 2) AS balance
FROM
    `money_business`
     ${ew.customSqlSegment}


    </select>
    <select id="getBalanceDetailYear" resultType="vip.xiaonuo.money.param.DsBusAllParam">
         SELECT
    DATE_FORMAT(create_time, '%Y') AS ymonth,
     ROUND(IFNULL(SUM(balance), 0)/10000, 2) AS balance
FROM
    `money_business`
     ${ew.customSqlSegment}


    </select>
    <select id="getBorrowDetailMonth" resultType="vip.xiaonuo.money.param.DsBusAllParam">
          SELECT ROUND(IFNULL(SUM(borrow_price), 0)/10000, 2)  borrow_price , DATE_FORMAT(borrow_time, '%Y-%m') AS ymonth ,
       (
        SELECT ROUND(IFNULL(SUM(borrow_price), 0)/10000, 2)
        FROM `money_borrow_warning`
        WHERE DATE_FORMAT(repayment_time, '%Y-%m') = DATE_FORMAT(mb.borrow_price, '%Y-%m')
       
    ) AS repaymentPrice 
       
        FROM `money_borrow_warning` mb
         ${ew.customSqlSegment}

    </select>
    <select id="getBorrowDetailYear" resultType="vip.xiaonuo.money.param.DsBusAllParam">
           SELECT ROUND(IFNULL(SUM(borrow_price), 0)/10000, 2)  borrow_price , DATE_FORMAT(borrow_time, '%Y') AS ymonth ,
       (
        SELECT ROUND(IFNULL(SUM(borrow_price), 0)/10000, 2)
        FROM `money_borrow_warning`
        WHERE DATE_FORMAT(repayment_time, '%Y') = DATE_FORMAT(mb.borrow_price, '%Y') -- 确保与主查询的月份相匹配
       
    ) AS repaymentPrice 
       
        FROM `money_borrow_warning` mb
         ${ew.customSqlSegment}

    </select>

</mapper>
