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

import com.google.common.collect.Maps;
import com.kingdee.util.StringUtils;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.CloseCallBack;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.EntryGrid;
import kd.bos.list.ListShowParameter;
import kd.bos.org.utils.DynamicObjectUtils;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.MetadataServiceHelper;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.util.HRDateTimeUtils;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

import java.util.Date;
import java.util.Map;

/**
 * 岗位申请单服务类
 * @author: jtd
 * @date: 2025-10-31 14:33
 */
public class PositionBillService {

    private static final Map<String, String> changeTypeToLocaleMap = Maps.newHashMapWithExpectedSize(2);

    private static final Map<String, String> billStatusToLocaleMap = Maps.newHashMapWithExpectedSize(7);

    static {
        changeTypeToLocaleMap.put("add", "新增岗位");
        changeTypeToLocaleMap.put("change", "变更信息");

        billStatusToLocaleMap.put("B", "已提交");
        billStatusToLocaleMap.put("D", "审批中");
        billStatusToLocaleMap.put("C", "审批通过");
        billStatusToLocaleMap.put("E", "审批不通过");
        billStatusToLocaleMap.put("F", "已废弃");
        billStatusToLocaleMap.put("G", "待重新提交");
        billStatusToLocaleMap.put("LD", "已删除");
    }

    /**
     * 获取分录序号
     * @param iDataModel
     * @param changeType
     * @return
     */
    public static int getEntrySeq(IDataModel iDataModel, Long changeType) {
        Long billId = iDataModel.getDataEntity().getLong(PositionBillConstant.ID_KEY);
        HRBaseServiceHelper hrBaseServiceHelper = new HRBaseServiceHelper(PositionBillConstant.NCKD_POSITIONBILLENTRY);
        QFilter qFilter = new QFilter(PositionBillConstant.NCKD_BILLID, QCP.equals, billId);
        qFilter.and(PositionBillConstant.NCKD_CHANGETYPE, QCP.equals, changeType);
        String selectProps = String.join(",", new String[]{PositionBillConstant.ID_KEY, PositionBillConstant.NCKD_SEQUENCE});
        String order = String.format("%s desc", PositionBillConstant.NCKD_SEQUENCE);
        DynamicObject dynamicObject = hrBaseServiceHelper.queryOne(selectProps, qFilter.toArray(), order);
        return dynamicObject == null ? 1 : dynamicObject.getInt(PositionBillConstant.NCKD_SEQUENCE) + 1;
    }

    /**
     * 打开表单界面
     * @param iFormView
     * @param operation
     * @param pluginName
     * @param filter
     */
    public static void openViewForm(IFormView iFormView, String operation, String pluginName, QFilter filter) {
        if (iFormView.getModel().getValue("org") == null) {// 179
            iFormView.showTipNotification("请先填写组织体系管理组织。");
        } else {
            HRBaseServiceHelper orgBatchChgBillHelper = new HRBaseServiceHelper(PositionBillConstant.NCKD_POSITIONBILL);
            QFilter idFilter = new QFilter("id", "=", iFormView.getModel().getDataEntity().getLong("id"));
            DynamicObject dynamicObject = orgBatchChgBillHelper.loadDynamicObject(idFilter);
            if (dynamicObject != null) {
                String billStatus = dynamicObject.getString(PositionBillConstant.BILL_STATUS_KEY);
                if (!billStatus.equals("A") && !billStatus.equals("G") && !billStatus.equals("D") && (!billStatus.equals("B") || !StringUtils.equals(iFormView.getFormShowParameter().getAppId(), "wftask"))) {
                    String operateLocaleName = changeTypeToLocaleMap.get(operation);
                    String auditstatusName = billStatusToLocaleMap.get(dynamicObject.getString(PositionBillConstant.BILL_STATUS_KEY));
                    iFormView.showErrorNotification(String.format("“%1$s”的单据不能“%2$s”。", new Object[]{auditstatusName, operateLocaleName}));
                    return;
                }
            }

            ListShowParameter listShowParameter = new ListShowParameter();
            if (filter != null) {// 202
                listShowParameter.getListFilterParameter().getQFilters().add(filter);
            }

            BillShowParameter formShowParameter = new BillShowParameter();

            switch (operation) {
                case PositionBillConstant.NEWENTRY_ADD:
                    formShowParameter.setFormId(PositionBillConstant.NCKD_POSBILLENTRYADD);
                    break;
                case PositionBillConstant.NEWENTRY_CHANGE:
                    formShowParameter.setFormId(PositionBillConstant.NCKD_POSBILLENTRYCHANGE);
                    break;
            }

            formShowParameter.setCustomParam("position_bsed", iFormView.getModel().getValue(PositionBillConstant.NCKD_EFFDT) == null ? HRDateTimeUtils.getNowDateTime().getTime() : ((Date)iFormView.getModel().getValue(PositionBillConstant.NCKD_EFFDT)).getTime());
            formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
            formShowParameter.setCustomParam("billid", iFormView.getModel().getDataEntity().getLong(PositionBillConstant.ID_KEY));
            formShowParameter.setCustomParam("billBsed", iFormView.getModel().getDataEntity().getDate(PositionBillConstant.NCKD_EFFDT));
            formShowParameter.setCloseCallBack(new CloseCallBack(pluginName, operation));
            formShowParameter.setCustomParam("org", iFormView.getModel().getDataEntity().getLong("org.id"));
            int sequence = getEntrySeq(iFormView.getModel(), PositionBillConstant.CHANGE_TYPE_ADD);
            formShowParameter.setCustomParam("sequence", sequence);
            iFormView.showForm(formShowParameter);
        }
    }

