<?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.fjhx.sale.mapper.sample.SampleMapper">
    <select id="getPage" resultType="com.fjhx.sale.entity.sample.vo.SampleVo">
        SELECT *
        FROM (SELECT t1.*,
                     IFNULL(t1.amount, 0) * IFNULL(t1.rate, 0) AS amountCNY,
                     CASE
                         WHEN (IFNULL(t1.amount, 0) * IFNULL(t1.rate, 0) - t1.sumClaimMoney) &lt;= 0 THEN
                             20
                         WHEN t1.sumClaimMoney = 0 THEN
                             0
                         ELSE 10
                         END                                   AS refundStatusNew
              FROM (SELECT t1.*,
                           us.nick_name                      salesmanName,
                           (SELECT IFNULL(SUM(IFNULL(cc.money_cny, IFNULL(cc.money, 0) * IFNULL(arw.rate, 1))), 0)
                            FROM claim_contract cc
                                     left join claim c on cc.claim_id = c.id
                                     LEFT JOIN account_running_water arw on c.business_id = arw.id
                            WHERE cc.contract_id = t1.id) AS sumClaimMoney
                    FROM sample t1
                             left join customer c on t1.buy_corporation_id = c.id
                             left join sys_user us on c.user_id = us.user_id) t1) t1
            ${ew.customSqlSegment}
    </select>

    <select id="sampleHandoverList" resultType="com.fjhx.sale.entity.contract.vo.ContractProductVo">
        SELECT
        t1.quantity,
        t1.create_time AS ct,
        t1.id,
        t1.sample_id sampleId,
        t1.product_id,
        t2.`code` AS `contractCode`,
        us.nick_name AS userName,
        t2.versions AS contractVersion,
        t2.buy_corporation_id buyCorporationId,
        <!-- 需要采购的合同数量-->
            t1.expend_quantity AS expendQuantity,
            <!-- 交接单时间-->
            t3.sampleTime,
            <!-- 合同审批通过后的时间（每次合同审批后都会修改时间 所以取update_time） -->
            t2.update_time updateTime,
            ( SELECT create_time FROM claim_contract WHERE contract_id = t2.id ORDER BY create_time DESC LIMIT 1 ) AS claimTime,
            t2.create_user
        FROM
            sample_product t1
            LEFT JOIN sample t2 ON t1.sample_id = t2.id
            <!-- 列表中只显示存在交接单的数据 获取到交接单时间最单的一条-->
        left join(SELECT business_id,create_time sampleTime from file_info GROUP BY business_id HAVING
        MAX(create_time)) t3 on
        t2.id = t3.business_id
        left join customer c on t2.buy_corporation_id = c.id
        left join sys_user us on c.user_id = us.user_id
        ${ew.customSqlSegment}
    </select>
    <update id="updateSample" parameterType="com.fjhx.sale.entity.sample.po.Sample">
        update sample
        <set>
            <if test="id != null">id = #{id},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isShow != null">is_show = #{isShow},</if>
            <if test="oldSampleId != null">old_sample_id = #{oldSampleId},</if>
            <if test="approvedDate != null">approved_date = DATE_FORMAT(NOW(),'%Y-%m-%d %H:%m:%s'),</if>
            update_time = sysdate()
        </set>
        where id = #{upId}
    </update>

    <select id="getAccountRunningWaterBySampleId"
            resultType="com.fjhx.account.entity.account.vo.AccountRunningWaterVo">
        SELECT arw.id,
               arw.create_time,
               ap.type,
               arw.currency,
               pad.money          amount,
               pad.remark         remarks,
               am.`name`          accountManagementName,
               am.account_opening accountManagementOpening,
               arw.`name`,
               arw.status
        FROM sample s
                 JOIN purchase p ON p.data_resource_id = s.id AND p.del_flag = 0
                 JOIN pay_detail `pad` ON pad.purchase_id = p.id AND pad.del_flag = 0
                 JOIN pay pa ON pad.pay_id = pa.id AND pa.del_flag = 0
                 JOIN account_payment ap ON ap.business_id = pa.id AND ap.del_flag = 0
                 JOIN account_running_water arw ON arw.business_id = ap.id AND arw.del_flag = 0
                 LEFT JOIN account_management am
                           ON arw.account_management_id = am.id AND am.del_flag = 0
        WHERE s.id = #{sampleId}
        UNION ALL
        SELECT arw.id,
               arw.create_time,
               ap.type,
               arw.currency,
               arfd.amount,
               arfd.remarks,
               am.`name`          accountManagementName,
               am.account_opening accountManagementOpening,
               arw.`name`,
               arw.status
        FROM sample s
                 JOIN account_request_funds_detail arfd
                      ON arfd.contract_id = s.id AND arfd.del_flag = 0
                 JOIN account_payment ap
                      ON ap.business_id = arfd.account_request_funds_id AND ap.del_flag = 0
                 JOIN account_running_water arw ON arw.business_id = ap.id AND arw.del_flag = 0
                 LEFT JOIN account_management am
                           ON arw.account_management_id = am.id AND am.del_flag = 0
        WHERE s.id = #{sampleId}
        UNION ALL
        SELECT arw.id,
               arw.create_time,
               30                 type,
               arw.currency,
               clc.money,
               arw.remarks,
               am.`name`          accountManagementName,
               am.account_opening accountManagementOpening,
               arw.`name`,
               arw.status
        FROM account_running_water arw
                 JOIN claim cl ON cl.business_id = arw.id
                 JOIN claim_contract clc ON clc.claim_id = cl.id
                 JOIN sample s ON clc.contract_id = s.id
                 LEFT JOIN account_management am
                           ON arw.account_management_id = am.id AND am.del_flag = 0
        WHERE arw.del_flag = 0
          AND s.id = #{sampleId}
    </select>

</mapper>
