select
a.id, a.pay_id, a.pay_type, a.rec_type, a.pay_man, a.pay_man_code, a.settle_bill_no,
a.bl_audit, a.audit_date, a.audit_man_code, a.audit_site_code, a.customer_code,
a.customer_name, a.pay_money, a.pay_status, a.pay_status_desc, a.break_type,
a.break_reason, a.break_emp_code, a.rec_man_code, a.settle_type, a.refund_date,
a.refund_man_code, a.refund_seq, a.refund_mode, a.refund_money, a.refund_reason,
a.refund_remark, a.refund_op_man_code, a.refund_register_site_code,
a.refund_register_man_code, a.satisfy_money, a.satisfy_reason, a.allowance_money,
a.allowance_reason, a.deduction_money, a.deduction_reason, a.other_money,
a.other_reason, a.trade_date,
-- 重新赋值备注字段:财务开票备注+账单备注
CONCAT_WS(';',
COALESCE(invoice_remarks.invoice_remark, ''),
COALESCE(bill_remark.remark, '')
) as remark,
a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time, a.create_site, a.update_site,
-- 运单号(多个用逗号分隔)
COALESCE(waybill_codes.bill_codes, '') as bill_codes,
-- 账单金额
COALESCE(bill_amount.rec_money, 0) as bill_amount,
-- 开票金额
COALESCE(invoice_amount.total_amount, 0) as invoice_amount,
-- 开票状态
COALESCE(invoice_status.status, '0') as invoice_status
from emis_settle_pay_record a
-- 关联获取运单号
left join (
select
espbr.pay_id,
GROUP_CONCAT(essb.bill_code SEPARATOR ',') as bill_codes
from emis_settle_pay_bill_rel espbr
left join emis_settle_sub_bill essb on espbr.settle_bill_no = essb.settle_bill_no
where espbr.del_flag = '0' and essb.del_flag = '0'
group by espbr.pay_id
) waybill_codes on a.pay_id = waybill_codes.pay_id
-- 关联获取账单金额
left join (
select
espbr.pay_id,
esb.rec_money
from emis_settle_pay_bill_rel espbr
left join emis_settle_bill esb on espbr.settle_bill_no = esb.settle_bill_no
where espbr.del_flag = '0' and esb.del_flag = '0'
group by espbr.pay_id
) bill_amount on a.pay_id = bill_amount.pay_id
-- 关联获取开票金额
left join (
select
espir.pay_id,
SUM(esir.open_money) as total_amount
from emis_settle_pay_invoice_rel espir
left join emis_settle_invoice_record esir on espir.apply_seq_no = esir.apply_seq_no
where espir.del_flag = '0' and esir.del_flag = '0' and esir.invoice_status = '1'
group by espir.pay_id
) invoice_amount on a.pay_id = invoice_amount.pay_id
-- 关联获取开票状态
left join (
select
espir.pay_id,
esir.invoice_status as status
from emis_settle_pay_invoice_rel espir
left join emis_settle_invoice_record esir on espir.apply_seq_no = esir.apply_seq_no
where espir.del_flag = '0' and esir.del_flag = '0'
group by espir.pay_id
) invoice_status on a.pay_id = invoice_status.pay_id
-- 关联获取财务开票备注
left join (
select
espir.pay_id,
GROUP_CONCAT(esir.invioce_remark SEPARATOR ';') as invoice_remark
from emis_settle_pay_invoice_rel espir
left join emis_settle_invoice_record esir on espir.apply_seq_no = esir.apply_seq_no
where espir.del_flag = '0' and esir.del_flag = '0'
and esir.invioce_remark is not null and esir.invioce_remark != ''
group by espir.pay_id
) invoice_remarks on a.pay_id = invoice_remarks.pay_id
-- 关联获取账单备注
left join (
select
espbr.pay_id,
esb.remark
from emis_settle_pay_bill_rel espbr
left join emis_settle_bill esb on espbr.settle_bill_no = esb.settle_bill_no
where espbr.del_flag = '0' and esb.del_flag = '0'
group by espbr.pay_id
) bill_remark on a.pay_id = bill_remark.pay_id
insert into emis_settle_pay_record
id,
pay_id,
pay_type,
rec_type,
pay_man,
pay_man_code,
settle_bill_no,
bl_audit,
audit_date,
audit_man_code,
audit_site_code,
customer_code,
customer_name,
pay_money,
pay_status,
pay_status_desc,
break_type,
break_reason,
break_emp_code,
rec_man_code,
settle_type,
refund_date,
refund_man_code,
refund_seq,
refund_mode,
refund_money,
refund_reason,
refund_remark,
refund_op_man_code,
refund_register_site_code,
refund_register_man_code,
satisfy_money,
satisfy_reason,
allowance_money,
allowance_reason,
deduction_money,
deduction_reason,
other_money,
other_reason,
trade_date,
remark,
del_flag,
create_by,
create_time,
update_by,
update_time,
create_site,
update_site,
#{id},
#{payId},
#{payType},
#{recType},
#{payMan},
#{payManCode},
#{settleBillNo},
#{blAudit},
#{auditDate},
#{auditManCode},
#{auditSiteCode},
#{customerCode},
#{customerName},
#{payMoney},
#{payStatus},
#{payStatusDesc},
#{breakType},
#{breakReason},
#{breakEmpCode},
#{recManCode},
#{settleType},
#{refundDate},
#{refundManCode},
#{refundSeq},
#{refundMode},
#{refundMoney},
#{refundReason},
#{refundRemark},
#{refundOpManCode},
#{refundRegisterSiteCode},
#{refundRegisterManCode},
#{satisfyMoney},
#{satisfyReason},
#{allowanceMoney},
#{allowanceReason},
#{deductionMoney},
#{deductionReason},
#{otherMoney},
#{otherReason},
#{tradeDate},
#{remark},
#{delFlag},
#{createBy},
#{createTime},
#{updateBy},
#{updateTime},
#{createSite},
#{updateSite},
update emis_settle_pay_record
pay_id = #{payId},
pay_type = #{payType},
rec_type = #{recType},
pay_man = #{payMan},
pay_man_code = #{payManCode},
settle_bill_no = #{settleBillNo},
bl_audit = #{blAudit},
audit_date = #{auditDate},
audit_man_code = #{auditManCode},
audit_site_code = #{auditSiteCode},
customer_code = #{customerCode},
customer_name = #{customerName},
pay_money = #{payMoney},
pay_status = #{payStatus},
pay_status_desc = #{payStatusDesc},
break_type = #{breakType},
break_reason = #{breakReason},
break_emp_code = #{breakEmpCode},
rec_man_code = #{recManCode},
settle_type = #{settleType},
refund_date = #{refundDate},
refund_man_code = #{refundManCode},
refund_seq = #{refundSeq},
refund_mode = #{refundMode},
refund_money = #{refundMoney},
refund_reason = #{refundReason},
refund_remark = #{refundRemark},
refund_op_man_code = #{refundOpManCode},
refund_register_site_code = #{refundRegisterSiteCode},
refund_register_man_code = #{refundRegisterManCode},
satisfy_money = #{satisfyMoney},
satisfy_reason = #{satisfyReason},
allowance_money = #{allowanceMoney},
allowance_reason = #{allowanceReason},
deduction_money = #{deductionMoney},
deduction_reason = #{deductionReason},
other_money = #{otherMoney},
other_reason = #{otherReason},
trade_date = #{tradeDate},
remark = #{remark},
del_flag = #{delFlag},
create_by = #{createBy},
create_time = #{createTime},
update_by = #{updateBy},
update_time = #{updateTime},
create_site = #{createSite},
update_site = #{updateSite},
where id = #{id}
update emis_settle_pay_record set del_flag='1' where id = #{id}
update emis_settle_pay_record set del_flag='1' where id in
#{id}