package yem.wm.bd.attachmenttype.opplugin.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import yem.base.common.utils.DynamicObjectUtil;

/**
 * @Description 附件类型
 * @Author: LiuHB
 * @CreateTime: 2025-02-19  10:23
 */
public class OnlyoneIsdefaultOpPlugin extends AbstractOperationServicePlugIn {

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        DynamicObject[] attachmenttypes = e.getDataEntities();
        for (DynamicObject attachmenttype : attachmenttypes) {
            boolean yemIsdefault = attachmenttype.getBoolean("yem_isdefault");
            if(yemIsdefault){
                long id = attachmenttype.getLong("id");
                DynamicObject yemBill = attachmenttype.getDynamicObject("yem_bill");
                String selectfields = DynamicObjectUtil.getSelectfields("yem_bd_attachmenttype", false);
                QFilter qFilter = new QFilter("id", QCP.not_equals, id);
                qFilter.and("yem_bill.number",QCP.equals,yemBill.getString("number"));
                DynamicObject[] bdAttachmenttypes = BusinessDataServiceHelper.load("yem_bd_attachmenttype", selectfields, qFilter.toArray());
                for (DynamicObject bdAttachmenttype : bdAttachmenttypes) {
                    bdAttachmenttype.set("yem_isdefault",false);
                }
                SaveServiceHelper.save(bdAttachmenttypes);
            }


        }

    }
}
