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

import com.yem.wm.fm.gathering.validator.UnCreatePlusMinusValidator;
import com.yem.wm.utils.YEM;
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.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.exception.KDException;
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;

/**
 * @author xwudd
 * @Description
 * @date 17:19
 **/
public class UnCreatePlusMinusOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_billtype");
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new UnCreatePlusMinusValidator());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject dataEntitie : dataEntities){
            String name = dataEntitie.getDataEntityType().getName();
            DynamicObject[] plusMinus = BusinessDataServiceHelper.load(name, "id,billno,billstatus,yem_billtype",
                    new QFilter[]{new QFilter("yem_sourcebilltype", QCP.equals, name),
                            new QFilter("yem_sourcebillid", QCP.equals, dataEntitie.getPkValue())});
            for(DynamicObject plusMinu : plusMinus){
                String billNo = plusMinu.getString("billno");
                DynamicObject billType = plusMinu.getDynamicObject("yem_billtype");
                String billTypeName = YEM.isNotEmpty(billType)?billType.getString("name"):"";
                String plusMinuName = plusMinu.getDynamicObjectType().getName();
                String billStatus = plusMinu.getString("billstatus");
                switch (billStatus){
                    case "A" :
                        OperationResult delete = OperationServiceHelper.executeOperate("delete", plusMinuName, new Object[]{plusMinu.getPkValue()}, OperateOption.create());
                        if(!delete.isSuccess()){
                            String message = delete.getMessage();
                            List<IOperateInfo> allErrorOrValidateInfo = delete.getAllErrorOrValidateInfo();
                            throw new KDBizException(String.format("%s【%s】删除失败，%s，%s",billTypeName,billNo,message,allErrorOrValidateInfo));
                        }
                        break;
                    case "B" :
                        OperationResult unSubmit = OperationServiceHelper.executeOperate("unsubmit", plusMinuName, new Object[]{plusMinu.getPkValue()}, OperateOption.create());
                        if(unSubmit.isSuccess()){
                            delete = OperationServiceHelper.executeOperate("delete", plusMinuName, new Object[]{plusMinu.getPkValue()}, OperateOption.create());
                            if(!delete.isSuccess()){
                                String message = delete.getMessage();
                                List<IOperateInfo> allErrorOrValidateInfo = delete.getAllErrorOrValidateInfo();
                                throw new KDBizException(String.format("%s【%s】删除失败，%s，%s",billTypeName,billNo,message,allErrorOrValidateInfo));
                            }
                        }else{
                            String message = unSubmit.getMessage();
                            List<IOperateInfo> allErrorOrValidateInfo = unSubmit.getAllErrorOrValidateInfo();
                            throw new KDBizException(String.format("%s【%s】删除失败，%s，%s",billTypeName,billNo,message,allErrorOrValidateInfo));
                        }
                        break;
                    case "C" :
                        OperationResult unAudit = OperationServiceHelper.executeOperate("unaudit", plusMinuName, new Object[]{plusMinu.getPkValue()}, OperateOption.create());
                        if(unAudit.isSuccess()){
                            delete = OperationServiceHelper.executeOperate("delete", plusMinuName, new Object[]{plusMinu.getPkValue()}, OperateOption.create());
                            if(!delete.isSuccess()){
                                String message = delete.getMessage();
                                List<IOperateInfo> allErrorOrValidateInfo = delete.getAllErrorOrValidateInfo();
                                throw new KDBizException(String.format("%s【%s】删除失败，%s，%s",billTypeName,billNo,message,allErrorOrValidateInfo));
                            }
                        }else{
                            String message = unAudit.getMessage();
                            List<IOperateInfo> allErrorOrValidateInfo = unAudit.getAllErrorOrValidateInfo();
                            throw new KDBizException(String.format("%s【%s】删除失败，%s，%s",billTypeName,billNo,message,allErrorOrValidateInfo));
                        }
                        break;
                }
            }
            dataEntitie.set("yem_bizstatus","A");
            SaveServiceHelper.save(new DynamicObject[]{dataEntitie});
        }
    }
}
