package nckd.jimin.jyyy.fi.plugin.operate;

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.validate.AbstractValidator;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.DepSecretaryConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DepSecretaryOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add(DepSecretaryConstant.NCKD_SECRETARY);
        e.getFieldKeys().add(DepSecretaryConstant.ENTITYID_ENTRY);
        e.getFieldKeys().add(DepSecretaryConstant.NCKD_ORG);
        e.getFieldKeys().add(DepSecretaryConstant.NCKD_REMARK);
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                ExtendedDataEntity[] dataEntitys = this.getDataEntities();
                for (ExtendedDataEntity dataEntity : dataEntitys) {
                    // 校验存在重复的部门名称
                    DynamicObject billInfo = dataEntity.getDataEntity();
                    DynamicObjectCollection entryCol = billInfo.getDynamicObjectCollection(DepSecretaryConstant.ENTITYID_ENTRY);
                    // 查询重复的部门
                    Map<String,List<String>> repeatDept = new HashMap<>();
                    for(DynamicObject entry : entryCol){
                        DynamicObject dept = entry.getDynamicObject(DepSecretaryConstant.NCKD_ORG);
                        if(dept != null){
                            String deptName = dept.getString("fullname");
                            String seqStr = String.format("第%s行",dept.getInt("seq") + 1);
                            if(repeatDept.containsKey(deptName)){
                                List<String> seqList = repeatDept.get(deptName);
                                seqList.add(seqStr);
                            }else{
                                List<String> seqList = new ArrayList<>();
                                seqList.add(seqStr);
                                repeatDept.put(deptName,seqList);
                            }
                        }
                    }

                    StringBuffer showMsg = new StringBuffer();
                    for(Map.Entry<String,List<String>> entry : repeatDept.entrySet()){
                        if(entry.getValue().size() > 1){
                            showMsg.append("部门名称重复：")
                                    .append(entry.getKey())
                                    .append("(").append(entry.getValue().stream().collect(Collectors.joining(","))).append(")").append("\r\n");
                        }
                    }
                    if(showMsg.length() > 0){
                        this.addErrorMessage(dataEntity,showMsg.toString());
                        return;
                    }
                }
            }
        });
    }
}
