package com.yem.wm.bd.enginetoorg.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.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 发动机型号厂商对照表
 * @Date: 2024/6/28 14:09
 * @Created: by ZZSLL
 */

public class EngineToOrgSubmitOp extends AbstractOperationServicePlugIn {

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
                for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
                    DynamicObject dataEntity = extendedDataEntity.getDataEntity();
                    dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDataEntityType().getName());

                    validatorRepeatEngineNo(extendedDataEntity, dataEntity);
                }
            }

            /**
             * 一个厂商会有多个型号，一个型号只对应一个厂商
             * @param extendedDataEntity
             * @param dataEntity
             */
            private void validatorRepeatEngineNo(ExtendedDataEntity extendedDataEntity, DynamicObject dataEntity) {

                StringBuilder sb = new StringBuilder();
                DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_enginedetail");
                Set<String> type = new HashSet<>();
                String currentNumber = dataEntity.getString("number");
                for (DynamicObject dynamicObject : collection) {
                    int index = collection.indexOf(dynamicObject);
                    String engineno = dynamicObject.getString("yem_engineno");
                    if (type.contains(engineno)) {
                        int sameLineNo = getSameLineNo(collection, engineno);
                        sb.append(String.format("明细信息第 [ %s ] 行发动机型号 [ %s ] 与 [ %s ] 明细信息第 [ %s ] 行重复，请检查数据！！", index + 1, engineno, currentNumber, sameLineNo)).append("\r\n");
                    }
                    type.add(engineno);
                }

                QFilter qFilter = new QFilter("yem_enginedetail.yem_engineno", QCP.in, type);
                qFilter.and("enable", QCP.equals, "1");
                qFilter.and("status", QCP.in, new String[]{"C", "B"});
                DynamicObject[] load = BusinessDataServiceHelper.load("yem_bd_enginetoorg", "billno, yem_enginedetail.yem_engineno", qFilter.toArray());
                for (DynamicObject dynamicObject : load) {
                    String number = dynamicObject.getString("number");
                    DynamicObjectCollection existCollection = dynamicObject.getDynamicObjectCollection("yem_enginedetail");
                    for (DynamicObject object : existCollection) {
                        int existIdx = existCollection.indexOf(object);
                        String engineno = object.getString("yem_engineno");
                        int lineNo = getSameLineNo(collection, engineno);

                        if (lineNo != -1) {
                            sb.append(String.format("明细信息第 [ %s ] 行发动机型号 [ %s ] 与 [ %s ] 明细信息第 [ %s ] 行重复，请检查数据！！", lineNo, engineno, number, existIdx + 1)).append("\r\n");
                        }
                    }
                }

                if (sb.length() > 0) {
                    this.addErrorMessage(extendedDataEntity, sb.toString());
                }
            }

            private int getSameLineNo(DynamicObjectCollection collection, String engineno) {
                for (DynamicObject dynamicObject : collection) {
                    int index = collection.indexOf(dynamicObject);
                    String existNo = dynamicObject.getString("yem_engineno");
                    if (existNo.equals(engineno)) {
                        return index + 1;
                    }
                }
                return -1;
            }
        });
    }
}
