package nckd.jxccl.hr.hdm.plugin.form.transfer;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.QueryEntityType;
import kd.bos.form.ConfirmCallBackListener;
import kd.bos.form.ConfirmTypes;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.MessageBoxResult;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.MetadataServiceHelper;
import kd.hr.hbp.business.servicehelper.HRQueryEntityHelper;
import kd.hr.hbp.common.util.HRDynamicObjectUtils;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;
import nckd.jxccl.hr.hdm.common.transfer.TransferApplyBillConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 调动基础单据表单插件
 * @entity: nckd_hdm_transferbase_ext
 * @author: jtd
 * @date: 2026/1/4 15:34
 */
public class TransferApplyBillFormPlugin extends AbstractFormPlugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);

        FormOperate formOperate = (FormOperate) args.getSource();
        String operateKey = formOperate.getOperateKey();
        switch (operateKey){
            case TransferApplyBillConstant.SAVE_OP:
                if (!HRStringUtils.equals(Boolean.TRUE.toString(), getView().getPageCache().get(TransferApplyBillConstant.FAMILY_RELATION_CONFIRM_PAGE_CACHE))) {
                    validateFamilyRelation(args);
                }
        }
    }

    private void validateFamilyRelation(BeforeDoOperationEventArgs args) {
        DynamicObject dataEntity = getModel().getDataEntity();
        // 获取调出公司
        DynamicObject bcompany = dataEntity.getDynamicObject(TransferApplyBillConstant.BCOMPANY_KEY);
        // 获取调入公司
        DynamicObject acompany = dataEntity.getDynamicObject(TransferApplyBillConstant.ACOMPANY_KEY);
        // 判断是否跨二级单位调动
        if (HRDynamicObjectUtils.compareValues(bcompany, acompany)) {
            return;
        }

        // 获取调动员工
        DynamicObject transferEmployee = dataEntity.getDynamicObject(TransferApplyBillConstant.BB_EM_TID);
        // 获取调入公司内员工近亲属关系
        QueryEntityType queryEntityType = (QueryEntityType) MetadataServiceHelper.getDataEntityType(TransferApplyBillConstant.NCKD_HRPI_GETFAMILYRELATION_ENTITY);
        // 查询 员工编码、员工姓名、亲属关系ID、亲属关系编码、亲属关系名称
        String queryFields = QueryFieldBuilder.create().add(TransferApplyBillConstant.EMPLOYEE_EMPNUMBER)
                .add(TransferApplyBillConstant.EMPLOYEE_NAME)
                .add(TransferApplyBillConstant.NCKD_RELATION_ID)
                .add(TransferApplyBillConstant.NCKD_RELATION_NUMBER)
                .add(TransferApplyBillConstant.NCKD_RELATION_NAME)
                .buildSelect();
        // 过滤 所属公司为调出公司 并且 近亲属员工为调动员工
        QFilter qFilter = new QFilter(String.join(".", TransferApplyBillConstant.HRPI_EMPPOSORGREL, TransferApplyBillConstant.COMPANY_KEY, TransferApplyBillConstant.NUMBER_KEY), QCP.equals, acompany.getString(TransferApplyBillConstant.NUMBER_KEY))
                .and(String.join(".", TransferApplyBillConstant.NCKD_EMPID_KEY, TransferApplyBillConstant.NUMBER_KEY), QCP.equals, transferEmployee.getString(TransferApplyBillConstant.NUMBER_KEY));
        DynamicObjectCollection familyRelationDys = HRQueryEntityHelper.getInstance().getQueryDyoColl(queryEntityType, queryFields, new QFilter[]{qFilter}, TransferApplyBillConstant.EMPLOYEE_EMPNUMBER);
        List<String> errorList = new ArrayList<String>();
        for (DynamicObject familyRelationDy : familyRelationDys) {
            // 获取近亲属员工姓名
            String empName = familyRelationDy.getString(TransferApplyBillConstant.EMPLOYEE_NAME);
            // 获取近亲属名称
            String relationName = familyRelationDy.getString(TransferApplyBillConstant.NCKD_RELATION_NAME);
            errorList.add(String.format("亲属姓名：%s，亲属关系：%s", empName, relationName));
        }

        if (!errorList.isEmpty()) {
            getView().showConfirm("近亲属关系", String.join(System.lineSeparator(), errorList), MessageBoxOptions.OKCancel, ConfirmTypes.Default, new ConfirmCallBackListener(TransferApplyBillConstant.FAMILY_RELATION_CONFIRM_CALLBACK));
            args.setCancel(true);
        }
    }

    @Override
    public void confirmCallBack(MessageBoxClosedEvent messageBoxClosedEvent) {
        super.confirmCallBack(messageBoxClosedEvent);

        switch (messageBoxClosedEvent.getCallBackId()) {
            case TransferApplyBillConstant.FAMILY_RELATION_CONFIRM_CALLBACK:
                if (messageBoxClosedEvent.getResult() == MessageBoxResult.Yes) {
                    getView().getPageCache().put(TransferApplyBillConstant.FAMILY_RELATION_CONFIRM_PAGE_CACHE, Boolean.TRUE.toString());
                    getView().invokeOperation(TransferApplyBillConstant.SAVE_OP);
                    getView().getPageCache().remove(TransferApplyBillConstant.FAMILY_RELATION_CONFIRM_PAGE_CACHE);
                }
        }
    }
}
