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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.List;

public class MerChantApplySaveOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("yem_merchantapply_e");
        keys.add("yem_standardid");
        keys.add("yem_groupid");
        keys.add("yem_easnumber");
        keys.add("yem_erpnumber");
        keys.add("yem_issave");
    }

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

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        for (ExtendedDataEntity dataEntity : e.getSelectedRows()) {
            DynamicObject billObj = dataEntity.getDataEntity();
            boolean yem_issave = billObj.getBoolean("yem_issave");
            if(!yem_issave){
                DynamicObjectCollection groupColl = billObj.getDynamicObjectCollection("yem_merchantapply_e");
                for (int i = 0; i < groupColl.size(); i++) {
                    DynamicObject dynamicObject = groupColl.get(i);
                    DynamicObject yem_standardid = dynamicObject.getDynamicObject("yem_standardid");
                    if (yem_standardid != null) {
                        if (yem_standardid.getString("number").equals("customerGroupStandard")) {
                            DynamicObject yem_groupid = dynamicObject.getDynamicObject("yem_groupid");
                            String groupNum = yem_groupid.getString("number");
                            DynamicObject obj = BusinessDataServiceHelper.newDynamicObject("bd_customer");
                            obj.set("group", yem_groupid);
                            //获取配置编码规则生成的编码
//                            CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(obj.getDataEntityType().getName(), obj, groupNum);
//                            String number = CodeRuleServiceHelper.getNumber(codeRule, obj);
//                            billObj.set("yem_easnumber", number);
//                            billObj.set("yem_erpnumber", number);
//                            billObj.set("yem_issave",true);
                        }
                    }
                }
            }
        }
    }
}
