<?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.ruoyi.oa.mapper.SysOaFinanceMapper">

    <resultMap type="com.ruoyi.oa.domain.vo.SysOaFinanceVo" id="SysOaFinanceResult">
        <result property="financeId" column="finance_id"/>
        <result property="projectId" column="project_id"/>
        <result property="financeTitle" column="finance_title"/>
        <result property="financeParties" column="finance_parties"/>
        <result property="payType" column="pay_type"/>
        <result property="financeType" column="finance_type"/>
        <result property="financeTime" column="finance_time"/>
        <result property="makeRatio" column="make_ratio"/>
        <result property="makePrice" column="make_price"/>
        <result property="makeTime" column="make_time"/>
        <result property="makeExplain" column="make_explain"/>
        <result property="accessory" column="accessory"/>
        <result property="remark" column="finance_remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="outType" column="out_type"/>
        <result property="outMoney" column="out_money"/>
        <result property="totalOut" column="total_out"/>
        <result property="totalIn" column="total_in"/>
        <result property="month" column="month"/>
        <result property="paymentProgressId" column="payment_progress_id"/>
        <result property="status" column="status"/>
        <result property="signingCompany" column="signing_company"/>

        <result property="receiveAccountId" column="receive_account_id"/>
        <result property="receiveAccountName" column="receive_account_name"/>
        <association property="project" column="project_id" javaType="SysOaProject" resultMap="SysOaProjectResult"/>
        <association property="detailList" javaType="java.util.List" resultMap="detailResult"/>

    </resultMap>



    <select id="selectByPaymentProgressId" resultType="com.ruoyi.oa.domain.vo.SysOaFinanceVo">
        SELECT *
        FROM sys_oa_finance
        WHERE payment_progress_id = #{progressId}
    </select>


    <resultMap id="detailResult" type="SysOaDetail">
        <id property="detailId" column="detail_id"/>
        <result property="financeId" column="finance_id"/>
        <result property="detailTitle" column="detail_title"/>
        <result property="price" column="price"/>
        <result property="bigPrice" column="big_price"/>
        <result property="exitType" column="exit_type"/>
        <result property="remark" column="detail_remark"/>
    </resultMap>

    <resultMap id="SysOaProjectResult" type="SysOaProject">
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
    </resultMap>

    <sql id="selectFinanceVo">
        select sof.finance_id,
               sof.project_id,
               sof.finance_title,
               sof.finance_parties,
               sof.pay_type,
               sof.finance_type,
               sof.finance_time,
               sof.make_ratio,
               sof.make_price,
               sof.make_time,
               sof.make_explain,
               sof.accessory,
               sof.remark as finance_remark,
               sof.create_by,
               sof.create_time,
               sof.update_by,
               sof.update_time,
               b.detail_id,
               b.detail_title,
               sof.out_type,
               b.price,
               b.big_price,
               b.remark as detail_remark,
               sof.signing_company
        from sys_oa_finance sof
                 left join sys_oa_detail b on sof.finance_id = b.finance_id
    </sql>

    <select id="selectPageFinanceList" resultMap="SysOaFinanceResult">
        <include refid="selectFinanceVo" />
            ${ew.getCustomSqlSegment}
    </select>

    <select id="selectFinanceList" resultMap="SysOaFinanceResult">
        select sof.finance_id,
               sof.project_id,
               sof.finance_title,
               sof.finance_parties,
               sof.pay_type,
               sof.finance_type,
               sof.finance_time,
               sof.make_ratio,
               sof.make_price,
               sof.make_time,
               sof.make_explain,
               sof.accessory,
               sof.remark as finance_remark,
               sof.create_by,
               sof.create_time,
               sof.update_by,
               sof.update_time,
               sof.out_type,
               b.detail_id,
               b.detail_title,
               b.price,
               b.big_price,
               b.remark as detail_remark,
               p.project_id,
               p.project_name,
               sora.receive_account_name,
               sora.receive_account_id,
               sof.payment_progress_id,
               sof.status
        from sys_oa_finance sof
                 left join sys_oa_detail b on sof.finance_id = b.finance_id
                 left join sys_oa_project p on sof.project_id = p.project_id
                left join sys_oa_receive_account sora on sof.receive_account_id = sora.receive_account_id
            ${ew.getCustomSqlSegment}
    </select>

    <select id="selectFinanceById" parameterType="Long" resultMap="SysOaFinanceResult">
        select sof.finance_id,
               sof.project_id,
               sof.finance_title,
               sof.finance_parties,
               sof.pay_type,
               sof.finance_type,
               sof.finance_time,
               sof.make_ratio,
               sof.make_price,
               sof.make_time,
               sof.make_explain,
               sof.accessory,
               sof.remark as finance_remark,
               sof.create_by,
               sof.create_time,
               sof.update_by,
               sof.update_time,
               sof.out_type,
               b.detail_id,
               b.detail_title,
               b.price,
               b.big_price,
               b.remark as detail_remark,
               p.project_id,
               p.project_name,
               sora.receive_account_name,
               sora.receive_account_id,
               sof.status,
            sof.payment_progress_id
        from sys_oa_finance sof
                 left join sys_oa_detail b on sof.finance_id = b.finance_id
                 left join sys_oa_project p on sof.project_id = p.project_id
                 left join sys_oa_receive_account sora on sof.receive_account_id = sora.receive_account_id
        where sof.finance_id = #{financeId}
    </select>
    <!--进出账查询-->
    <select id="selectFinanceByProjectId" resultMap="SysOaFinanceResult">
        select a.finance_id, a.project_id, a.finance_title, a.finance_parties, a.pay_type, a.finance_type, a.finance_time, a.make_ratio, a.make_price, a.make_time, a.make_explain,  a.accessory, a.remark as finance_remark, a.create_by, a.create_time, a.update_by, a.update_time,
               b.detail_id, b.detail_title, b.price, b.big_price, b.remark as detail_remark,a.out_type,a.payment_progress_id,a.status
        from sys_oa_finance a
                 left join sys_oa_detail b on a.finance_id = b.finance_id
        where a.project_id = #{projectId} and a.finance_type = #{financeType}
    </select>

    <!--进出账查询-->
    <select id="findFinanceByTime" resultMap="SysOaFinanceResult">
        <include refid="selectFinanceVo" />
        where sof.finance_type = #{financeType}
        <if test="  payType!=NULL and payType!=null and  payType !='0' and payType!=0 ">
            and sof.pay_type = #{payType}
        </if>

        <if test="  receiveAccountId!=NULL and receiveAccountId!=null and receiveAccountId!='-1' ">
            and sof.receive_account_id = #{receiveAccountId}
        </if>
        and sof.project_id = 0
        and date_format(sof.finance_time,'%Y-%m-%d %H:%i:%s') >= date_format(#{beginTime},'%Y-%m-%d %H:%i:%s')
        and date_format(sof.finance_time,'%Y-%m-%d %H:%i:%s') &lt;= date_format(#{endTime},'%Y-%m-%d %H:%i:%s')
    </select>
    <!-- 月度收支对比：仅查询近 6 个月，使用 f.finance_type 判断进（1）出（0）账 -->
    <select id="getBarData" resultType="com.ruoyi.oa.domain.dto.BarDataDTO">
        SELECT
        DATE_FORMAT(f.finance_time, '%Y-%m') AS month,
        SUM(CASE WHEN f.finance_type = '0' THEN d.price ELSE 0 END) AS totalOut,
        SUM(CASE WHEN f.finance_type = '1' THEN d.price ELSE 0 END) AS totalIn
        FROM sys_oa_detail d
        JOIN sys_oa_finance f ON d.finance_id = f.finance_id
        <where>
            <!-- 可选的项目过滤 -->
            <if test="projectId != null and projectId > 0">
                AND f.project_id = #{projectId}
            </if>
            <!-- 只筛近 6 个月：从 5 个月前的第一天到当月最后一天 -->
            AND f.finance_time BETWEEN
            DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 5 MONTH), '%Y-%m-01')
            AND LAST_DAY(CURDATE())
        </where>
        GROUP BY DATE_FORMAT(f.finance_time, '%Y-%m')
        ORDER BY month
    </select>

    <!-- 明细列表：含币种判断（使用 f.remark 来模糊匹配币种），并只查询近 6 个月的数据 -->
    <select id="getFinanceDetails" resultType="com.ruoyi.oa.domain.dto.FinanceDetailDTO">
        SELECT
        DATE_FORMAT(f.finance_time, '%Y-%m-%d') AS date,
        f.finance_type                        AS type,
        CASE
        WHEN f.remark LIKE '%美金%' OR f.remark LIKE '%美元%' THEN 'USD'
        ELSE 'CNY'
        END                                   AS currency,
        d.price                               AS amount,
        f.project_id                          AS projectId
        FROM sys_oa_detail d
        JOIN sys_oa_finance f ON d.finance_id = f.finance_id
        <where>
            <!-- 项目过滤 -->
            <if test="projectId != null and projectId > 0">
                AND f.project_id = #{projectId}
            </if>
            <!-- 进/出账类型过滤 -->
            <if test="financeType != null and financeType != ''">
                AND f.finance_type = #{financeType}
            </if>
            <!-- 只查询近 6 个月的记录 -->
            AND f.finance_time BETWEEN
            DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 5 MONTH), '%Y-%m-01')
            AND LAST_DAY(CURDATE())
        </where>
        ORDER BY f.finance_time DESC
    </select>

</mapper>
