package nckd.jimin.jyyy.fi.plugin.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.BillShowParameter;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.ext.fi.plugin.ArApConvert.util.EmptyUtils;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.util.StringUtils;
import nckd.base.helper.CommonHelperUtils;

import java.util.Arrays;
import java.util.EventObject;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 单据标识：还款单(nckd_er_repaymentbill_ext)
 * @author wanghaiwu_kd
 * @date 2025/06/05
 */
public class ErRepaymentBillCusEditPlugin extends AbstractBillPlugIn {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);

        Map<String, Object> customParams = this.getView().getFormShowParameter().getCustomParams();
        BillShowParameter parameter = new BillShowParameter();
        String formId = "";
        if(EmptyUtils.isNotEmpty(customParams.get("formId"))){
            formId =customParams.get("formId").toString();
            parameter.setFormId(formId);
            if(StringUtils.isNotEmpty(formId)) {
                this.getModel().setValue("nckd_pageid", formId);

                //【单据类型】字段，根据页面类型自动默认赋值
                Boolean haveBillType = this.getModel().getDataEntityType().getProperties().containsKey("nckd_billtypefield");
                if(haveBillType) {
                    //处理单据类型
                    QFilter f = new QFilter("number", "=", formId);
                    DynamicObject layoutInfo = BusinessDataServiceHelper.loadSingle("bos_formmeta", "id, number,name, basedatafield", f.toArray());

                    if (layoutInfo != null) {
                        DynamicObject billTypeInfo = CommonHelperUtils.queryBaseDynamicObject("bos_billtype", "layoutsolution", layoutInfo.getString("id"));
                        this.getModel().setValue("nckd_billtypefield", billTypeInfo);
                    }
                }
            }

            //如果是预付退款，还款类型设置为预付退款
            if("nckd_refundbill".equals(formId)){
                this.getModel().setValue("reimbursetype", "prepayrefund");
            }
        }

        if(customParams.get("viewType") != null){
            String[] typeStrings = customParams.get("viewType").toString().split(",");
            QFilter qFilter = new QFilter("number", QCP.in, typeStrings);
            DynamicObject[] viewTypeCols = BusinessDataServiceHelper.load("nckd_payviewtype", "id, number, name", qFilter.toArray());
            if(viewTypeCols != null && viewTypeCols.length > 0){
                //页面类型赋值
                this.getModel().setValue("nckd_payviewtype", viewTypeCols[0]);

                List<Long> idList = Arrays.stream(viewTypeCols)
                        .map(type ->  type.getLong("id"))
                        .collect(Collectors.toList());
                Object[] basedataIds = idList.toArray();
                //页面类型多选赋值
                this.getModel().setValue("nckd_payviewtypemul", basedataIds);
            }
        }
    }
}
