package nckd.jxccl.swc.hcdm.formplugin.annualincome;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.IListView;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.swc.constants.SwcConstant;

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

/**
 * Tyx 2025-11-25
 * 年收入证明统计单列表插件 【nckd_salinnualincomebill】
 * 该类是一个年收入证明统计单列表的插件实现，继承自AbstractListPlugin并实现Plugin接口。
 * 主要功能包括批量新增、废弃操作等。
 */
public class SalAnnualIncomeBillList extends AbstractListPlugin implements Plugin {

    /**
     * 操作前事件处理方法
     * @param args 操作事件参数
     */
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate formOperate = (FormOperate)args.getSource();
        String opKey = formOperate.getOperateKey();
        switch(opKey) {
            case "donothing_batchaddnew":
                this.openBatchAddnewWindow(args);
            break;
            case "donothing_update":
                this.beforeUpdateVerify(args);
            break;
        }
    }

    /**
     * 更新数据前校验是否都为社保未拉取，如果没有可更新的数据则提示
     * @param args
     */
    public void beforeUpdateVerify(BeforeDoOperationEventArgs args) {

    }

    /**
     * 打开批量新增窗口
     * @param args 操作事件参数
     */
    private void openBatchAddnewWindow(BeforeDoOperationEventArgs args) {
        FormShowParameter formShowParameter = new FormShowParameter();
        formShowParameter.setFormId("nckd_incomebatchaddnew");
        formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
        formShowParameter.setCloseCallBack(new CloseCallBack(this, "batchAddnew"));
        this.getView().showForm(formShowParameter);
    }

    /**
     * 窗口关闭回调方法
     * @param closedCallBackEvent 关闭回调事件参数
     */
    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        super.closedCallBack(closedCallBackEvent);
        String actionId = closedCallBackEvent.getActionId();
        switch(actionId) {
            case "batchAddnew":
                doBatchAddnewClosedCallBackEvent(closedCallBackEvent);
        }
    }

    /**
     * 处理批量新增窗口关闭后的回调事件
     * @param closedCallBackEvent 关闭回调事件参数
     */
    private void doBatchAddnewClosedCallBackEvent(ClosedCallBackEvent closedCallBackEvent) {
        // 如果有返回数据，则显示成功消息并刷新列表
        if(!ObjectUtils.isEmpty(closedCallBackEvent.getReturnData())) {
            this.getView().showSuccessNotification(closedCallBackEvent.getReturnData().toString());
            this.getView().invokeOperation("refresh");
        }
    }

    /**
     * 操作后事件处理方法
     * @param afterDoOperationEventArgs 操作后事件参数
     */
    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
        String key = afterDoOperationEventArgs.getOperateKey();
        switch (key) {
            case "donothing_discard":
                doDiscardByRow();
                break;
            case "donothing_update":
                if(afterDoOperationEventArgs.getOperationResult().isSuccess()) {
                    this.getView().showSuccessNotification("更新成功！");
                    this.getView().invokeOperation("refresh");
                }
        }
    }

    /**
     * 按行废弃方法
     * 将选中的行状态设置为废弃状态，并刷新列表
     */
    private void doDiscardByRow() {
        IFormView view = this.getView();
        IListView listview = (IListView) this.getView();
        ListSelectedRowCollection selectedRows = listview.getSelectedRows();
        Object[] entryIds = selectedRows.getEntryPrimaryKeyValues();

        // 将选中的行ID转换为Long类型的List
        List<Long> entryIdsList = Arrays.stream(entryIds).map(obj -> {
            return (Long) obj;
        }).collect(Collectors.toList());

        // 创建查询过滤器，获取选中的行数据
        QFilter filter = new QFilter("nckd_entryentity.id", "in", entryIdsList);
        DynamicObject[] billDyns = SwcConstant.SALANNUALINCOMEBILL_ENTITY.load("nckd_entryentity.nckd_pushstatus", filter.toArray());
        // 遍历数据，将选中的行状态设置为废弃（3）
        for(DynamicObject bill : billDyns) {
            DynamicObjectCollection entryDyns = bill.getDynamicObjectCollection("nckd_entryentity");
            for(DynamicObject entry : entryDyns) {
                if(entryIdsList.contains(entry.getLong("id"))) {
                    entry.set("nckd_pushstatus", "3");
                }
            }
        }
        // 更新数据并刷新列表
        SwcConstant.AGENCYPAYBILL_ENTITY.update(billDyns);
        view.showSuccessNotification("废弃成功！");
        view.invokeOperation("refresh");
    }
}