<?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.dayou.mapper.FinanceClaimMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.FinanceClaim">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="deleted" property="deleted" />
        <result column="modified" property="modified" />
        <result column="real_fund_id" property="realFundId" />
        <result column="production_fund_id" property="productionFundId" />
        <result column="claim_user_id" property="claimUserId" />
        <result column="claim_datetime" property="claimDatetime" />
        <result column="claim_amount" property="claimAmount" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        created,
        deleted,
        modified,
        real_fund_id, production_fund_id, claim_user_id, claim_datetime, claim_amount
    </sql>

    <select id="getList" parameterType="com.dayou.entity.FinanceClaim" resultType="com.dayou.vo.FinanceClaimVO">
        SELECT
            fc.id,
            fc.real_fund_id,
            ( CASE orf.business_type WHEN 'MAJOR_BUSINESS' THEN '大中型业务' WHEN 'PERSONAL_BUSINESS' THEN '个贷业务' WHEN 'ASSET_BUSINESS' THEN '资产业务' ELSE '-' END ) AS businessType,
            orf.should_amount,
            fc.claim_amount,
            u.NAME AS claimName,
            fc.created,
            orf.order_id,
            orf.order_name,
            orf.remark
        FROM
            finance_claim fc
                LEFT JOIN order_fund orf ON fc.order_fund_id = orf.id
                LEFT JOIN user u ON u.id = fc.claim_user_id
        WHERE
            fc.real_fund_id = #{realFundId}
          AND fc.deleted = 0
        ORDER BY
            fc.created DESC
    </select>

    <select id="getTotalClaimAmountByProductionFundId" parameterType="java.lang.Long" resultType="java.math.BigDecimal">
        select ifnull(sum(claim_amount),0) from finance_claim where production_fund_id = #{productionFundId} and deleted = 0
    </select>

    <select id="residueAmount" parameterType="java.lang.Long" resultType="java.math.BigDecimal">
        SELECT amount -( SELECT IFNULL( sum( claim_amount ), 0 ) FROM `finance_claim` WHERE deleted = 0 AND real_fund_id = #{financeFundId} ) AS residueAmount
        FROM
            finance_real_fund
        WHERE
            id = #{financeFundId} and deleted = 0
    </select>

    <sql id="majorStatQuery">
        SELECT
        fc.*,
        f.production_no as reportNo,
        f.order_id,
        f.should_amount,
        m.financial,
        m.members,
        m.bailor,
        m.NAME AS orderName,
        mp.NAME,
        mp.if_save_file,
        mp.evaluate_amount,
        mp.production as productionType,
        u.NAME AS clientManager,
        u1.NAME AS principal,
        cc1.NAME AS clientName,
        cc2.NAME AS clientSubName,
        frf.pay_datetime,
        frf.payer,
        u2.NAME AS claimUser,
        d.NAME AS evaluateDepartment,
        (
        SELECT
        GROUP_CONCAT( NAME SEPARATOR '、' )
        FROM
        department
        WHERE
        id IN (
        SELECT
        department_id
        FROM
        post
        WHERE
        id IN ( SELECT post_id FROM user_post WHERE user_id = u.id ))) AS marketDepartment
        FROM
        (
        SELECT
        of.id AS oId,
        pf.id AS pId,
        of.business_id,
        of.order_id,
        pf.production_no,
        of.should_amount
        FROM
        order_fund of
        LEFT JOIN ( SELECT id, real_amount, production_no, production_type, order_fund_id, created FROM production_fund WHERE business_type = "MAJOR_BUSINESS" AND deleted = 0 AND real_amount != 0 ) pf ON pf.order_fund_id = of.id
        WHERE
        of.business_type = 'MAJOR_BUSINESS'
        AND of.deleted = 0
        ) f
        INNER JOIN finance_claim fc ON
        IF
        (
        f.pId IS NULL,(
        fc.order_fund_id = f.oId
        ),(
        fc.order_fund_id = f.oId
        AND fc.production_fund_id = f.pId
        )
        )
        LEFT JOIN major m ON f.business_id = m.id
        LEFT JOIN user u ON u.id = m.client_manager_id
        LEFT JOIN user u1 ON u1.id = m.principal_id
        LEFT JOIN department d ON d.id = m.department_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        LEFT JOIN customer_linkman cl1 ON cl1.id = m.clientele_contact_id
        LEFT JOIN finance_real_fund frf ON frf.id = fc.real_fund_id
        LEFT JOIN ( SELECT major_id, report_no,production, evaluate_amount, if_save_file, NAME FROM major_production WHERE deleted = 0 ) mp ON ( mp.major_id = m.id AND mp.report_no = f.production_no )
        LEFT JOIN user u2 ON u2.id = fc.claim_user_id
        where m.deleted = 0
        <if test="majorStat!=null and majorStat.orderId!=null and majorStat.orderId!='' ">
            and f.order_id like concat ('%',#{majorStat.orderId},'%')
        </if>
        <if test="majorStat!=null and majorStat.reportNo!=null and majorStat.reportNo!='' ">
            and f.production_no like concat ('%',#{majorStat.reportNo},'%')
        </if>
        <if test="majorStat!=null and majorStat.startDate!=null and majorStat.startDate!='' ">
            and fc.claim_datetime &gt;= #{majorStat.startDate}
        </if>
        <if test="majorStat!=null and majorStat.endDate!=null and majorStat.endDate!='' ">
            and fc.claim_datetime &lt;= #{majorStat.endDate}
        </if>
        <if test="majorStat!=null and majorStat.principal!=null and majorStat.principal!='' ">
            and u1.name like concat ('%',#{majorStat.principal},'%')
        </if>
        <if test="majorStat!=null and majorStat.claimAmount!=null and majorStat.claimAmount!='' ">
            and fc.claim_amount = #{majorStat.claimAmount}
        </if>
        <if test="majorStat!=null and majorStat.ifSaveFile!=null ">
            and mp.if_save_file = #{majorStat.ifSaveFile}
        </if>
        <if test="majorStat!=null and majorStat.financial!=null ">
            and m.financial = #{majorStat.financial}
        </if>
        <if test="majorStat!=null and majorStat.claimUser!=null and majorStat.claimUser!='' ">
            and u2.NAME like concat ('%',#{majorStat.claimUser},'%')
        </if>
        ORDER BY
        fc.claim_datetime DESC
    </sql>
    <select id="majorStat" parameterType="com.dayou.vo.RealFundMajorStatVO" resultType="com.dayou.vo.RealFundMajorStatVO">
        <include refid="majorStatQuery" />
    </select>

    <select id="majorStatExport" parameterType="com.dayou.vo.RealFundMajorStatVO" resultType="com.dayou.vo.RealFundMajorStatVO">
        <include refid="majorStatQuery" />
    </select>

    <!--资产实收款统计-->
    <sql id="assetsStatQuery">
        SELECT
        claim.real_fund_id,
        claim.order_fund_id,
        claim.production_fund_id,
        claim.id,
        prod.production_no AS reportNo,
        assets.order_id AS orderId,
        assets.`name` AS orderName,
        (
        SELECT
        department.name
        FROM
        department,
        post,
        user_post,
        `user`
        WHERE
        `user`.id = user_post.user_id
        AND user_post.post_id = post.id
        AND post.department_id = department.id
        AND `user`.id = assets.client_manager_id
        ) AS marketDepartment,
        ( SELECT department.name FROM department WHERE department.id = assets.department_id ) AS evaluateDepartment,
        managerUser.name AS clientManagerName,
        principalUser.name AS principalName,
        realFund.payer AS payer,
        realFund.pay_datetime AS payDateTime,
        order_fund.should_amount AS orderShouldAmount,
        prodFund.production_should_amount AS prodShouldAmount,
        claim.claim_amount AS claimAmount,
        claimUser.name AS claimUser,
        assets.assets_business_gener AS assetsBusinessGener,
        ( SELECT customer_company.name FROM customer_company WHERE customer_company.id = assets.clientele_id ) AS clientName,
        ( SELECT customer_company.name FROM customer_company WHERE customer_company.id = assets.clientele_sub_id ) AS clientSubName,
        assets.bailor AS bailor,
        prod.evaluate_amount,
        prod.production_type AS productionType,
        prod.if_save_file AS ifSaveFile
        FROM
        finance_claim AS claim
        LEFT JOIN finance_real_fund AS realFund ON realFund.id = claim.real_fund_id
        LEFT JOIN order_fund ON order_fund.id = claim.order_fund_id
        LEFT JOIN production_fund AS prodFund ON prodFund.id = claim.production_fund_id
        LEFT JOIN assets ON assets.id = order_fund.business_id
        AND assets.order_id = order_fund.order_id
        LEFT JOIN `user` AS managerUser ON managerUser.id = assets.client_manager_id
        LEFT JOIN `user` AS principalUser ON principalUser.id = assets.principal_id
        LEFT JOIN `user` AS claimUser ON claimUser.id = claim.claim_user_id
        LEFT JOIN assets_production AS prod ON assets.id = prod.business_id AND prodFund.production_no = prod.production_no
        WHERE order_fund.business_type = 'ASSET_BUSINESS'
        <if test="dto != null and dto.orderId != null and dto.orderId != ''">
            AND assets.order_id = #{dto.orderId}
        </if>
        <if test="dto != null and dto.reportNo != null and dto.reportNo != ''">
            AND prod.production_no = #{dto.reportNo}
        </if>
        <if test="dto != null and dto.principalId != null and dto.principalId != ''">
            AND assets.principal_id = #{dto.principalId}
        </if>
        <if test="dto != null and dto.claimUserId != null and dto.claimUserId != ''">
            AND claim.claim_user_id = #{dto.claimUserId}
        </if>
        <if test="dto != null and dto.claimAmount != null and dto.claimAmount != ''">
            AND claim.claim_amount = #{dto.claimAmount}
        </if>
        <if test="dto != null and dto.assetsBusinessGener != null and dto.assetsBusinessGener != ''">
            AND assets.assets_business_gener = #{dto.assetsBusinessGener}
        </if>
        <if test="dto != null and dto.ifSaveFile != null and dto.ifSaveFile != ''">
            AND prod.if_save_file = #{dto.ifSaveFile}
        </if>
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (claim.claim_datetime BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        ORDER BY claim.claim_datetime DESC
    </sql>

    <!--查询资产实收款统计-->
    <select id="assetsSate" parameterType="com.dayou.dto.RealFundAssetsStatDTO" resultType="com.dayou.vo.RealFundAssetsStatVO">
        <include refid="assetsStatQuery" />
    </select>

    <!--资产实收款统计导出-->
    <select id="assetsSateExport" resultType="com.dayou.vo.RealFundAssetsStatVO">
        <include refid="assetsStatQuery" />
    </select>

</mapper>
