<?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.account.mapper.write.WriteOffRecordsMapper">
    <select id="getPage" resultType="com.fjhx.account.entity.write.vo.WriteOffRecordsVo">
        select wor.id,
               wor.account_payment_id,
               wor.account_payment_date,
               wor.remark,
               wor.create_user,
               wor.create_time,
               wor.update_user,
               wor.update_time,
               ap.corporation_id,
               ap.type,
               ap.currency,
               ap.business_id,
               if(type!=20,
                  (SELECT GROUP_CONCAT(t4.`code`)
                   FROM account_request_funds t2
                            LEFT JOIN account_request_funds_detail t3 ON t2.id = t3.account_request_funds_id
                            LEFT JOIN `bytesailing_sale`.contract t4 ON t3.contract_id = t4.id
                   where ap.business_id = t2.id),
                  (SELECT GROUP_CONCAT(if(c.`code` is null, p.`code`, c.`code`))
                   FROM pay pa
                            LEFT JOIN pay_detail pad ON pad.pay_id = pa.id LEFT JOIN purchase p ON pad.purchase_id = p.id LEFT JOIN contract c ON p.data_resource_id = c.id WHERE ap.business_id = pa.id)
	        )                 as contractCodes,
               ap.create_time as applicationTime,
               ap.amount
        from write_off_records wor
                 LEFT JOIN account_payment ap ON wor.account_payment_id = ap.id
            ${ew.customSqlSegment}
    </select>

</mapper>
