package com.yem.wm.im.delivernotice.op;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.List;

import static com.yem.wm.im.delivernotice.op.DeliverNoticeUnAuditOp.setValueToStorage;

/**
 * @author xwudd
 * @Description
 * @date 16:56
 **/
public class DeliverNoticeNewUnAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_isaccgift");
        fieldKeys.add("yem_contractallamt");
        fieldKeys.add("yem_contractadvanceamt");
        fieldKeys.add("yem_deliveramt");
        fieldKeys.add("yem_thisdeliveramt");
        fieldKeys.add("yem_thisrealityamt");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject dataEntitie : dataEntities) {
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            delDebCredNote(dataEntitie);
            emptyRealityAmt(dataEntitie);
            setValueToStorage(dataEntitie);
            SaveServiceHelper.save(new DynamicObject[]{dataEntitie});
        }
    }

    /**
     * @description 反审核删除相关贷记单
     * @author xwudd
     * @param[1] dataEntitie
     * return
     * @time 2023/11/10 17:26
     */

    public void delDebCredNote(DynamicObject dataEntitie){
        boolean isAccGift = dataEntitie.getBoolean("yem_isaccgift");
        if (isAccGift) {
            String name = dataEntitie.getDynamicObjectType().getName();
            long id = dataEntitie.getLong("id");
            QFilter qFilter = new QFilter("yem_sourcebilltype", QCP.equals, name);
            qFilter.and(new QFilter("yem_sourcebillid",QCP.equals,id));
            qFilter.and(new QFilter("billstatus",QCP.equals,"C"));
            DynamicObject[] debCredNotes = BusinessDataServiceHelper.load("yem_debcrednote", "id", qFilter.toArray());
            for(DynamicObject debCredNote : debCredNotes){
                String debCredNoteNumber = debCredNote.getDynamicObjectType().getName();
                OperationResult unAudit = OperationServiceHelper.executeOperate("unaudit", debCredNoteNumber, new Object[]{debCredNote.getPkValue()}, OperateOption.create());
                if(unAudit.isSuccess()) {
                    OperationResult del = OperationServiceHelper.executeOperate("delete", debCredNoteNumber, new Object[]{debCredNote.getPkValue()}, OperateOption.create());
                    if(del.isSuccess()){

                    }else{
                        List<IOperateInfo> allErrorOrValidateInfo = unAudit.getAllErrorOrValidateInfo();
                        String message = unAudit.getMessage();
                        throw new KDBizException("关联生成的贷记单删除失败," + message + "，" + allErrorOrValidateInfo);
                    }
                }else{
                    List<IOperateInfo> allErrorOrValidateInfo = unAudit.getAllErrorOrValidateInfo();
                    String message = unAudit.getMessage();
                    throw new KDBizException("关联生成的贷记单反审核失败," + message + "，" + allErrorOrValidateInfo);
                }
            }
        }
    }

    /**
     * @description 清空欠款相关字段
     * @author xwudd
     * @param[1] dataEntitie
     * return
     * @time 2023/11/16 9:45
     */

    public void emptyRealityAmt(DynamicObject dataEntitie){
        dataEntitie.set("yem_contractallamt",null);//合同所有收款
        dataEntitie.set("yem_contractadvanceamt",null);//合同的预收款
        dataEntitie.set("yem_deliveramt",null);//已发货金额
        dataEntitie.set("yem_thisdeliveramt",null);//本次发货尾款
        dataEntitie.set("yem_thisrealityamt",null);//本次发货实际收款总欠款金额
    }
}
