package nckd.jxccl.hr.psms.plugin.form.other;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.IFormView;
import kd.bos.form.IPageCache;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.mvc.SessionManager;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.enums.psms.TypeStateEnum;
import nckd.jxccl.base.common.utils.DateUtil;
import nckd.jxccl.base.common.utils.ShowOperExecuteResult;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.base.entity.helper.EntityHelper;
import nckd.jxccl.base.hrpi.helper.EmpPosOrgRelHelper;
import nckd.jxccl.base.org.helper.OrgHelper;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 新建任命
* 实体标识：nckd_newapptpopup
* @author W.Y.C
* @date 2025/12/4 18:50
* @version 1.0
*/
public class NewApptPopupFormPlugin extends AbstractFormPlugin implements Plugin {

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        String operateKey = e.getOperateKey();
        if(e.getOperationResult() != null && e.getOperationResult().isSuccess()){
            if(FormConstant.AFFIRM_OP.equalsIgnoreCase(operateKey)) {

                //新建管理任命
                DynamicObjectCollection entryColl = this.getModel().getDataEntity(true).getDynamicObjectCollection(FormConstant.NCKD_ENTRYENTITY);
                List<DynamicObject> newEntityList = new ArrayList<>();
                for (DynamicObject entry : entryColl) {
                    DynamicObject newEntity = EntityHelper.newEntity(PositionStructureConstant.NCKD_PERSONPOSFILE);
                    DynamicObject person = entry.getDynamicObject(PositionStructureConstant.NCKD_PERSON);
                    long personId = person.getLong(FormConstant.ID_KEY);
                    String personName = person.getString(FormConstant.NAME_KEY);
                    newEntity.set(FormConstant.NCKD_PERSON, entry.get(FormConstant.NCKD_PERSON));
                    Date beginDate = entry.getDate(PositionStructureConstant.NCKD_BEGINDATE);
                    LocalDateTime beginDateDateTime = DateUtil.toLocalDateTime(beginDate);
                    newEntity.set(PositionStructureConstant.NCKD_BEGINDATE, beginDate);
                    newEntityList.add(newEntity);
                }
                OperateOption operateOption = OperateOption.create();
                operateOption.setVariableValue(PositionStructureConstant.NCKD_TYPESTATE, TypeStateEnum.MANAGEMENT_SEQUENCE_EMPLOYMENT.getCode());
                OperationResult operationResult = OperationServiceHelper.executeOperate(
                        "savenewappointment",
                        PositionStructureConstant.PERSONPOSFILE_ENTITYID,
                        newEntityList.toArray(new DynamicObject[0]),
                        OperateOption.create()
                );
                if (!operationResult.isSuccess()) {
                    this.getView().showForm(ShowOperExecuteResult.getOperResultForm("新建任命", this.getView().getParentView(),this.getPageCache(), operationResult));
                } else {
                    this.getView().returnDataToParent("true");
                    this.getView().getParentView().showSuccessNotification("新建任命成功");
                    this.getView().close();
                }
            }
        }
    }
}