package nckd.jxccl.swc.init.plugin.operate;

import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.validate.ErrorLevel;
import kd.bos.entity.validate.ValidationErrorInfo;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.swc.init.business.BaseMedicalAllowanceServiceImpl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 单据操作插件
 * author: turborao
 * date: 2025/10/27 13:05
 */
public class BaseMedicalAllowanceOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        //获取列表中选择的单据内码
        List<Long> billids = (List) Arrays.asList(e.getDataEntities()).stream().map((entityx) -> {
            return entityx.getLong("id");
        }).collect(Collectors.toList());

        if(billids != null || billids.size() > 0) {
            BaseMedicalAllowanceServiceImpl baseMedicalAllowanceService = new BaseMedicalAllowanceServiceImpl();
            Map<String, String> result= baseMedicalAllowanceService.calcBaseMedicalAllowanceForPersons(billids);

            if(result.get("code").equals("200")){
                this.operationResult.setShowMessage(true);
                this.operationResult.setSuccess(true);
                this.operationResult.setMessage(result.get("msg"));
            }else{
                this.operationResult.addErrorInfo(new ValidationErrorInfo("", null,
                        0, 0, "同步失败", "提示信息", result.get("msg"), ErrorLevel.Error));
            }
        }else{
            this.operationResult.setShowMessage(true);
            this.operationResult.setSuccess(false);
            this.operationResult.setMessage("没选中数据！");
        }
    }
}