<?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.FinanceRealFundMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.FinanceRealFund">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="modified" property="modified" />
        <result column="created" property="created" />
        <result column="payer" property="payer" />
        <result column="payer_bank" property="payerBank" />
        <result column="payer_account" property="payerAccount" />
        <result column="amount" property="amount" />
        <result column="pay_datetime" property="payDatetime" />
        <result column="remark" property="remark" />
        <result column="creator_id" property="creatorId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        modified,
        created,
        payer, payer_bank, payer_account, amount, pay_datetime, remark, creator_id
    </sql>

    <sql id="pageListSql">
        SELECT
        frf.id,
        frf.created,
        frf.payer,
        frf.payer_bank,
        frf.payer_account,
        frf.amount,
        frf.pay_datetime,
        frf.remark,
        frf.creator_id,
        u.name AS creator,
        ifnull(t.ca,0) AS claimAmount,
        ( frf.amount - ifnull(t.ca,0) ) AS notClaimAmount
        FROM
        finance_real_fund frf
        LEFT JOIN user u ON u.id = frf.creator_id
        LEFT JOIN ( SELECT real_fund_id, sum(ifnull(claim_amount,0)) ca FROM finance_claim WHERE deleted = 0 GROUP BY real_fund_id ) t ON t.real_fund_id = frf.id
        WHERE
        frf.deleted = 0
        <if test="vo!=null and vo.notFinished">
            and  ifnull(t.ca,0) &lt; frf.amount
        </if>
        <if test="vo!=null and !vo.notFinished">
            and  ifnull(t.ca,0) &gt;= frf.amount
        </if>
        <if test="keyword!=null and keyword!='' ">
            and (
            frf.payer like concat ('%',#{keyword},'%')
            or frf.payer_bank like concat('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            or frf.payer_account like concat('%',#{keyword},'%')
            or frf.amount like concat('%',#{keyword},'%')
            or frf.remark like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.startDate!=null and vo.startDate!=''">
            and frf.pay_datetime &gt;= #{vo.startDate}
        </if>
        <if test="vo!=null and vo.endDate!=null and vo.endDate!=''">
            and frf.pay_datetime &lt;= #{vo.endDate}
        </if>
        <if test="vo!=null and vo.xStartDate!=null and vo.xStartDate!=''">
            and frf.created &gt;= #{vo.xStartDate}
        </if>
        <if test="vo!=null and vo.xEndDate!=null and vo.xEndDate!=''">
            and frf.created &lt;= #{vo.xEndDate}
        </if>
    </sql>

    <sql id="pageListSql1">
        SELECT
        #{userId} as userId,
        frf.id,
        frf.created,
        frf.payer,
        frf.payer_bank,
        frf.payer_account,
        frf.amount,
        frf.pay_datetime,
        frf.remark,
        frf.creator_id,
        u.name AS creator,
        ifnull(t.ca,0) AS claimAmount,
        ( frf.amount - ifnull(t.ca,0) ) AS notClaimAmount
        FROM
        finance_real_fund frf
        LEFT JOIN user u ON u.id = frf.creator_id
        LEFT JOIN ( SELECT real_fund_id, sum(ifnull(claim_amount,0)) ca FROM finance_claim WHERE deleted = 0 GROUP BY real_fund_id ) t ON t.real_fund_id = frf.id
        WHERE
        frf.deleted = 0
        <if test="vo!=null and vo.notFinished">
            and  ifnull(t.ca,0) &lt; frf.amount
        </if>
        <if test="vo!=null and !vo.notFinished">
            and  ifnull(t.ca,0) &gt;= frf.amount
        </if>
        <if test="keyword!=null and keyword!='' ">
            and (
            frf.payer like concat ('%',#{keyword},'%')
            or frf.payer_bank like concat('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            or frf.payer_account like concat('%',#{keyword},'%')
            or frf.amount like concat('%',#{keyword},'%')
            or frf.remark like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.startDate!=null and vo.startDate!=''">
            and frf.pay_datetime &gt;= #{vo.startDate}
        </if>
        <if test="vo!=null and vo.endDate!=null and vo.endDate!=''">
            and frf.pay_datetime &lt;= #{vo.endDate}
        </if>
        <if test="vo!=null and vo.xStartDate!=null and vo.xStartDate!=''">
            and frf.created &gt;= #{vo.xStartDate}
        </if>
        <if test="vo!=null and vo.xEndDate!=null and vo.xEndDate!=''">
            and frf.created &lt;= #{vo.xEndDate}
        </if>
        ORDER BY
        ifnull(t.ca,0) ASC ,frf.created DESC
    </sql>

    <resultMap id="realAmountClaimMap" type="com.dayou.vo.FinanceRealFundVO">
        <result column="userId" property="userId" />
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="payer" property="payer" />
        <result column="payer_bank" property="payerBank" />
        <result column="payer_account" property="payerAccount" />
        <result column="amount" property="amount" />
        <result column="pay_datetime" property="payDatetime" />
        <result column="remark" property="remark" />
        <result column="creator_id" property="creatorId" />
        <result column="creator" property="creator" />
        <result column="claimAmount" property="claimAmount" />
        <result column="notClaimAmount" property="notClaimAmount" />
        <collection property="recommends" select="findRecommends" ofType="java.lang.String" column="{amount=amount,userId=userId}" />
     </resultMap>

    <select id="getPage" parameterType="com.dayou.entity.FinanceRealFund" resultType="com.dayou.vo.FinanceRealFundVO">
        <include refid="pageListSql" />
        ORDER BY
        ifnull(t.ca,0) ASC ,frf.created DESC
    </select>

    <select id="claimPage" resultMap="realAmountClaimMap">
        SELECT
        #{userId} as userId,
        frf.id,
        frf.created,
        frf.payer,
        frf.payer_bank,
        frf.payer_account,
        frf.amount,
        frf.pay_datetime,
        frf.remark,
        frf.creator_id,
        u.name AS creator,
        ifnull(t.ca,0) AS claimAmount,
        ( frf.amount - ifnull(t.ca,0) ) AS notClaimAmount
        FROM
        finance_real_fund frf
        LEFT JOIN user u ON u.id = frf.creator_id
        LEFT JOIN ( SELECT real_fund_id, sum(ifnull(claim_amount,0)) ca FROM finance_claim WHERE deleted = 0 GROUP BY real_fund_id ) t ON t.real_fund_id = frf.id
        WHERE
        frf.deleted = 0
        <if test="vo!=null and vo.notFinished">
            and  ifnull(t.ca,0) &lt; frf.amount
        </if>
        <if test="vo!=null and !vo.notFinished">
            and  ifnull(t.ca,0) &gt;= frf.amount
        </if>
        <if test="keyword!=null and keyword!='' ">
            and (
            frf.payer like concat ('%',#{keyword},'%')
            or frf.payer_bank like concat('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            or frf.payer_account like concat('%',#{keyword},'%')
            or frf.amount like concat('%',#{keyword},'%')
            or frf.remark like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.startDate!=null and vo.startDate!=''">
            and frf.pay_datetime &gt;= #{vo.startDate}
        </if>
        <if test="vo!=null and vo.endDate!=null and vo.endDate!=''">
            and frf.pay_datetime &lt;= #{vo.endDate}
        </if>
        <if test="vo!=null and vo.xStartDate!=null and vo.xStartDate!=''">
            and frf.created &gt;= #{vo.xStartDate}
        </if>
        <if test="vo!=null and vo.xEndDate!=null and vo.xEndDate!=''">
            and frf.created &lt;= #{vo.xEndDate}
        </if>
        ORDER BY
        frf.created DESC
    </select>

    <select id="findRecommends"  resultType="java.lang.String">
        SELECT
            tt1.business_type
        FROM
            (
                SELECT
                    order_fund.real_amount,
                    order_fund.business_type,
                    temp.invoiceAmount
                FROM
                    (
                        SELECT
                            order_fund_id,
                            sum( real_amount ) AS invoiceAmount
                        FROM
                            finance_invoice
                        WHERE
                            state = '已开票'
                          AND deleted = 0
                          AND production_fund_id IS NULL
                          <if test="userId!=null">
                              and apply_id = #{userId}
                          </if>
                        GROUP BY
                            order_fund_id UNION ALL
                        SELECT
                            order_fund_id,
                            sum( real_amount ) AS invoiceAmount
                        FROM
                            finance_invoice
                        WHERE
                            state = '已开票'
                          AND deleted = 0
                          AND production_fund_id IS NOT NULL
                        <if test="userId!=null">
                            and apply_id = #{userId}
                        </if>
                        GROUP BY
                            production_fund_id
                    ) temp
                        LEFT JOIN order_fund ON order_fund.id = temp.order_fund_id
            ) tt1
        WHERE
            tt1.invoiceAmount = #{amount}
        AND tt1.real_amount IS NULL
    </select>

    <select id="getNotClaimAmount" parameterType="java.lang.Long" resultType="java.math.BigDecimal">
        SELECT ( frf.amount -( SELECT ifnull( sum( claim_amount ), 0 ) AS ca FROM finance_claim WHERE deleted = 0 AND real_fund_id = #{realFundId} )
                   ) AS notClaimAmount
        FROM
            finance_real_fund frf
        WHERE
            frf.id = #{realFundId}
            AND frf.deleted = 0
    </select>

    <sql id="majorOverdueSql">
        SELECT
        DATEDIFF(
        CURDATE(),
        DATE( mp.repertory_out_time )) AS overdueDay,
        pf.production,
        m.allot_type,
        m.order_id,
        m.business_object_type,
        u.NAME AS clientManager,
        u1.NAME AS principal,
        cc1.NAME AS clientName,
        cc2.NAME AS clientSubName,
        pf.production_should_amount as shouldAmount,
        mp.*
        FROM
        ( SELECT business_id, production_no,
        ( CASE production_type WHEN 'STATEMENT' THEN '意见书' WHEN 'REPORT' THEN '报告' WHEN 'LETTER' THEN '复评函' ELSE '' END ) production,
        production_type
        , production_should_amount, real_amount FROM production_fund WHERE business_type = 'MAJOR_BUSINESS'
        AND deleted = 0 and production_should_amount is not null and production_should_amount!=0  and real_amount is null)  pf
        inner join
        (
        SELECT
        id,
        major_id,
        report_no,
        production as productionType,
        NAME,
        evaluate_amount,
        repertory_out_time,
        standard_amount
        FROM
        major_production mp
        WHERE
        deleted = 0
        AND repertory_out_time IS NOT NULL
        ) mp
        ON ( mp.major_id = pf.business_id AND mp.report_no = pf.production_no and mp.productionType = pf.production_type )

        LEFT JOIN major m ON m.id = mp.major_id
        LEFT JOIN user u ON u.id = m.client_manager_id
        LEFT JOIN user u1 ON u1.id = m.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        <where>
            <if test="overdueVO!=null and overdueVO.overdueMin!=null">
                AND DATEDIFF(CURDATE(),DATE( mp.repertory_out_time )) >= #{overdueVO.overdueMin}
            </if>
            <if test="overdueVO!=null and overdueVO.overdueMax!=null">
                AND DATEDIFF(CURDATE(),DATE( mp.repertory_out_time )) &lt;= #{overdueVO.overdueMax}
            </if>
            <if test="overdueVO!=null and overdueVO.orderId!=null and overdueVO.orderId!=''">
                and m.order_id = #{overdueVO.orderId}
            </if>
            <if test="overdueVO!=null and overdueVO.reportNo!=null and overdueVO.reportNo!=''">
                and mp.report_no like concat ('%',#{overdueVO.reportNo},'%')
            </if>
            <if test="overdueVO!=null and overdueVO.principal!=null and overdueVO.principal!=''">
                and u1.NAME like concat ('%',#{overdueVO.principal},'%')
            </if>
            <if test="overdueVO!=null and overdueVO.userIds!=null and overdueVO.userIds.size!=0">
                and m.client_manager_id in
                <foreach collection="overdueVO.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        </where>
        order by overdueDay ASC
    </sql>
    <select id="overdueMajor" parameterType="com.dayou.vo.MajorProdOverdueVO" resultType="com.dayou.vo.MajorProdOverdueVO">
        <include refid="majorOverdueSql" />
    </select>

    <select id="overdueMajorExport" parameterType="com.dayou.vo.MajorProdOverdueVO" resultType="com.dayou.vo.MajorProdOverdueVO">
        <include refid="majorOverdueSql" />
    </select>

    <resultMap id="settleeMajorProductionVOMap" type="com.dayou.vo.SettleMajorProductionVO">
        <id property="id" column="id" />
        <result property="id" column="id" />
        <result property="claimAmount" column="claim_amount" />
        <result property="orderId" column="order_id" />
        <result property="reportNo" column="report_no" />
        <result property="claimDate" column="claimDate" />
        <result property="saveFileDate" column="save_file_date" />
        <result property="settleDate" column="settleDate" />
        <result property="deliveryDate" column="deliveryDate" />
        <result property="name" column="name" />
        <result property="bailor" column="bailor" />
        <result property="evaluateAmount" column="evaluate_amount" />
        <result property="principal" column="principal" />
        <result property="clientManager" column="clientManager" />
        <result property="clientName" column="clientName" />
        <result property="clientSubName" column="clientSubName" />
        <result property="evaluateDepartmentName" column="evaluateDepartmentName" />
        <result property="marketDepartmentName" column="marketDepartmentName" />
        <result property="payDateTime" column="pay_datetime" />
<!--        <collection property="invoices" ofType="com.dayou.vo.SettleProInvoiceVo">-->
<!--            <result property="invoiceDate" column="invoiceDate" />-->
<!--            <result property="invoiceUser" column="invoiceUser" />-->
<!--            <result property="invoiceNo" column="invoiceNo" />-->
<!--        </collection>-->
    </resultMap>

    <sql id="majorEvaluatorSettleSql">
        SELECT
        fc.id,
        fc.claim_amount,
        m.order_id,
        mp.report_no,
        fc.claim_datetime as claimDate,
        mp.save_file_date,
        IF
        ( fc.claim_datetime > mp.save_file_date, fc.claim_datetime, mp.save_file_date ) AS settleDate,
        mp.name,
        m.bailor,
        mp.evaluate_amount,
        u.name AS principal,
        u1.name AS clientManager,
        cc.name AS clientName,
        cc1.name AS clientSubName,
        d.name AS evaluateDepartmentName,
        (
        SELECT
        GROUP_CONCAT( name )
        FROM
        department
        WHERE
        id IN (
        SELECT
        department_id
        FROM
        post
        WHERE
        id IN ( SELECT post_id FROM user_post WHERE user_id = m.client_manager_id ))) AS marketDepartmentName,
        frf.pay_datetime
        FROM
        (
        SELECT
        id,
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime,
        real_fund_id
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL

        ) fc
        INNER JOIN ( SELECT id, pay_datetime FROM finance_real_fund WHERE deleted = 0
        <if test="settleVO!=null and settleVO.payStartDate!=null and settleVO.payStartDate!='' ">
            AND pay_datetime &gt;= #{settleVO.payStartDate}
        </if>
        <if test="settleVO!=null and settleVO.payEndDate!=null and settleVO.payEndDate!='' ">
            AND pay_datetime &lt;= #{settleVO.payEndDate}
        </if>
        ) frf ON frf.id = fc.real_fund_id
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
        INNER JOIN (
        SELECT
        id,
        major_id,
        NAME,
        report_no,
        production,
        evaluate_amount,
        save_file_date,
        fund.real_amount
        FROM
        major_production mpr
        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
        WHERE
        fund.real_amount IS NOT NULL
        AND mpr.save_file_date IS NOT NULL
        ) mp ON mp.report_no = pf.production_no
        LEFT JOIN major m ON m.id = mp.major_id
        LEFT JOIN user u ON u.id = m.principal_id
        LEFT JOIN user u1 ON u1.id = m.client_manager_id
        LEFT JOIN customer_company cc ON cc.id = m.clientele_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_sub_id
        LEFT JOIN department d ON d.id = m.department_id
        <where>
            <if test="keyword!=null and keyword!='' ">
                and (
                mp.report_no like concat ('%',#{keyword},'%')
                or fc.claim_amount =#{keyword}
                or m.order_id like concat('%',#{keyword},'%')
                or u.name like concat('%',#{keyword},'%')
                or u1.name like concat('%',#{keyword},'%')
                )
            </if>
            <if test="settleVO!=null and settleVO.startDate!=null and settleVO.startDate!='' ">
                AND (select IF
                ( fc.claim_datetime > mp.save_file_date, fc.claim_datetime, mp.save_file_date )) &gt;= #{settleVO.startDate}
            </if>
            <if test="settleVO!=null and settleVO.endDate!=null and settleVO.endDate!='' ">
                AND (select IF
                ( fc.claim_datetime > mp.save_file_date, fc.claim_datetime, mp.save_file_date )) &lt;= #{settleVO.endDate}
            </if>
        </where>
        order by fc.claim_datetime ASC
    </sql>
    <select id="majorEvaluatorSettleProd" parameterType="com.dayou.vo.SettleMajorProductionVO" resultMap="settleeMajorProductionVOMap">
       <include refid="majorEvaluatorSettleSql" />
    </select>

    <select id="majorEvaluatorSettleExport" parameterType="com.dayou.vo.SettleMajorProductionVO" resultMap="settleeMajorProductionVOMap">
        <include refid="majorEvaluatorSettleSql" />
    </select>

    <sql id="majorMarketerSettleSql">
        SELECT
        fc.id,
        fc.claim_amount,
        m.order_id,
        mp.report_no,
        fc.claim_datetime AS claimDate,
        mp.delivery_date AS deliveryDate,
        IF
        ( fc.claim_datetime > mp.delivery_date, fc.claim_datetime, mp.delivery_date ) AS settleDate,
        mp.NAME,
        m.bailor,
        mp.evaluate_amount,
        u.NAME AS principal,
        u1.NAME AS clientManager,
        cc.NAME AS clientName,
        cc1.NAME AS clientSubName,
        d.NAME AS evaluateDepartmentName,
        (
        SELECT
        GROUP_CONCAT( NAME )
        FROM
        department
        WHERE
        id IN (
        SELECT
        department_id
        FROM
        post
        WHERE
        id IN ( SELECT post_id FROM user_post WHERE user_id = m.client_manager_id ))) AS marketDepartmentName,
        frf.pay_datetime
        FROM
        (
        SELECT
        id,
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime,
        real_fund_id
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        ) fc
        INNER JOIN ( SELECT id, pay_datetime FROM finance_real_fund WHERE deleted = 0
        <if test="settleVO!=null and settleVO.payStartDate!=null and settleVO.payStartDate!='' ">
            AND pay_datetime &gt;= #{settleVO.payStartDate}
        </if>
        <if test="settleVO!=null and settleVO.payEndDate!=null and settleVO.payEndDate!='' ">
            AND pay_datetime &lt;= #{settleVO.payEndDate}
        </if>
        ) frf ON frf.id = fc.real_fund_id
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
        INNER JOIN (
        SELECT
        id,
        major_id,
        NAME,
        report_no,
        production,
        evaluate_amount,
        delivery_date,
        fund.real_amount
        FROM
        major_production mpr
        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
        WHERE
        fund.real_amount IS NOT NULL
        AND mpr.delivery_date IS NOT NULL
        ) mp ON mp.report_no = pf.production_no
        LEFT JOIN major m ON m.id = mp.major_id
        LEFT JOIN user u ON u.id = m.principal_id
        LEFT JOIN user u1 ON u1.id = m.client_manager_id
        LEFT JOIN customer_company cc ON cc.id = m.clientele_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_sub_id
        LEFT JOIN department d ON d.id = m.department_id
        <where>
            <if test="keyword!=null and keyword!='' ">
                and (
                mp.report_no like concat ('%',#{keyword},'%')
                or fc.claim_amount =#{keyword}
                or m.order_id like concat('%',#{keyword},'%')
                or u.name like concat('%',#{keyword},'%')
                or u1.name like concat('%',#{keyword},'%')
                )
            </if>
            <if test="settleVO!=null and settleVO.startDate!=null and settleVO.startDate!='' ">
                AND  (select IF
                ( fc.claim_datetime > mp.delivery_date, fc.claim_datetime, mp.delivery_date )) &gt;= #{settleVO.startDate}
            </if>
            <if test="settleVO!=null and settleVO.endDate!=null and settleVO.endDate!='' ">
                AND  (select IF
                ( fc.claim_datetime > mp.delivery_date, fc.claim_datetime, mp.delivery_date )) &lt;= #{settleVO.endDate}
            </if>
        </where>
            order by fc.claim_datetime ASC
    </sql>
    <select id="majorMarketerSettleProd" parameterType="com.dayou.vo.SettleMajorProductionVO" resultMap="settleeMajorProductionVOMap">
        <include refid="majorMarketerSettleSql" />
    </select>

    <select id="majorMarketSettleExport" parameterType="com.dayou.vo.SettleMajorProductionVO" resultMap="settleeMajorProductionVOMap">
        <include refid="majorMarketerSettleSql" />
    </select>

    <select id="majorEvaluatorCommission" parameterType="com.dayou.vo.UserCommissionTotalAmountVO" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT mpa.user_id as id, u.name as userName, sum(( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio )) AS commissionTotalAmount
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}

            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN (
        SELECT
        id,
        major_id,
        NAME,
        report_no,
        production,
        evaluate_amount,
        save_file_date,
        fund.real_amount
        FROM
        major_production mpr
        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
        WHERE
        fund.real_amount IS NOT NULL
        AND mpr.save_file_date IS NOT NULL
        ) mp ON mp.report_no = pf.production_no
                LEFT JOIN major m ON m.id = mp.major_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                        cd.business_id = m.id
                    AND  cd.production_id = mp.id )
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT major_id, major_production_id, user_id, ratio FROM major_production_allot WHERE deleted = 0 AND user_type = 'EVALUATE'

                ) mpa ON ( mpa.major_id = m.id AND mpa.major_production_id = mp.id)
                LEFT JOIN user u ON u.id = mpa.user_id
        <where>
            <if test="vo!=null and vo.userIds!=null and vo.userIds.size!=0">
                mpa.user_id in
                <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        </where>

        GROUP BY
            mpa.user_id
            having 1=1
            <if test="vo!=null and vo.userName!=null and vo.userName!=''">
                and u.name like concat ('%',#{vo.userName},'%')
            </if>
        order by commissionTotalAmount DESC
    </select>

    <select id="majorEvaluatorCommissionDetail" parameterType="com.dayou.vo.UserCommissionProdVO" resultType="com.dayou.vo.UserCommissionProdVO">
        SELECT
            mp.id,
            fc.claim_amount as realAmount,
            mp.report_no,
            IF
                ( fc.claim_datetime > mp.save_file_date, fc.claim_datetime, mp.save_file_date ) AS settleDate,
            mp.NAME,
            mp.created,
            dd.NAME AS businessCate,
            bcr.min_ratio AS ratio,
            mpa.user_id,
            mpa.ratio AS allotRatio,
            ( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio ) AS commissionAmount,
            ( fc.claim_amount * ( bcr.min_ratio / 100 ) ) AS totalAmount,
            u.name as userName
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN (
                SELECT
                    id,
                    major_id,
                    NAME,
                    report_no,
                    production,
                    evaluate_amount,
                    save_file_date,
                    fund.real_amount,
                    created
                FROM
                    major_production mpr
                        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
                WHERE
                    fund.real_amount IS NOT NULL
                  AND mpr.save_file_date IS NOT NULL
            ) mp ON mp.report_no = pf.production_no
                LEFT JOIN major m ON m.id = mp.major_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                        cd.business_id = m.id
                    AND  cd.production_id = mp.id )
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT major_id, major_production_id, user_id, ratio FROM major_production_allot WHERE deleted = 0 AND user_type = 'EVALUATE' ) mpa ON ( mpa.major_id = m.id AND mpa.major_production_id = mp.id)
                LEFT JOIN user u on u.id = mpa.user_id
        WHERE
            mpa.user_id = #{vo.userId}
        order by settleDate DESC
    </select>

    <select id="majorEvaluatorTotalAmountExport" parameterType="com.dayou.vo.UserCommissionTotalAmountVO" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT mpa.user_id as id, u.name as userName, ROUND(sum(( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio )),2) AS commissionTotalAmount,
               '评估人员' as userType,
               '大中型业务' as businessType,
               #{vo.startDate} as startDate,
                #{vo.endDate} as endDate
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN (
                SELECT
                    id,
                    major_id,
                    NAME,
                    report_no,
                    production,
                    evaluate_amount,
                    save_file_date,
                    fund.real_amount
                FROM
                    major_production mpr
                        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
                WHERE
                    fund.real_amount IS NOT NULL
                  AND mpr.save_file_date IS NOT NULL
            ) mp ON mp.report_no = pf.production_no
                LEFT JOIN major m ON m.id = mp.major_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                        cd.business_id = m.id
                    AND  cd.production_id = mp.id )
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT major_id, major_production_id, user_id, ratio FROM major_production_allot WHERE deleted = 0 AND user_type = 'EVALUATE'

            ) mpa ON ( mpa.major_id = m.id AND mpa.major_production_id = mp.id)
                LEFT JOIN user u ON u.id = mpa.user_id
        GROUP BY
            mpa.user_id
    </select>

    <select id="evaluatorUserDetailExport" parameterType="com.dayou.vo.UserCommissionProdVO" resultType="com.dayou.vo.UserCommissionProdVO">
        SELECT
            mp.id,
        ROUND(fc.claim_amount,2) as realAmount,
            mp.report_no,
            IF
                ( fc.claim_datetime > mp.save_file_date, fc.claim_datetime, mp.save_file_date ) AS settleDate,
            mp.NAME,
            mp.created,
            dd.NAME AS businessCate,
        ROUND(bcr.min_ratio,2) AS ratio,
            mpa.user_id,
        ROUND(mpa.ratio*100,2) AS allotRatio,
        ROUND(( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio ),2) AS commissionAmount,
        ROUND(( fc.claim_amount * ( bcr.min_ratio / 100 ) ),2) AS totalAmount,
            u.name as userName
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN (
        SELECT
        id,
        major_id,
        NAME,
        report_no,
        production,
        evaluate_amount,
        save_file_date,
        fund.real_amount,
        created
        FROM
        major_production mpr
        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
        WHERE
        fund.real_amount IS NOT NULL
        AND mpr.save_file_date IS NOT NULL
        ) mp ON mp.report_no = pf.production_no
                LEFT JOIN major m ON m.id = mp.major_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                        cd.business_id = m.id
                    AND cd.production_id = mp.id )
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT major_id, major_production_id, user_id, ratio FROM major_production_allot WHERE deleted = 0 AND user_type = 'EVALUATE' ) mpa ON ( mpa.major_id = m.id AND mpa.major_production_id = mp.id)
                LEFT JOIN user u on u.id = mpa.user_id
            <where>
                <if test="vo!=null and vo.userId!=null">
                   and mpa.user_id = #{vo.userId}
                </if>
            </where>
        order by mpa.user_id
    </select>

    <select id="majorMarketCommission" parameterType="com.dayou.vo.UserCommissionTotalAmountVO" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT mpa.user_id as id, u.name as userName, sum(( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio )) AS commissionTotalAmount
        FROM
        (
        SELECT
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        AND claim_datetime &gt;= #{vo.startDate}
        AND claim_datetime  &lt;= #{vo.endDate}
        ) fc
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
        INNER JOIN (
        SELECT
        id,
        major_id,
        NAME,
        report_no,
        production,
        evaluate_amount,
        delivery_date,
        fund.real_amount
        FROM
        major_production mpr
        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
        WHERE
        fund.real_amount IS NOT NULL
        AND mpr.delivery_date IS NOT NULL
        ) mp ON mp.report_no = pf.production_no
        LEFT JOIN major m ON m.id = mp.major_id
        INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
        cd.business_id = m.id
        AND cd.production_id = mp.id )
        LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
        LEFT JOIN ( SELECT major_id, major_production_id,declare_id, user_id, ratio FROM major_production_allot WHERE deleted = 0 AND user_type = 'MARKET'

        ) mpa ON ( mpa.major_id = m.id AND cd.id = mpa.declare_id AND (mpa.major_production_id is null or mpa.major_production_id = mp.id ))
        LEFT JOIN user u ON u.id = mpa.user_id
        <where>
            <if test="vo!=null and vo.userIds!=null and vo.userIds.size!=0">
                mpa.user_id in
                <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        </where>
        GROUP BY
        mpa.user_id
        having 1=1
        <if test="vo!=null and vo.userName!=null and vo.userName!=''">
            and u.name like concat ('%',#{vo.userName},'%')
        </if>
        order by commissionTotalAmount DESC
    </select>

    <select id="majorMarketCommissionDetail" parameterType="com.dayou.vo.UserCommissionProdVO" resultType="com.dayou.vo.UserCommissionProdVO">
        SELECT
            mp.id,
            fc.claim_amount as realAmount,
            mp.report_no,
            IF
                ( fc.claim_datetime > mp.delivery_date, fc.claim_datetime, mp.delivery_date ) AS settleDate,
            mp.NAME,
            mp.created,
            dd.NAME AS businessCate,
            bcr.min_ratio AS ratio,
            mpa.user_id,
            mpa.ratio AS allotRatio,
            ( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio ) AS commissionAmount,
            ( fc.claim_amount * ( bcr.min_ratio / 100 ) ) AS totalAmount,
            u.name as userName
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN (
                SELECT
                    id,
                    major_id,
                    NAME,
                    report_no,
                    production,
                    evaluate_amount,
                    delivery_date,
                    fund.real_amount,
                    created
                FROM
                    major_production mpr
                        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
                WHERE
                    fund.real_amount IS NOT NULL
                  AND mpr.delivery_date IS NOT NULL

            ) mp ON mp.report_no = pf.production_no
                LEFT JOIN major m ON m.id = mp.major_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                        cd.business_id = m.id
                    AND  cd.production_id = mp.id )
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT major_id, major_production_id, user_id, ratio,declare_id FROM major_production_allot WHERE deleted = 0 AND user_type = 'MARKET' ) mpa  ON ( mpa.major_id = m.id AND cd.id = mpa.declare_id AND (mpa.major_production_id is null or mpa.major_production_id = mp.id ))
                LEFT JOIN user u on u.id = mpa.user_id
        WHERE
            mpa.user_id = #{vo.userId}
        order by settleDate DESC
    </select>

    <select id="majorMarketTotalAmountExport" parameterType="com.dayou.vo.UserCommissionTotalAmountVO" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT mpa.user_id as id, u.name as userName, ROUND(sum(( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio )),2) AS commissionTotalAmount,
               '市场人员' as userType,
               '大中型业务' as businessType,
               #{vo.startDate} as startDate,
               #{vo.endDate} as endDate
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN (
                SELECT
                    id,
                    major_id,
                    NAME,
                    report_no,
                    production,
                    evaluate_amount,
                    delivery_date,
                    fund.real_amount
                FROM
                    major_production mpr
                        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
                WHERE
                    fund.real_amount IS NOT NULL
                  AND mpr.delivery_date IS NOT NULL
            ) mp ON mp.report_no = pf.production_no
                LEFT JOIN major m ON m.id = mp.major_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                        cd.business_id = m.id
                    AND  cd.production_id = mp.id )
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT major_id, major_production_id,declare_id, user_id, ratio FROM major_production_allot WHERE deleted = 0 AND user_type = 'MARKET'

            ) mpa ON ( mpa.major_id = m.id AND cd.id = mpa.declare_id AND (mpa.major_production_id is null or mpa.major_production_id = mp.id ))
                LEFT JOIN user u ON u.id = mpa.user_id

        GROUP BY
            mpa.user_id
        order by commissionTotalAmount DESC
    </select>

    <select id="marketUserDetailExport" parameterType="com.dayou.vo.UserCommissionProdVO" resultType="com.dayou.vo.UserCommissionProdVO">
        SELECT
        mp.id,
        ROUND(fc.claim_amount,2) as realAmount,
        mp.report_no,
        IF
        ( fc.claim_datetime > mp.delivery_date, fc.claim_datetime, mp.delivery_date ) AS settleDate,
        mp.NAME,
        mp.created,
        dd.NAME AS businessCate,
        ROUND(bcr.min_ratio,2) AS ratio,
        mpa.user_id,
        ROUND(mpa.ratio*100,2) AS allotRatio,
        ROUND(( fc.claim_amount * ( bcr.min_ratio / 100 )* mpa.ratio ),2) AS commissionAmount,
        ROUND(( fc.claim_amount * ( bcr.min_ratio / 100 ) ),2) AS totalAmount,
        u.name as userName
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN (
        SELECT
        id,
        major_id,
        NAME,
        report_no,
        production,
        evaluate_amount,
        delivery_date,
        fund.real_amount,
        created
        FROM
        major_production mpr
        INNER JOIN ( SELECT production_no, real_amount, production_type FROM production_fund WHERE deleted = 0 AND business_type = 'MAJOR_BUSINESS' ) fund ON ( fund.production_no = mpr.report_no AND fund.production_type = mpr.production )
        WHERE
        fund.real_amount IS NOT NULL
        AND mpr.delivery_date IS NOT NULL
        ) mp ON mp.report_no = pf.production_no
                LEFT JOIN major m ON m.id = mp.major_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                        cd.business_id = m.id
                    AND cd.production_id = mp.id )
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT major_id, major_production_id, user_id, ratio,declare_id FROM major_production_allot WHERE deleted = 0 AND user_type = 'MARKET' ) mpa  ON ( mpa.major_id = m.id AND cd.id = mpa.declare_id AND (mpa.major_production_id is null or mpa.major_production_id = mp.id ))
                LEFT JOIN user u on u.id = mpa.user_id
        <where>
            <if test="vo!=null and vo.userId!=null">
                and mpa.user_id = #{vo.userId}
            </if>
        </where>
        order by mpa.user_id
    </select>

    <sql id="personalOverdueSql">
        SELECT
        DATEDIFF(
        CURDATE(),
        DATE ( pp.rot )) AS overdueDay,
        p.*,
        pt.acreage,
        pt.price,
        pt.amount,
        cc1.NAME AS clientName,
        cc2.NAME AS clientSubName,
        cl1.NAME AS contactName,
        cl1.mobile AS contactTel,
        u.NAME AS clientManager,
        u1.NAME AS outwardStaffName,
        u2.NAME AS inwardStaffName,
        u3.NAME AS pricingStaffName,
        pp.rot
        FROM
        (
        SELECT
        id,
        location,
        order_id,
        agent,
        bailorA,
        bailorB,
        clientele_id,
        clientele_sub_id,
        clientele_contact_id,
        client_manager_id,
        outward_staff,
        inward_staff,
        pricing_staff
        FROM
        personal
        WHERE
        deleted = 0
        ) p
        inner JOIN personal_target pt ON pt.personal_id = p.id
        inner JOIN ( SELECT business_id, should_amount, real_amount FROM order_fund WHERE business_type = 'PERSONAL_BUSINESS' AND deleted = 0 AND real_amount IS NULL ) ofu ON ofu.business_id = p.id
        inner JOIN ( SELECT target_id,( repertory_out_time ) rot FROM personal_production WHERE repertory_out_time IS NOT NULL GROUP BY target_id ) pp ON pp.target_id = pt.id
        LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = p.clientele_sub_id
        LEFT JOIN customer_linkman cl1 ON cl1.id = p.clientele_contact_id
        LEFT JOIN `user` u ON u.id = p.client_manager_id
        LEFT JOIN `user` u1 ON u1.id = p.outward_staff
        LEFT JOIN `user` u2 ON u2.id = p.inward_staff
        LEFT JOIN `user` u3 ON u3.id = p.pricing_staff
        <where>
            <if test="overdueVO!=null and overdueVO.overdueMin!=null">
                AND DATEDIFF(CURDATE(),DATE ( pp.rot )) >= #{overdueVO.overdueMin}
            </if>
            <if test="overdueVO!=null and overdueVO.overdueMax!=null">
                AND DATEDIFF(CURDATE(),DATE( pp.rot )) &lt;= #{overdueVO.overdueMax}
            </if>
            <if test="overdueVO!=null and overdueVO.orderId!=null and overdueVO.orderId!=''">
                and p.order_id = #{overdueVO.orderId}
            </if>
            <if test="overdueVO!=null and overdueVO.userIds!=null and overdueVO.userIds.size!=0">
                and p.client_manager_id in
                <foreach collection="overdueVO.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        </where>
        ORDER BY
        overdueDay ASC
    </sql>
    <select id="overduePersonal" parameterType="com.dayou.vo.PersonalOverdueVO" resultType="com.dayou.vo.PersonalOverdueVO">
       <include refid="personalOverdueSql" />
    </select>

    <select id="overduePersonalList" parameterType="com.dayou.vo.PersonalOverdueVO" resultType="com.dayou.vo.PersonalOverdueVO">
        <include refid="personalOverdueSql" />
    </select>
    <!--资产评估人员提成计算-->
    <select id="assetsEvaluatorCommission" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT bpd.member_id as id,
        u.name as userName,
        SUM(IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
        fc.claim_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100),
        cd.defined_low_limit_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100)
        )) AS allCommissionTotalAmount,
        SUM(IF(ap.save_file_date IS NOT NULL, (IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
        fc.claim_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100),
        cd.defined_low_limit_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100)
        )), 0)) AS commissionTotalAmount
        FROM
        (
        SELECT
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        AND claim_datetime &gt;= #{vo.startDate}
        AND claim_datetime &lt;= #{vo.endDate}
        UNION ALL
        SELECT
        allot_amount AS claim_amount,
        order_fund_id,
        production_fund_id,
        created AS claim_datetime
        FROM
        order_fund_allot
        WHERE
        deleted = 0
        AND created &gt;= #{vo.startDate}
        AND created &lt;= #{vo.endDate}
        ) fc
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type =
        'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id
        )
        INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, save_file_date, created FROM
        assets_production
        WHERE deleted = 0) ap ON ap.production_no = pf.production_no
        LEFT JOIN assets a ON a.id = ap.business_id
        INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id, ratio, defined_low_limit_amount FROM
        commission_declare WHERE
        business_type = 'COMMISSION_DECLARE_ASSET_EVALUATE' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
        cd.business_id = a.id
        AND ( cd.production_id IS NULL OR cd.production_id = ap.id ))
        LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
        LEFT JOIN ( SELECT business_id, report_no, member_id, performance_distribution FROM
        business_performance_distribution WHERE deleted = 0
        AND user_type = '评估人员'
        ) bpd ON ( bpd.business_id = a.id AND bpd.report_no = ap.production_no)
        LEFT JOIN user u ON u.id = bpd.member_id

        GROUP BY
        bpd.member_id
        having 1=1
        <if test="vo!=null and vo.userName!=null and vo.userName!=''">
            and u.name like concat ('%',#{vo.userName},'%')
        </if>
        order by commissionTotalAmount DESC
    </select>

    <!--资产评估人员提成计算详情-->
    <select id="assetsEvaluatorCommissionDetail" resultType="com.dayou.vo.UserCommissionProdAssetsVO">
        SELECT
            ap.id,
            fc.claim_amount as realAmount,
            ap.production_no AS reportNo,
            IF
            ( fc.claim_datetime > ap.save_file_date, fc.claim_datetime, ap.save_file_date ) AS settleDate,
            ap.assets_name AS name,
            ap.created,
            dd.name AS businessCate,
            IFNULL(cd.ratio,bcr.min_ratio) AS ratio,
            cd.defined_low_limit_amount AS lowLimitAmount,
            bpd.member_id,
            bpd.performance_distribution AS allotRatio,
            IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
               fc.claim_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100),
               cd.defined_low_limit_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100)
            ) AS commissionAmount,
            IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
               fc.claim_amount * (cd.ratio / 100),
               cd.defined_low_limit_amount * (cd.ratio / 100)
            ) AS totalAmount,
            u.name AS userName,
            pf.modified AS realFundDateTime
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
                UNION ALL
                SELECT
                    allot_amount AS claim_amount,
                    order_fund_id,
                    production_fund_id,
                    created AS claim_datetime
                FROM
                    order_fund_allot
                WHERE
                    deleted = 0
                  AND created  &gt;= #{vo.startDate}
                  AND created &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no,modified FROM production_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, save_file_date, created FROM assets_production WHERE deleted = 0) ap ON ap.production_no = pf.production_no
                LEFT JOIN assets a ON a.id = ap.business_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id, ratio,defined_low_limit_amount FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_ASSET_EVALUATE' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                cd.business_id = a.id
                    AND ( cd.production_id IS NULL OR cd.production_id = ap.id ))
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT business_id, report_no, member_id, performance_distribution FROM business_performance_distribution WHERE deleted = 0 AND user_type = '评估人员' ) bpd ON ( bpd.business_id = a.id AND bpd.report_no = ap.production_no)
                LEFT JOIN user u on u.id = bpd.member_id
        WHERE
            bpd.member_id = #{vo.userId}
        order by settleDate DESC
    </select>

    <!--资产评估人员合计导出-->
    <select id="assetsEvaluatorTotalAmountExport" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT bpd.member_id    as id,
               u.name           as userName,
               ROUND(SUM(IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
                            fc.claim_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100),
                            cd.defined_low_limit_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100)
                         )), 2) AS allCommissionTotalAmount,
               ROUND(SUM(IF(ap.save_file_date IS NOT NULL, (IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
                                                               fc.claim_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100),
                                                               cd.defined_low_limit_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100)
                                                            )), 0)), 2) AS commissionTotalAmount,
               '评估人员'       as userType,
               '资产业务'       as businessType,
               #{vo.startDate}  as startDate,
               #{vo.endDate}    as endDate
        FROM (SELECT claim_amount,
                     order_fund_id,
                     production_fund_id,
                     claim_datetime
              FROM finance_claim
              WHERE deleted = 0
                AND production_fund_id IS NOT NULL
                AND claim_datetime &gt;= #{vo.startDate}
                AND claim_datetime &lt;= #{vo.endDate}
              UNION ALL
              SELECT allot_amount AS claim_amount,
                     order_fund_id,
                     production_fund_id,
                     created      AS claim_datetime
              FROM order_fund_allot
              WHERE deleted = 0
                AND created &gt;= #{vo.startDate}
                AND created &lt;= #{vo.endDate}) fc
                 INNER JOIN (SELECT id, order_fund_id, business_id, production_no
                             FROM production_fund
                             WHERE business_type = 'ASSET_BUSINESS'
                               AND deleted = 0) pf
                            ON (pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id)
                 INNER JOIN (SELECT id,
                                    business_id,
                                    assets_name,
                                    production_no,
                                    production_type,
                                    save_file_date,
                                    created
                             FROM assets_production
                             WHERE deleted = 0) ap ON ap.production_no = pf.production_no
                 LEFT JOIN assets a ON a.id = ap.business_id
                 INNER JOIN (SELECT id, business_id, production_id, commission_rate_id, defined_low_limit_amount, ratio
                             FROM commission_declare
                             WHERE business_type = 'COMMISSION_DECLARE_ASSET_EVALUATE'
                               AND deleted = 0
                               AND declare_result = '审核通过') cd ON (
            cd.business_id = a.id
                AND (cd.production_id IS NULL OR cd.production_id = ap.id))
                 LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                 LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                 LEFT JOIN (SELECT business_id, report_no, member_id, performance_distribution
                            FROM business_performance_distribution
                            WHERE deleted = 0
                              AND user_type = '评估人员') bpd
                           ON (bpd.business_id = a.id AND bpd.report_no = ap.production_no)
                 LEFT JOIN user u ON u.id = bpd.member_id
        GROUP BY bpd.member_id
    </select>

    <!--资产评估人员提成信息导出-->
    <select id="assetsEvaluatorUserDetailExport" resultType="com.dayou.vo.UserCommissionProdAssetsVO">
        SELECT
        ap.id,
        ROUND(fc.claim_amount,2) as realAmount,
        ap.production_no AS reportNo,
        IF
        ( fc.claim_datetime > ap.save_file_date, fc.claim_datetime, ap.save_file_date ) AS settleDate,
        ap.assets_name AS name,
        ap.created,
        dd.NAME AS businessCate,
        IFNULL(cd.ratio,bcr.min_ratio) AS ratio,
        cd.defined_low_limit_amount AS lowLimitAmount,
        ROUND(bcr.min_ratio,2) AS ratio,
        bpd.member_id AS userId,
        ROUND(bpd.performance_distribution,2) AS allotRatio,
        ROUND(IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
        fc.claim_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100),
        cd.defined_low_limit_amount * (cd.ratio / 100) * (bpd.performance_distribution / 100)
        ),2) AS commissionAmount,
        ROUND(IF(cd.defined_low_limit_amount &lt; fc.claim_amount,
        fc.claim_amount * (cd.ratio / 100),
        cd.defined_low_limit_amount * (cd.ratio / 100)
        ),2) AS totalAmount,
        u.name as userName,
        pf.modified AS realFundDateTime
        FROM
        (
        SELECT
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        AND claim_datetime &gt;= #{vo.startDate}
        AND claim_datetime  &lt;= #{vo.endDate}
        UNION ALL
        SELECT
        allot_amount AS claim_amount,
        order_fund_id,
        production_fund_id,
        created AS claim_datetime
        FROM
        order_fund_allot
        WHERE
        deleted = 0
        AND created  &gt;= #{vo.startDate}
        AND created &lt;= #{vo.endDate}
        ) fc
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no,modified FROM production_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
        INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, save_file_date, created FROM assets_production WHERE deleted = 0 ) ap ON ap.production_no = pf.production_no
        LEFT JOIN assets a ON a.id = ap.business_id
        INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id, ratio, defined_low_limit_amount FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_ASSET_EVALUATE' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
        cd.business_id = a.id
        AND ( cd.production_id IS NULL OR cd.production_id = ap.id ))
        LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
        LEFT JOIN ( SELECT business_id, report_no, member_id, performance_distribution FROM business_performance_distribution WHERE deleted = 0 AND user_type = '评估人员' ) bpd ON ( bpd.business_id = a.id AND bpd.report_no = ap.production_no)
        LEFT JOIN user u on u.id = bpd.member_id
        <where>
            <if test="vo!=null and vo.userId!=null">
                and bpd.member_id = #{vo.userId}
            </if>
        </where>
        order by bpd.member_id
    </select>

    <!--资产市场人员提成计算-->
    <select id="assetsMarketCommission" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT bpd.member_id as id,
               u.name as userName,
               SUM(( fc.claim_amount * ( bcr.min_ratio / 100 ) * (bpd.performance_distribution / 100))) AS commissionTotalAmount
        FROM
        (
        SELECT
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        AND claim_datetime &gt;= #{vo.startDate}
        AND claim_datetime  &lt;= #{vo.endDate}

        UNION ALL SELECT allot_amount AS claim_amount, order_fund_id, production_fund_id, created AS claim_datetime FROM order_fund_allot WHERE deleted = 0
        AND created  &gt;= #{vo.startDate}
        AND created &lt;= #{vo.endDate}
        ) fc
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
        INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, save_file_date, created FROM assets_production WHERE deleted = 0 AND delivery = TRUE ) ap ON ap.production_no = pf.production_no
        LEFT JOIN assets a ON a.id = ap.business_id
        INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_ASSET_MARKET' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
        cd.business_id = a.id
        AND ( cd.production_id IS NULL OR cd.production_id = ap.id ))
        LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
        LEFT JOIN ( SELECT business_id, report_no,declare_id, member_id, performance_distribution FROM business_performance_distribution WHERE deleted = 0 AND user_type = '市场人员'

        ) bpd ON ( bpd.business_id = a.id AND cd.id = bpd.declare_id AND (bpd.report_no is null or bpd.report_no = ap.production_no ))
        LEFT JOIN user u ON u.id = bpd.member_id

        GROUP BY
        bpd.member_id
        having 1=1
        <if test="vo!=null and vo.userName!=null and vo.userName!=''">
            and u.name like concat ('%',#{vo.userName},'%')
        </if>
        order by commissionTotalAmount DESC
    </select>

    <!--资产市场人员提成计算详情-->
    <select id="assetsMarketCommissionDetail" resultType="com.dayou.vo.UserCommissionProdVO">
        SELECT
            ap.id,
            fc.claim_amount as realAmount,
            ap.production_no AS reportNo,
            IF
            ( fc.claim_datetime > ap.save_file_date, fc.claim_datetime, ap.save_file_date ) AS settleDate,
            ap.assets_name AS name,
            ap.created,
            dd.NAME AS businessCate,
            bcr.min_ratio AS ratio,
            bpd.member_id AS userId,
            bpd.performance_distribution AS allotRatio,
            ( fc.claim_amount * ( bcr.min_ratio / 100 ) * (bpd.performance_distribution / 100) ) AS commissionAmount,
            ( fc.claim_amount * ( bcr.min_ratio / 100 ) ) AS totalAmount,
            u.name as userName
        FROM
            (
                SELECT
                    claim_amount,
                    order_fund_id,
                    production_fund_id,
                    claim_datetime
                FROM
                    finance_claim
                WHERE
                    deleted = 0
                  AND production_fund_id IS NOT NULL
                  AND claim_datetime &gt;= #{vo.startDate}
                  AND claim_datetime  &lt;= #{vo.endDate}
                UNION ALL
                SELECT
                    allot_amount AS claim_amount,
                    order_fund_id,
                    production_fund_id,
                    created AS claim_datetime
                FROM
                    order_fund_allot
                WHERE
                    deleted = 0
                  AND created  &gt;= #{vo.startDate}
                  AND created &lt;= #{vo.endDate}
            ) fc
                INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
                INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, save_file_date, created FROM assets_production WHERE deleted = 0 AND delivery = TRUE ) ap ON ap.production_no = pf.production_no
                LEFT JOIN assets a ON a.id = ap.business_id
                INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE business_type = 'COMMISSION_DECLARE_ASSET_MARKET' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
                cd.business_id = a.id
                    AND ( cd.production_id IS NULL OR cd.production_id = ap.id ))
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN ( SELECT business_id, report_no, member_id, performance_distribution,declare_id FROM business_performance_distribution WHERE deleted = 0 AND user_type = '市场人员' ) bpd
                    ON ( bpd.business_id = a.id AND cd.id = bpd.declare_id AND (bpd.report_no is null or bpd.report_no = ap.production_no ))
                LEFT JOIN user u on u.id = bpd.member_id
        WHERE
            bpd.member_id = #{vo.userId}
        order by settleDate DESC
    </select>

    <!--资产市场人员提成合计导出-->
    <select id="assetsMarketTotalAmountExport" resultType="com.dayou.vo.UserCommissionTotalAmountVO">
        SELECT bpd.member_id   as id,
               u.name          as userName,
               ROUND(SUM((fc.claim_amount * (bcr.min_ratio / 100) * (bpd.performance_distribution / 100))),
                     2)        AS commissionTotalAmount,
               '市场人员'      as userType,
               '资产业务'      as businessType,
               #{vo.startDate} as startDate,
               #{vo.endDate}   as endDate
        FROM (SELECT claim_amount,
                     order_fund_id,
                     production_fund_id,
                     claim_datetime
              FROM finance_claim
              WHERE deleted = 0
                AND production_fund_id IS NOT NULL
                AND claim_datetime &gt;= #{vo.startDate}
                AND claim_datetime &lt;= #{vo.endDate}

              UNION ALL
              SELECT allot_amount AS claim_amount, order_fund_id, production_fund_id, created AS claim_datetime
              FROM order_fund_allot
              WHERE deleted = 0
                AND created &gt;= #{vo.startDate}
                AND created &lt;= #{vo.endDate}) fc
                 INNER JOIN (SELECT id, order_fund_id, business_id, production_no
                             FROM production_fund
                             WHERE business_type = 'ASSET_BUSINESS'
                               AND deleted = 0) pf
                            ON (pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id)
                 INNER JOIN (SELECT id,
                                    business_id,
                                    assets_name,
                                    production_no,
                                    production_type,
                                    save_file_date,
                                    created
                             FROM assets_production
                             WHERE deleted = 0
                               AND delivery = true) ap ON ap.production_no = pf.production_no
                 LEFT JOIN assets a ON a.id = ap.business_id
                 INNER JOIN (SELECT id, business_id, production_id, commission_rate_id
                             FROM commission_declare
                             WHERE business_type = 'COMMISSION_DECLARE_ASSET_MARKET'
                               AND deleted = 0
                               AND declare_result = '审核通过') cd ON (
            cd.business_id = a.id
                AND (cd.production_id IS NULL OR cd.production_id = ap.id))
                 LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                 LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                 LEFT JOIN (SELECT business_id, report_no, declare_id, member_id, performance_distribution
                            FROM business_performance_distribution
                            WHERE deleted = 0
                              AND user_type = '市场人员') bpd ON (bpd.business_id = a.id AND cd.id = bpd.declare_id AND
                                                                  (bpd.report_no is null or bpd.report_no = ap.production_no))
                 LEFT JOIN user u ON u.id = bpd.member_id
        GROUP BY bpd.member_id
        ORDER BY commissionTotalAmount DESC
    </select>

    <!--资产市场人员提成信息导出-->
    <select id="assetsMarketUserDetailExport" resultType="com.dayou.vo.UserCommissionProdVO">
        SELECT
        ap.id,
        ROUND(fc.claim_amount,2) as realAmount,
        ap.production_no AS reportNo,
        IF
        ( fc.claim_datetime > ap.save_file_date, fc.claim_datetime, ap.save_file_date ) AS settleDate,
        ap.assets_name AS name,
        ap.created,
        dd.`name` AS businessCate,
        ROUND(bcr.min_ratio,2) AS ratio,
        bpd.member_id AS userId,
        ROUND(bpd.performance_distribution,2) AS allotRatio,
        ROUND(( fc.claim_amount * ( bcr.min_ratio / 100 ) * (bpd.performance_distribution / 100) ),2) AS
        commissionAmount,
        ROUND(( fc.claim_amount * ( bcr.min_ratio / 100 ) ),2) AS totalAmount,
        u.name as userName
        FROM
        (
        SELECT
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        AND claim_datetime &gt;= #{vo.startDate}
        AND claim_datetime &lt;= #{vo.endDate}
        UNION ALL
        SELECT
        allot_amount AS claim_amount,
        order_fund_id,
        production_fund_id,
        created AS claim_datetime
        FROM
        order_fund_allot
        WHERE
        deleted = 0
        AND created &gt;= #{vo.startDate}
        AND created &lt;= #{vo.endDate}
        ) fc
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type =
        'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id
        )
        INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, save_file_date, created FROM
        assets_production WHERE deleted = 0 AND delivery = true ) ap ON ap.production_no = pf.production_no
        LEFT JOIN assets a ON a.id = ap.business_id
        INNER JOIN ( SELECT id, business_id, production_id, commission_rate_id FROM commission_declare WHERE
        business_type = 'COMMISSION_DECLARE_ASSET_MARKET' AND deleted = 0 AND declare_result = '审核通过' ) cd ON (
        cd.business_id = a.id
        AND ( cd.production_id IS NULL OR cd.production_id = ap.id ))
        LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
        LEFT JOIN ( SELECT business_id, report_no, member_id, performance_distribution, declare_id FROM
        business_performance_distribution WHERE deleted = 0 AND user_type = '市场人员' ) bpd
        ON ( bpd.business_id = a.id AND cd.id = bpd.declare_id AND (bpd.report_no is null or bpd.report_no =
        ap.production_no ))
        LEFT JOIN user u on u.id = bpd.member_id
        <where>
            <if test="vo!=null and vo.userId!=null">
                and bpd.member_id = #{vo.userId}
            </if>
        </where>
        order by bpd.member_id
    </select>

    <!--资产收款逾期查询Sql-->
    <sql id="assetsOverdueSql">
        SELECT
        DATEDIFF(CURDATE(), DATE( ap.repertory_out_time )) AS overdueDay,
        a.allot_type,
        a.order_id,
        a.assets_business_gener,
        u.name AS clientManager,
        u1.name AS principal,
        cc1.name AS clientName,
        cc2.name AS clientSubName,
        pf.production_should_amount,
        `of`.should_amount,
        pf.standard_amount,
        a.name as projectName,
        ap.*
        FROM
        (
        SELECT
        id,
        business_id,
        production_no,
        ( CASE production_type WHEN 'STATEMENT' THEN '意见书' WHEN 'REPORT' THEN '评估报告' WHEN 'LETTER' THEN '意见函' WHEN 'CONSULT' THEN '咨询报告' ELSE '' END ) productionType,
        evaluate_amount,
        repertory_out_time
        FROM
        assets_production ap
        WHERE
        deleted = 0
        AND repertory_out_time IS NOT NULL
        AND production_type != 'STATEMENT'
        ) ap
        LEFT JOIN ( SELECT business_id, production_no, production_should_amount, real_amount, standard_amount FROM production_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 ) pf
        ON ( ap.business_id = pf.business_id AND ap.production_no = pf.production_no )
        LEFT JOIN ( SELECT business_id,should_amount FROM order_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 ) `of` ON `of`.business_id = ap.business_id
        LEFT JOIN assets a ON a.id = ap.business_id
        LEFT JOIN user u ON u.id = a.client_manager_id
        LEFT JOIN user u1 ON u1.id = a.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = a.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = a.clientele_sub_id
        WHERE
        ap.productionType != 'STATEMENT'
        AND ( pf.production_no IS NULL OR pf.real_amount IS NULL )
            <if test="overdueVO!=null and overdueVO.overdueMin!=null">
                AND DATEDIFF(CURDATE(),DATE( ap.repertory_out_time )) &gt;= #{overdueVO.overdueMin}
            </if>
            <if test="overdueVO!=null and overdueVO.overdueMax!=null">
                AND DATEDIFF(CURDATE(),DATE( ap.repertory_out_time )) &lt;= #{overdueVO.overdueMax}
            </if>
            <if test="overdueVO!=null and overdueVO.orderId!=null and overdueVO.orderId!=''">
                and a.order_id = #{overdueVO.orderId}
            </if>
            <if test="overdueVO!=null and overdueVO.productionNo!=null and overdueVO.productionNo!=''">
                and ap.production_no like concat ('%',#{overdueVO.productionNo},'%')
            </if>
            <if test="overdueVO!=null and overdueVO.principal!=null and overdueVO.principal!=''">
                and u1.name like concat ('%',#{overdueVO.principal},'%')
            </if>
            <if test="overdueVO!=null and overdueVO.userIds!=null and overdueVO.userIds.size!=0">
            and a.client_manager_id in
                <foreach collection="overdueVO.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        order by overdueDay asc
    </sql>

    <!--资产收款逾期列表-->
    <select id="overdueAssets" resultType="com.dayou.vo.AssetsProdOverdueVO">
        <include refid="assetsOverdueSql" />
    </select>

    <!--资产收款逾期列表导出-->
    <select id="overdueAssetsExport" resultType="com.dayou.vo.AssetsProdOverdueVO">
        <include refid="assetsOverdueSql" />
    </select>

    <resultMap id="settleAssetsProductionVOMap" type="com.dayou.vo.SettleAssetsProductionVO">
        <id property="id" column="id" />
        <result property="id" column="id" />
        <result property="claimAmount" column="claim_amount" />
        <result property="orderId" column="order_id" />
        <result property="productionNo" column="production_no" />
        <result property="claimDate" column="claimDate" />
        <result property="saveFileDate" column="save_file_date" />
        <result property="settleDate" column="settleDate" />
        <result property="deliveryDate" column="deliveryDate" />
        <result property="projectName" column="projectName" />
        <result property="bailor" column="bailor" />
        <result property="evaluateAmount" column="evaluate_amount" />
        <result property="principal" column="principal" />
        <result property="clientManager" column="clientManager" />
        <result property="clientName" column="clientName" />
        <result property="clientSubName" column="clientSubName" />
        <result property="evaluateDepartmentName" column="evaluateDepartmentName" />
        <result property="marketDepartmentName" column="marketDepartmentName" />
        <result property="payDateTime" column="pay_datetime" />
        <collection property="invoices" ofType="com.dayou.vo.SettleProInvoiceVo">
            <result property="invoiceDate" column="invoiceDate" />
            <result property="invoiceUser" column="invoiceUser" />
            <result property="invoiceNo" column="invoiceNo" />
        </collection>
    </resultMap>

    <!--资产市场人员结算项目Sql-->
    <sql id="assetsMarketerSettleSql">
        SELECT
        fc.id,
        fc.claim_amount,
        a.order_id,
        ap.production_no,
        fc.claim_datetime as claimDate,
        ap.delivery_date AS deliveryDate,
        ap.save_file_date,
        IF
        ( fc.claim_datetime > ap.delivery_date, fc.claim_datetime, ap.delivery_date ) AS settleDate,
        ap.assets_name AS projectName,
        a.bailor,
        ap.evaluate_amount,
        u.name AS principal,
        u1.name AS clientManager,
        cc.name AS clientName,
        cc1.name AS clientSubName,
        d.name AS evaluateDepartmentName,
        (
        SELECT
        GROUP_CONCAT( department.name )
        FROM
        department
        WHERE
        id IN (
        SELECT
        department_id
        FROM
        post
        WHERE
        id IN ( SELECT post_id FROM user_post WHERE user_id = a.client_manager_id ))) AS marketDepartmentName,
        invoice.name AS invoiceUser,
        invoice.real_make_date AS invoiceDate,
        invoice.tax_no AS invoiceNo,
        frf.pay_datetime
        FROM
        (
        SELECT
            id,
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime,
        real_fund_id
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        AND claim_datetime &gt;= #{settleVO.startDate}
        AND claim_datetime &lt;= #{settleVO.endDate} UNION ALL
        SELECT id,allot_amount AS claim_amount, order_fund_id, production_fund_id, created AS claim_datetime,real_fund_id FROM
        order_fund_allot WHERE deleted = 0
        AND created &gt;= #{settleVO.startDate}
        AND created &lt;= #{settleVO.endDate}
        ) fc
        inner join (
        select id,pay_datetime from finance_real_fund
        where deleted = 0
        <if test="settleVO!=null and settleVO.payStartDate!=null and settleVO.payStartDate!='' ">
            AND pay_datetime &gt;= #{settleVO.payStartDate}
        </if>
        <if test="settleVO!=null and settleVO.payEndDate!=null and settleVO.payEndDate!='' ">
            AND pay_datetime &lt;= #{settleVO.payEndDate}
        </if>
        ) frf on frf.id = fc.real_fund_id
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type =
        'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id
        )
        INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, evaluate_amount,
        delivery_date, save_file_date FROM assets_production WHERE deleted = 0 AND delivery = true ) ap ON ap.production_no =
        pf.production_no
        LEFT JOIN assets a ON a.id = ap.business_id
        LEFT JOIN user u ON u.id = a.principal_id
        LEFT JOIN user u1 ON u1.id = a.client_manager_id
        LEFT JOIN customer_company cc ON cc.id = a.clientele_id
        LEFT JOIN customer_company cc1 ON cc1.id = a.clientele_sub_id
        LEFT JOIN department d ON d.id = a.department_id
        LEFT JOIN (
            SELECT real_make_date, name, tax_no, order_fund_id, production_fund_id
            FROM finance_invoice, user
            WHERE maker_id = user.id
            AND finance_invoice.deleted = 0
            AND state = '已开票'
        ) AS invoice ON IF(invoice.production_fund_id IS NOT NULL ,invoice.order_fund_id = pf.order_fund_id AND invoice.production_fund_id = pf.id,invoice.order_fund_id = pf.order_fund_id)
        <where>
            <if test="keyword!=null and keyword!='' ">
                and (
                a.order_id = #{keyword}
                or ap.production_no like concat ('%',#{keyword},'%')
                or u.name like concat ('%',#{keyword},'%')
                or u1.name like concat ('%',#{keyword},'%')
                )
            </if>
        </where>
        order by fc.claim_datetime  ASC
    </sql>

    <!--资产结算项目查询-->
    <select id="assetsMarketSettleProd" resultMap="settleAssetsProductionVOMap">
        <include refid="assetsMarketerSettleSql" />
    </select>

    <!--资产结算项目导出-->
    <select id="assetsMarketSettleExport" resultMap="settleAssetsProductionVOMap">
        <include refid="assetsMarketerSettleSql" />
    </select>

    <!--资产评估人员结算项目Sql-->
    <sql id="assetsEvaluatorSettleSql">
        SELECT
        fc.id,
        fc.claim_amount,
        a.order_id,
        ap.production_no,
        fc.claim_datetime as claimDate,
        ap.save_file_date,
        IF( fc.claim_datetime > ap.save_file_date, fc.claim_datetime, ap.save_file_date ) AS settleDate,
        IF(ap.assets_name IS NOT NULL, ap.assets_name, a.name ) AS projectName,
        a.bailor,
        ap.evaluate_amount,
        u.name AS principal,
        u1.name AS clientManager,
        cc.name AS clientName,
        cc1.name AS clientSubName,
        d.name AS evaluateDepartmentName,
        (
        SELECT
        GROUP_CONCAT( NAME )
        FROM
        department
        WHERE
        id IN (
        SELECT
        department_id
        FROM
        post
        WHERE
        id IN ( SELECT post_id FROM user_post WHERE user_id = a.client_manager_id ))) AS marketDepartmentName,
        invoice.name AS invoiceUser,
        invoice.real_make_date AS invoiceDate,
        invoice.tax_no AS invoiceNo,
        frf.pay_datetime
        FROM
        (
        SELECT
        id,
        claim_amount,
        order_fund_id,
        production_fund_id,
        claim_datetime,
        real_fund_id
        FROM
        finance_claim
        WHERE
        deleted = 0
        AND production_fund_id IS NOT NULL
        AND claim_datetime &gt;= #{settleVO.startDate}
        AND claim_datetime  &lt;= #{settleVO.endDate} UNION ALL
        SELECT id, allot_amount AS claim_amount, order_fund_id, production_fund_id, created AS claim_datetime ,real_fund_id FROM order_fund_allot WHERE deleted = 0
        AND created  &gt;= #{settleVO.startDate}
        AND created &lt;= #{settleVO.endDate}
        ) fc

        inner join (
        select id,pay_datetime from finance_real_fund
        where deleted = 0
        <if test="settleVO!=null and settleVO.payStartDate!=null and settleVO.payStartDate!='' ">
            AND pay_datetime &gt;= #{settleVO.payStartDate}
        </if>
        <if test="settleVO!=null and settleVO.payEndDate!=null and settleVO.payEndDate!='' ">
            AND pay_datetime &lt;= #{settleVO.payEndDate}
        </if>
        ) frf on frf.id = fc.real_fund_id
        INNER JOIN ( SELECT id, order_fund_id, business_id, production_no FROM production_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 ) pf ON ( pf.id = fc.production_fund_id AND pf.order_fund_id = fc.order_fund_id )
        INNER JOIN ( SELECT id, business_id, assets_name, production_no, production_type, evaluate_amount, save_file_date FROM assets_production WHERE deleted = 0 AND save_file_date IS NOT NULL ) ap ON ap.production_no = pf.production_no
        LEFT JOIN assets a ON a.id = ap.business_id
        LEFT JOIN user u ON u.id = a.principal_id
        LEFT JOIN user u1 ON u1.id = a.client_manager_id
        LEFT JOIN customer_company cc ON cc.id = a.clientele_id
        LEFT JOIN customer_company cc1 ON cc1.id = a.clientele_sub_id
        LEFT JOIN department d ON d.id = a.department_id
        LEFT JOIN (
            SELECT real_make_date, name, tax_no, order_fund_id, production_fund_id
            FROM finance_invoice, user
            WHERE maker_id = user.id
            AND finance_invoice.deleted = 0
            AND state = '已开票'
        ) AS invoice ON IF(invoice.production_fund_id IS NOT NULL ,invoice.order_fund_id = pf.order_fund_id AND invoice.production_fund_id = pf.id,invoice.order_fund_id = pf.order_fund_id)
        <where>
            <if test="keyword!=null and keyword!='' ">
                and (
                a.order_id = #{keyword}
                or ap.production_no like concat ('%',#{keyword},'%')
                or u.name like concat ('%',#{keyword},'%')
                or u1.name like concat ('%',#{keyword},'%')
                )
            </if>
        </where>
        order by  fc.claim_datetime ASC
    </sql>

    <!--资产评估人员结算项目查询-->
    <select id="assetsEvaluatorSettleProd" resultMap="settleAssetsProductionVOMap">
        <include refid="assetsEvaluatorSettleSql" />
    </select>

    <!--资产评估人员结算项目导出-->
    <select id="assetsEvaluatorSettleExport" resultMap="settleAssetsProductionVOMap">
        <include refid="assetsEvaluatorSettleSql" />
    </select>

    <select id="exportList" parameterType="com.dayou.entity.FinanceRealFund" resultType="com.dayou.dto.FinanceRealFundExportDTO">
        <include refid="pageListSql" />
        ORDER BY frf.pay_datetime DESC
    </select>

    <sql id="realAmountClaimedSQL">
        SELECT
        claimd.id,
        fr.amount,
        fr.payer,
        fr.pay_datetime,
        fr.remark,
        claimd.claimAmount,
        claimd.claimDate,
        u.NAME AS creator,
        u1.NAME AS claimUser,
        (fr.amount-claimd.claimAmount) as notClaimAmount
        FROM
        (
        SELECT
        frf.id,
        sum( fc.claim_amount ) claimAmount,
        max( fc.claim_datetime ) claimDate,
        fc.claim_user_id
        FROM
        finance_claim fc
        LEFT JOIN finance_real_fund frf ON frf.id = fc.real_fund_id
        WHERE
        frf.deleted = 0
        AND fc.deleted = 0
        GROUP BY
        frf.id
        ) claimd
        LEFT JOIN finance_real_fund fr ON fr.id = claimd.id
        LEFT JOIN user u ON u.id = fr.creator_id
        LEFT JOIN user u1 ON u1.id = claimd.claim_user_id
        <where>
            <if test="keyword!=null and keyword!='' ">
                and (
                fr.payer like concat ('%',#{keyword},'%')
                or u1.name like concat('%',#{keyword},'%')
                or claimd.claimAmount = #{keyword}
                or fr.amount = #{keyword}
                or fr.remark like concat('%',#{keyword},'%')
                )
            </if>
            <if test="vo!=null and vo.startDate!=null and vo.startDate!=''">
                and fr.pay_datetime &gt;= #{vo.startDate}
            </if>
            <if test="vo!=null and vo.endDate!=null and vo.endDate!=''">
                and fr.pay_datetime &lt;= #{vo.endDate}
            </if>
            <if test="vo!=null and vo.xStartDate!=null and vo.xStartDate!=''">
                and claimd.claimDate &gt;= #{vo.xStartDate}
            </if>
            <if test="vo!=null and vo.xEndDate!=null and vo.xEndDate!=''">
                and claimd.claimDate &lt;= #{vo.xEndDate}
            </if>
        </where>
        ORDER BY
        claimd.claimDate DESC
    </sql>

    <select id="realAmountClaimed" parameterType="com.dayou.vo.FinanceRealFundVO" resultType="com.dayou.vo.FinanceRealFundVO">
       <include refid="realAmountClaimedSQL" />
    </select>

    <select id="realClaimedExport" parameterType="com.dayou.vo.FinanceRealFundVO" resultType="com.dayou.dto.FinanceRealFundExportDTO">
        <include refid="realAmountClaimedSQL" />
    </select>

    <select id="financeRealFundNotClaimExport" parameterType="com.dayou.entity.FinanceRealFund" resultType="com.dayou.dto.FinanceRealNotClaimDTO">
        <include refid="pageListSql" />
        ORDER BY
        ifnull(t.ca,0) ASC ,frf.created DESC
    </select>
</mapper>
