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

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.yem.wm.bc.companytitle.form.CompanyTitleEdit;
import com.yem.wm.utils.FunctionalCommon;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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 java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张立攀
 * @version 1.0
 * @description
 * @date 2023-08-07 18:53
 */
public class DebCredUnAuditPlugin extends AbstractOperationServicePlugIn {
    private static final Log log = LogFactory.getLog(CompanyTitleEdit.class);
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEnties = e.getDataEntities();
        for(DynamicObject temp:dataEnties){
            String billtype = temp.getString("yem_billtype.number");
            if("yem_debcrednote_d".equals(billtype)){
                DynamicObject customer = temp.getDynamicObject("yem_customer");
                if(customer != null) {
                    List<QFilter> list = new ArrayList<>();
                    list.add(new QFilter("yem_customer.id", QCP.equals, customer.getLong("id")));
                    list.add(new QFilter("billstatus", QCP.equals, "C"));
                    DynamicObject loadColl = BusinessDataServiceHelper.loadSingle("yem_spcapitalpool", "yem_spcapitalpool_o.yem_origintype", list.toArray(new QFilter[0]));
                    if(loadColl != null){
                        loadColl = BusinessDataServiceHelper.loadSingle(loadColl.getPkValue(), loadColl.getDynamicObjectType().getName());
                        DynamicObjectCollection yem_spcapitalpool_o =  loadColl.getDynamicObjectCollection("yem_spcapitalpool_o");

                        for(DynamicObject temps:yem_spcapitalpool_o){
                            //有来源、有去向反审核
                            DynamicObjectCollection dynamicObjectCollection = temps.getDynamicObjectCollection("yem_spcapitalpool_o_t");
                            if(temp.getPkValue() != null && temp.getPkValue().equals(temps.getLong("yem_sourcebillid_o"))){
                                String yemOriginno = temps.getString("yem_originno");
                                for(DynamicObject dt:dynamicObjectCollection){
                                    String yemTowardno = dt.getString("yem_towardno");
                                    if(!yemOriginno.equals(yemTowardno)){
                                        throw new KDBizException("此【贷记单】无法反审核");
                                    }
                                }
                                if(yem_spcapitalpool_o != null && temps != null){
                                    yem_spcapitalpool_o.remove(temps);
                                }
                                break;
                            }else {
                                //仅有去向
                                String billno = temp.getString("billno");
                                DynamicObject deleteOb = null;
                                for(DynamicObject dt:dynamicObjectCollection){
                                    String yemTowardno = dt.getString("yem_towardno");
                                    if(billno.equals(yemTowardno)){
                                        deleteOb = dt;

                                    }
                                }
                                if(dynamicObjectCollection != null && deleteOb != null){
                                    BigDecimal yemTowardamtEnd = BigDecimal.ZERO;
                                    BigDecimal yemTowardamtbaseEnd = BigDecimal.ZERO;
                                    dynamicObjectCollection.remove(deleteOb);
                                    for(DynamicObject dt:dynamicObjectCollection){
                                        BigDecimal yemTowardamt = dt.getBigDecimal("yem_towardamt");
                                        BigDecimal yemTowardamtbase = dt.getBigDecimal("yem_towardamtbase");
                                        yemTowardamtEnd = yemTowardamtEnd.add(yemTowardamt);
                                        yemTowardamtbaseEnd = yemTowardamtbaseEnd.add(yemTowardamtbase);
                                    }
                                    BigDecimal yem_amount = temps.getBigDecimal("yem_originamt");
                                    BigDecimal yem_amountbase = temps.getBigDecimal("yem_amountbase");
//                                    BigDecimal yem_amount_sub = temps.getBigDecimal("yem_amount_sub");
//                                    BigDecimal yem_amountbase_sub = temps.getBigDecimal("yem_amountbase_sub");
//
                                    temps.set("yem_chargeoffamt",yemTowardamtEnd);
                                    temps.set("yem_chargeoffamtbase",yemTowardamtbaseEnd);
                                    temps.set("yem_residueavaamt",yem_amount.subtract(yemTowardamtEnd));
                                    temps.set("yem_residueavaamtbase",yem_amountbase.subtract(yemTowardamtbaseEnd));
                                }
                            }
                        }


                    }else {
                        return;
                    }
                    FunctionalCommon.countTotalSpCapitalPool(loadColl);
                    OperationResult operationResult1 = OperationServiceHelper.executeOperate("save", "yem_spcapitalpool", new DynamicObject[]{loadColl}, OperateOption.create());
                    if(!operationResult1.isSuccess()){
//                        log.error("保存成功"+operationResult1.getMessage());
                    }
                }
            }


        }
    }


    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().add("yem_billtype");
        e.getFieldKeys().add("yem_customer");
        e.getFieldKeys().add("yem_sourcebillid_o");
        e.getFieldKeys().add("yem_spcapitalpool_o");
        e.getFieldKeys().add("billno");
        e.getFieldKeys().add("yem_originamt");
        e.getFieldKeys().add("yem_amountbase");
        e.getFieldKeys().add("yem_amount_sub");
        e.getFieldKeys().add("yem_amountbase_sub");
        e.getFieldKeys().add("yem_chargeoffamt");
        e.getFieldKeys().add("yem_chargeoffamtbase");
        e.getFieldKeys().add("yem_residueavaamt");
        e.getFieldKeys().add("yem_residueavaamtbase");
        e.getFieldKeys().add("billstatus");
    }
}
