select wad.id, wad.apply_id, wad.apply_no, wad.bill_code, wad.apply_money, wad.settle_bill_no, wad.del_flag,
CONCAT(IFNULL(send_site.site_name, ''), '-', IFNULL(dest_site.site_name, '')) as express_type,
w.id as w_id, w.order_sn as w_order_sn, w.cust_order_id as w_cust_order_id, w.bill_code as w_bill_code, w.bill_code_sub as w_bill_code_sub,
w.order_status as w_order_status, w.waybill_status as w_waybill_status, w.order_type as w_order_type, w.order_date as w_order_date,
w.user_id as w_user_id, w.customer_code as w_customer_code, w.customer_name as w_customer_name, w.open_id as w_open_id,
w.receive_name as w_receive_name, w.receive_company as w_receive_company, w.receive_mobile as w_receive_mobile, w.receive_tel as w_receive_tel,
w.receive_country as w_receive_country, w.receive_province as w_receive_province, w.receive_city as w_receive_city,
w.receive_county as w_receive_county, w.receive_town as w_receive_town, w.receive_address as w_receive_address, w.receive_postcode as w_receive_postcode,
w.send_name as w_send_name, w.send_company as w_send_company, w.send_mobile as w_send_mobile, w.send_tel as w_send_tel,
w.send_country as w_send_country, w.send_province as w_send_province, w.send_city as w_send_city,
w.send_county as w_send_county, w.send_town as w_send_town, w.send_address as w_send_address, w.send_postcode as w_send_postcode,
w.freight as w_freight, w.salesmen as w_salesmen, w.payee as w_payee, w.payment_type as w_payment_type, w.product_type as w_product_type,
tp.prod_name as w_product_type_name,
w.parcel_qty as w_parcel_qty, w.bill_weight as w_bill_weight, w.total_volume as w_total_volume, w.send_date as w_send_date,
w.dispatch_date as w_dispatch_date, w.sign_man as w_sign_man, w.sign_man_code as w_sign_man_code,
w.sign_site_code as w_sign_site_code, w.sign_date as w_sign_date, w.timezone_offset as w_timezone_offset, w.payment_status as w_payment_status,
w.payment_date as w_payment_date, w.payment_remark as w_payment_remark, w.order_remark as w_order_remark, w.data_from as w_data_from,
w.bl_special_goods as w_bl_special_goods, w.ext_info as w_ext_info, w.lading_bill_code as w_lading_bill_code, w.company_code as w_company_code,
w.departure_port as w_departure_port, w.destination_port as w_destination_port, w.box_quantity as w_box_quantity, w.settlement_weight as w_settlement_weight,
w.send_site_code as w_send_site_code, send_site.site_name as w_send_site_name, w.dispatch_underling_site_code as w_dispatch_underling_site_code, dest_site.site_name as w_dispatch_underling_site_name,
sb.id as sb_id, sb.settle_bill_no as sb_settle_bill_no, sb.settle_bill_name as sb_settle_bill_name, sb.settle_type as sb_settle_type,
sb.settle_start_date as sb_settle_start_date, sb.settle_end_date as sb_settle_end_date, sb.bill_month as sb_bill_month,
sb.rec_money as sb_rec_money, sb.reced_money as sb_reced_money, sb.invoiced_money as sb_invoiced_money, sb.cust_no as sb_cust_no,
sb.cust_name as sb_cust_name, sb.customer_code as sb_customer_code, sb.customer_name as sb_customer_name, sb.site_code as sb_site_code,
sb.site_name as sb_site_name, sb.satisfy_money as sb_satisfy_money, sb.allowance_money as sb_allowance_money, sb.deduction_money as sb_deduction_money,
sb.other_money as sb_other_money, sb.satisfy_reason as sb_satisfy_reason, sb.allowance_reason as sb_allowance_reason,
sb.deduction_reason as sb_deduction_reason, sb.other_reason as sb_other_reason, sb.open_bill_status as sb_open_bill_status,
sb.payment_status as sb_payment_status, sb.charge_status as sb_charge_status, sb.send_piece_sum as sb_send_piece_sum,
sb.piece_number as sb_piece_number, sb.fee_weight as sb_fee_weight, sb.uncollected_amount as sb_uncollected_amount,
sb.send_money_sum as sb_send_money_sum, sb.refund_amount as sb_refund_amount, sb.refund_money as sb_refund_money,
sb.payee as sb_payee, sb.salesmen as sb_salesmen, sb.credit_period as sb_credit_period, sb.credit_period_type as sb_credit_period_type,
sb.settle_day as sb_settle_day, sb.payment_due_date as sb_payment_due_date, sb.bl_send_oms as sb_bl_send_oms,
sb.bl_confirm_center as sb_bl_confirm_center, sb.confirm_center_date as sb_confirm_center_date, sb.confirm_center_note as sb_confirm_center_note,
sb.confirm_center_man_code as sb_confirm_center_man_code, sb.confirm_center_code as sb_confirm_center_code,
sb.bl_confirm_site as sb_bl_confirm_site, sb.confirm_site_date as sb_confirm_site_date, sb.confirm_site_man_code as sb_confirm_site_man_code,
sb.confirm_site_note as sb_confirm_site_note, sb.confirm_site_code as sb_confirm_site_code,
ssb.id as ssb_id, ssb.bill_no as ssb_bill_no, ssb.parent_bill_no as ssb_parent_bill_no, ssb.settle_bill_no as ssb_settle_bill_no,
ssb.bill_code as ssb_bill_code, ssb.bill_date as ssb_bill_date, ssb.bill_fee as ssb_bill_fee, ssb.currency as ssb_currency,
ssb.settle_type as ssb_settle_type, ssb.bl_split as ssb_bl_split, ssb.bl_merge as ssb_bl_merge, ssb.bill_month as ssb_bill_month,
ssb.cust_no as ssb_cust_no, ssb.cust_name as ssb_cust_name, ssb.customer_code as ssb_customer_code, ssb.customer_name as ssb_customer_name,
ssb.invoiced_money as ssb_invoiced_money, ssb.reced_money as ssb_reced_money, ssb.satisfy_money as ssb_satisfy_money,
ssb.allowance_money as ssb_allowance_money, ssb.deduction_money as ssb_deduction_money, ssb.other_money as ssb_other_money,
ssb.satisfy_reason as ssb_satisfy_reason, ssb.allowance_reason as ssb_allowance_reason, ssb.deduction_reason as ssb_deduction_reason,
ssb.other_reason as ssb_other_reason, ssb.open_bill_status as ssb_open_bill_status, ssb.payment_status as ssb_payment_status,
ssb.charge_status as ssb_charge_status, ssb.payee as ssb_payee, ssb.salesmen as ssb_salesmen, ssb.bl_special_quote as ssb_bl_special_quote,
ssb.bl_sensitive as ssb_bl_sensitive, ssb.bl_confirm_center as ssb_bl_confirm_center, ssb.confirm_center_date as ssb_confirm_center_date,
ssb.confirm_center_note as ssb_confirm_center_note, ssb.confirm_center_man_code as ssb_confirm_center_man_code,
ssb.confirm_center_code as ssb_confirm_center_code, ssb.bl_confirm_site as ssb_bl_confirm_site, ssb.confirm_site_date as ssb_confirm_site_date,
ssb.confirm_site_man_code as ssb_confirm_site_man_code, ssb.confirm_site_note as ssb_confirm_site_note, ssb.confirm_site_code as ssb_confirm_site_code
from emis_writeoff_apply_detail wad
left join emis_waybill w on wad.bill_code = w.bill_code and w.del_flag = '0'
left join emis_trans_product tp on w.product_type = tp.prod_code and tp.del_flag = '0'
left join emis_settle_bill sb on wad.settle_bill_no = sb.settle_bill_no and sb.del_flag = '0'
left join emis_settle_sub_bill ssb on wad.bill_code = ssb.bill_code and ssb.del_flag = '0'
left join emis_site send_site on w.send_site_code = send_site.site_code and send_site.del_flag = '0'
left join emis_site dest_site on w.dispatch_underling_site_code = dest_site.site_code and dest_site.del_flag = '0'
insert into emis_writeoff_apply_detail
apply_id,apply_no,bill_code,apply_money,settle_bill_no,del_flag,#{applyId},#{applyNo},#{billCode},#{applyMoney},#{settleBillNo},#{delFlag},
insert into emis_writeoff_apply_detail (apply_id, apply_no, bill_code, apply_money, settle_bill_no, del_flag)
values
(#{item.applyId}, #{item.applyNo}, #{item.billCode}, #{item.applyMoney}, #{item.settleBillNo}, '0')
update emis_writeoff_apply_detail
apply_id = #{applyId},apply_no = #{applyNo},bill_code = #{billCode},apply_money = #{applyMoney},settle_bill_no = #{settleBillNo},
where id = #{id}
update emis_writeoff_apply_detail set del_flag = '2' where id = #{id}
update emis_writeoff_apply_detail set del_flag = '2' where apply_id = #{applyId}
update emis_writeoff_apply_detail set del_flag = '2' where id in
#{id}