    public static void entryEntityMore(IDataModel iDataModel, IFormView iFormView, String tag, OperationStatus status, String pluginName) {
        /*EntryGrid entryEntity = iFormView.getControl(ENTRY_ENTITY_PREFIX + tag);
        int row = entryEntity.getEntryState().getFocusRow();
        DynamicObject dynamicObject = iDataModel.getDataEntity(true).getDynamicObjectCollection(ENTRY_ENTITY_PREFIX + tag).get(row);
        Map map = HRDynamicObjectUtils.convertDynamicObjectToMap(dynamicObject);
        long id = dynamicObject.getLong("id");
        BillShowParameter formShowParameter = new BillShowParameter();
        if ("add".equals(tag)) {
            formShowParameter.setFormId(PositionBillConstant.NCKD_POSBILLENTRYADD);
        } else if ("change".equals(tag)) {
            formShowParameter.setFormId(PositionBillConstant.NCKD_POSBILLENTRYCHANGE);
        }

        HRBaseServiceHelper helper = new HRBaseServiceHelper(PositionBillConstant.NCKD_POSITIONBILLENTRY);
        boolean exists = helper.isExists(id);
        if (exists) {
            formShowParameter.setPkId(id);
        }
        formShowParameter.setStatus(status);
        formShowParameter.setCustomParam("id", id);
        formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
        formShowParameter.setCustomParam("selectObject", SerializationUtils.toJsonString(map));
        formShowParameter.setCustomParam("billid", iDataModel.getValue(PositionBillConstant.ID_KEY));
        formShowParameter.setCustomParam("iscard", "isCardFalse");
        formShowParameter.setCustomParam("org_bsed", iFormView.getModel().getValue(PositionBillConstant.NCKD_EFFDT) == null ? HRDateTimeUtils.getNowDateTime().getTime() : ((Date)iFormView.getModel().getValue(PositionBillConstant.NCKD_EFFDT)).getTime());
        formShowParameter.setCustomParam("org", iDataModel.getDataEntity().getLong("org.id"));
        formShowParameter.setCustomParam("billstatus", iDataModel.getDataEntity().get(PositionBillConstant.BILL_STATUS_KEY));
        int sequence = getEntrySeq(iDataModel, PositionBillConstant.CHANGE_TYPE_MAP.get(tag));
        formShowParameter.setCustomParam("sequence", sequence);
        formShowParameter.setCloseCallBack(new CloseCallBack(pluginName, ENTRY_ENTITY_PREFIX + tag));
        iFormView.showForm(formShowParameter);*/

        String entryEntityId = "nckd_entryentity_" + tag;
        EntryGrid strategyEntryGrid = iFormView.getControl(entryEntityId);
        int row = strategyEntryGrid.getEntryState().getFocusRow();
        DynamicObject dynamicObject = iDataModel.getDataEntity(true).getDynamicObjectCollection(entryEntityId).get(row);
        Long id = dynamicObject.getLong(PositionBillConstant.ID_KEY);
        BillShowParameter formShowParameter = new BillShowParameter();
        formShowParameter.setCustomParam("position_bsed", iFormView.getModel().getValue(PositionBillConstant.NCKD_EFFDT) == null ? HRDateTimeUtils.getNowDateTime().getTime() : ((Date)iFormView.getModel().getValue(PositionBillConstant.NCKD_EFFDT)).getTime());
        if ("add".equals(tag)) {
            formShowParameter.setFormId(PositionBillConstant.NCKD_POSBILLENTRYADD);
        } else if ("change".equals(tag)) {
            formShowParameter.setFormId(PositionBillConstant.NCKD_POSBILLENTRYCHANGE);
        }
        formShowParameter.setPkId(id);
        formShowParameter.setStatus(status);
        if (HRStringUtils.equals(iFormView.getFormShowParameter().getAppId(), "wftask")) {
            formShowParameter.setHasRight(true);
        }

        if (!status.equals(OperationStatus.VIEW)) {
            formShowParameter.setCloseCallBack(new CloseCallBack(pluginName, entryEntityId));
        }
        formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
        iFormView.showForm(formShowParameter);
    }


    /**
     * 获取岗位信息维护对象
     * @param addDy
     * @return
     */
    public static DynamicObject getPositionHrDy(DynamicObject addDy) {
        DynamicObject positionHrDy = new DynamicObject(MetadataServiceHelper.getDataEntityType(PositionBillConstant.HBPM_POSITIONHR));
        DynamicObjectUtils.copy(addDy, positionHrDy);
        positionHrDy.set("id", addDy.getLong(PositionBillConstant.ID_KEY));
        positionHrDy.set("parent", addDy.get(PositionBillConstant.NCKD_PARENT));
        positionHrDy.set("adminorg", addDy.get(PositionBillConstant.NCKD_ADMINORG));
        return positionHrDy;
    }

}
