package nckd.jxccl.sit.hcsi.formplugin.web.file.attach;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.util.StringUtils;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.sit.hcsi.common.constant.SitConstant;

/**
 * Tyx 2025-10-14
 * 社保基数设置动态表单插件：【nckd_hcsi_filesibase_ext】
 *
 */
public class FileSIBasePluginEx extends AbstractFormPlugin implements Plugin {
    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
        String operateKey = afterDoOperationEventArgs.getOperateKey();
        OperationResult operationResult = afterDoOperationEventArgs.getOperationResult();
        switch (operateKey) {
            case "donothing_save":
                if (operationResult == null || operationResult.isSuccess()) {
                    IDataModel model = this.getModel();
                    DynamicObjectCollection entryCols = model.getDataEntity().getDynamicObjectCollection("entryentity");
                    // 保存社保基数成功之后，把对应字段反写到档案头上，并刷新父页面
                    Long fileSinSurStringId = (Long)this.getView().getFormShowParameter().getCustomParam("sinsurfile");
                    if (fileSinSurStringId != null) {
                        DynamicObject fileDyn = SitConstant.SINSURFILE_HELPER.loadOne(fileSinSurStringId);
                        for(DynamicObject entry : entryCols) {
                            String key = entry.getString("welfaretype.nckd_sinsurfilefields");
                            if(!StringUtils.isEmpty(key)) {
                                fileDyn.set(key, entry.getLong("nckd_sinsurdimension.id"));
                            }
                        }
                        //SaveServiceHelper.update(fileDyn);
                        SaveServiceHelper.saveOperate("hcsi_sinsurfile", new DynamicObject[]{fileDyn}, OperateOption.create());
                    }
                }
        }
    }
}