package nckd.jxccl.opmc.pm.plugin.operate;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName: ExaminProjectResultOpPlugin
 * @Description: 绩效结果操作插件
 * @Author: PJJ
 * @Date: 2026/1/9 上午11:16
 * @Version: 1.0
 */
public class ExaminProjectResultOpPlugin extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().addAll(this.billEntityType.getAllFields().keySet());
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        String operationKey = e.getOperationKey();
        if (StringUtils.equals(operationKey, "submit")) {
            DynamicObject[] dataEntities = e.getDataEntities();
            //提示语句
            StringBuffer sb = new StringBuffer();
            sb.append("以下员工排名为空，请填写备注!\n");
            Boolean submit = false;
            for (DynamicObject baseData : dataEntities) {
                String userName = baseData.getLocaleString("nckd_user.name").getLocaleValue_zh_CN();
                //备注
                String remark = baseData.getString("nckd_remark");
                DynamicObjectCollection entry = baseData.getDynamicObjectCollection("nckd_entryentity");
                //排名
                List<String> collect = entry.stream().filter(i -> i.getString("nckd_examine_dimension.number").equals("JT-00012")).map(i -> i.getString("nckd_result")).collect(Collectors.toList());
                //排名为空且备注未填写时校验
                if(StringUtils.isBlank(collect.get(0)) && StringUtils.isBlank(remark)){
                    submit = true;
                    sb.append(userName).append("；");
                }
            }
            if( submit ){
                e.setCancel(true);
                e.setCancelMessage(sb.toString());
            }
        }
    }
}
