package com.yem.wm.fm.gathering.op;

import com.yem.wm.es.contactbook.op.ContactBookSaveOp;
import com.yem.wm.fm.gathering.validator.GatheringSubmitOPValidator;
import com.yem.wm.fm.gathering.validator.GatheringValidator;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;

/**
 * @Description: TODO
 * @Date: 2024/5/24 20:02
 * @Created: by ZZSLL
 */

public class GatheringProSaveOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().add("yem_receiptdetail");
        e.getFieldKeys().add("yem_claimdetail");
        e.getFieldKeys().add("yem_receivableamt");
        e.getFieldKeys().add("yem_salcontractno");
        e.getFieldKeys().add("yem_claimamt");
        e.getFieldKeys().add("yem_salcontractamt");
        e.getFieldKeys().add("yem_sumreceivableamt");
        e.getFieldKeys().add("yem_sumfundsreceived");
        e.getFieldKeys().add("yem_claimstate");
        e.getFieldKeys().add("yem_sumreceivableamt");
        e.getFieldKeys().add("yem_sumreceivableamtba");
        e.getFieldKeys().add("yem_receivableamtbase");
        e.getFieldKeys().add("yem_shippingdetailsno");
        e.getFieldKeys().add("yem_shipclaimamt");
        e.getFieldKeys().add("yem_shipamt");

    }


    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        e.addValidator(new GatheringSubmitOPValidator());
        e.addValidator(new GatheringValidator());
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {

        super.beginOperationTransaction(e);
        DynamicObject[] entities = e.getDataEntities();
        for (DynamicObject bill : entities) {
            //应收金额
            BigDecimal sumreceivableamt = bill.getBigDecimal("yem_sumreceivableamt");
            //实收金额
            BigDecimal sumfundsreceived = bill.getBigDecimal("yem_sumfundsreceived");
            if (sumreceivableamt.compareTo(BigDecimal.ZERO)==0){
                bill.set("yem_claimstate","A");
            }
            if (sumreceivableamt.compareTo(sumfundsreceived) == -1&& sumreceivableamt.compareTo(BigDecimal.ZERO)!=0) {
                bill.set("yem_claimstate","B");
            }
            if (sumreceivableamt.compareTo(sumreceivableamt) < -1) {
                bill.set("yem_claimstate","C");
            }
            Long pkValue = (Long) bill.getPkValue();
            AttachmentServiceHelper.remove("yem_gathering", pkValue);
            DynamicObject to = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), "yem_gathering");
            ContactBookSaveOp.uploadAttachmentPanel(bill, to);
        }
    }
}
