package nckd.jxccl.hrmp.hbpm.business.hr.service.impl;

import kd.bos.common.enums.EnableEnum;
import kd.bos.consts.OrgViewTypeConst;
import kd.bos.context.RequestContext;
import kd.bos.form.IFormView;
import kd.bos.form.container.Container;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.permission.api.HasPermOrgResult;
import kd.bos.servicehelper.model.PermissionStatus;
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
import kd.bos.servicehelper.permission.PermissionServiceHelper;
import kd.hr.hbp.common.util.HRStringUtils;
import kd.sdk.hr.hdm.common.enums.reg.RegBillStatusEnum;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

import java.util.EventObject;
import java.util.List;
import java.util.Objects;

/**
 * 岗位申请单前置绑定数据服务
 * @from: kd.hr.homs.business.domain.batchbill.service.impl.AdminOrgBatchBillBeforeBindDataService
 * @author: jtd
 * @date: 2025/12/30 22:05
 */
public class PositionBillBeforeBindDataService extends PositionBillBaseService {
    private static final Log LOGGER = LogFactory.getLog(PositionBillBeforeBindDataService.class);

    public PositionBillBeforeBindDataService(IFormView view) {
        super(view);
    }

    public void beforeBindData(EventObject e) {
        changePageCacheFromWhatVie();
        getView().getPageCache().put(PositionBillConstant.PC_ISLIST, EnableEnum.YES.getCode());
        if (HRStringUtils.equals(getView().getFormShowParameter().getAppId(), PositionBillConstant.WFTASK_APP)) {
            Container baseInfo = getView().getControl(PositionBillConstant.FS_BASEINFO_KEY);
            baseInfo.setCollapse(true);
        } else {
            Container baseInfo = getView().getControl(PositionBillConstant.FS_BASEINFO_KEY);
            baseInfo.setCollapse(false);
        }

        if (getModel().getDataEntity(true).getDynamicObject(PositionBillConstant.ORG_KEY) == null) {
            getView().getModel().setValue(PositionBillConstant.ORG_KEY, OrgUnitServiceHelper.getRootOrgId());
        }

        String billstatus = getModel().getDataEntity().getString(PositionBillConstant.BILL_STATUS_KEY);
        // 显示/隐藏 查看流程图
        if (HRStringUtils.equalsAny(billstatus, RegBillStatusEnum.ALREADYSUBMIT.getCode(), RegBillStatusEnum.APPROVEPASSED.getCode(), RegBillStatusEnum.APPROVING.getCode(), RegBillStatusEnum.APPROVEREJECTED.getCode()) && getModel().getDataEntity().getBoolean(PositionBillConstant.ISEXISTSWORKFLOW_KEY)) {
            getView().setVisible(true, new String[]{PositionBillConstant.BAR_VIEWFLOWCHART_KEY});
        } else {
            getView().setVisible(false, new String[]{PositionBillConstant.BAR_VIEWFLOWCHART_KEY});
        }

    }

    private void changePageCacheFromWhatVie() {
        if (Boolean.TRUE.toString().equals(getView().getFormShowParameter().getCustomParam(PositionBillConstant.CP_FROMLIST))) {
            Object businessUnit = getView().getFormShowParameter().getCustomParam(PositionBillConstant.CP_BUSINESSUNIT);
            if (businessUnit != null) {
                getModel().setValue(PositionBillConstant.ORG_KEY, businessUnit);
            } else if (!Boolean.TRUE.toString().equals(getView().getFormShowParameter().getCustomParam(PositionBillConstant.CP_ISFROMBILLCLICK))) {
                HasPermOrgResult hrPermOrg = PermissionServiceHelper.getAllPermOrgs(RequestContext.get().getCurrUserId(), OrgViewTypeConst.HR_OD, PositionBillConstant.HOMS_APP, PositionBillConstant.HAOS_ADMINORGDETAIL_ENTITY, PermissionStatus.View, false);
                List<Long> hasPermOrgs = hrPermOrg.getHasPermOrgs();
                if (!Objects.isNull(hasPermOrgs) && hasPermOrgs.size() != 0) {
                    if (hasPermOrgs.contains(RequestContext.get().getOrgId())) {
                        getModel().setValue(PositionBillConstant.ORG_KEY, RequestContext.get().getOrgId());
                    } else {
                        getModel().setValue(PositionBillConstant.ORG_KEY, hasPermOrgs.get(0));
                    }
                }
            }

            getView().getFormShowParameter().setCustomParam(PositionBillConstant.CP_FROMLIST, Boolean.FALSE.toString());
            getView().cacheFormShowParameter();
        } else if (Boolean.TRUE.toString().equals(getView().getPageCache().get(PositionBillConstant.PC_ISFROMIMPORT)) || getView().getParentView() != null && (PositionBillConstant.NCKD_POSTMGR_APPHOME_ENTITY.equals(getView().getParentView().getEntityId()) || getView().getParentView().getEntityId().startsWith("wf_"))) {
            if (getView().getParentView() != null && PositionBillConstant.NCKD_POSTMGR_APPHOME_ENTITY.equals(getView().getParentView().getEntityId()) && getView().getPageCache().get(PositionBillConstant.PC_ISFROMHOMEPAGE) == null) {
                HasPermOrgResult hrPermOrg = PermissionServiceHelper.getAllPermOrgs(RequestContext.get().getCurrUserId(), OrgViewTypeConst.HR_OD, PositionBillConstant.HOMS_APP, PositionBillConstant.HAOS_ADMINORGDETAIL_ENTITY, PermissionStatus.View, false);
                List<Long> hasPermOrgs = hrPermOrg.getHasPermOrgs();
                if (!Objects.isNull(hasPermOrgs) && hasPermOrgs.size() != 0) {
                    if (hasPermOrgs.contains(RequestContext.get().getOrgId())) {
                        getModel().setValue(PositionBillConstant.ORG_KEY, RequestContext.get().getOrgId());
                    } else {
                        getModel().setValue(PositionBillConstant.ORG_KEY, hasPermOrgs.get(0));
                    }

                    getView().getPageCache().put(PositionBillConstant.PC_ISFROMHOMEPAGE, Boolean.TRUE.toString());
                }
            }

            getView().getPageCache().put(PositionBillConstant.PC_ISFROMIMPORT, Boolean.FALSE.toString());
        } else {
            String parentView = getView().getParentView() != null ? getView().getParentView().getEntityId() : "null";
            LOGGER.info("method changePageCacheFromWhatVie parentView is [{}]", parentView);
        }

    }
}
