package nckd.jimin.jyyy.hr.hom.operate;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

/**
 * Module           :核心人力云-入职管理-入职人员管理-入职申请
 * Description      :入职申请/办理保存插件
 * @author Tyx
 * @date  2025/5/14
 * 标识 hom_onbrdinfo
 */
public class OnbrdInfoSaveOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("candidate");
        e.getFieldKeys().add("nckd_companyemail");
    }

    /**
     * 在标品保存之后处理公司电子邮箱字段，把公司电子邮箱反写回候选人对应的【候选人联系信息表】
     * 然后再通过标品的
     * @param e
     */
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        //查询当前版本的候选人联系信息表
        DynamicObject[] bills = e.getDataEntities();
        for(DynamicObject bill : bills) {
            //候选人ID
            long candidateId = bill.getLong("candidate.id");
            //查询候选人联系信息表
            QFilter filter = new QFilter("candidate.id", QCP.equals, candidateId);
            filter.and("iscurrentversion", QCP.equals, "1");
            DynamicObject contactInfo = BusinessDataServiceHelper.loadSingle("hcf_cancontactinfo", new QFilter[]{filter});
            if(!ObjectUtils.isEmpty(contactInfo)) {
                contactInfo.set("nckd_companyemail", bill.getString("nckd_companyemail"));
                SaveServiceHelper.save(new DynamicObject[]{contactInfo});
            }
        }
    }
}