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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.*;
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.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import nckd.jxccl.swc.constants.SwcConstant;

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

/**
 * Tyx 2025-11-25
 * 单位代发单列表插件
 */
public class AgencyBillList extends AbstractListPlugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate formOperate = (FormOperate)args.getSource();
        String opKey = formOperate.getOperateKey();
        if (StringUtils.equals(opKey, "donothing_batchupdate")) {
            this.openBatchUpdateWindow(args);
        }
        else if (StringUtils.equals(opKey, "donothing_selectorg")) {
            this.doSelectOrg(args);
        }
    }

    public void doSelectOrg (BeforeDoOperationEventArgs args) {
        //创建弹出列表界面对象，ListShowParameter 表示弹出页面为列表界面
        ListShowParameter listShowParameter = new ListShowParameter();
        //设置F7列表表单模板
        listShowParameter.setFormId("hbp_hislistf7");
        //设置BillFormId为基础资料的标识
        listShowParameter.setBillFormId("hsas_payrollgrp");
        //设置弹出页面标题
        listShowParameter.setCaption("薪资核算组选择界面");
        //设置弹出页面的打开方式
        listShowParameter.getOpenStyle().setShowType(ShowType.Modal);
        //设置打开页面的大小
        StyleCss inlineStyleCss = new StyleCss();
        inlineStyleCss.setHeight("580");
        inlineStyleCss.setWidth("960");
        listShowParameter.getOpenStyle().setInlineStyleCss(inlineStyleCss);
        //LookUp必须为true，该界面才是可选界面
        listShowParameter.setLookUp(true);
        //是否只显示审核的数据
        listShowParameter.setShowApproved(true);
        //是否只显示只启用的数据
        listShowParameter.setShowUsed(true);
        //设置子页面关闭回调参数，回调标识为XXX setControlKey用来设置f7选择的数据返回给哪个字段
        CloseCallBack closeCallBack = new CloseCallBack(this, "selectorg");
        listShowParameter.setCloseCallBack(closeCallBack);
        //弹出F7选择界面
        this.getView().showForm(listShowParameter);
    }


    /**
     *
     * @param args
     */
    private void openBatchUpdateWindow(BeforeDoOperationEventArgs args) {
        FormShowParameter formShowParameter = new FormShowParameter();
        formShowParameter.setFormId("nckd_agencybatchupdate");
        formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
        formShowParameter.setCloseCallBack(new CloseCallBack(this, "batchUpdate"));
        this.getView().showForm(formShowParameter);
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        super.closedCallBack(closedCallBackEvent);
        IFormView view = this.getView();
        IListView listview = (IListView) this.getView();
        String actionId = closedCallBackEvent.getActionId();
        if ("batchUpdate".equals(actionId)) {
            Map map = (Map) closedCallBackEvent.getReturnData();
            if (map != null) {

                Date month = (Date) map.get("month");
                DynamicObject payRollScene = (DynamicObject) map.get("payRollScene");
                ListSelectedRowCollection selectedRows = listview.getSelectedRows();
                Object[] entryIds = selectedRows.getEntryPrimaryKeyValues();

                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.AGENCYPAYBILL_ENTITY.load("id, nckd_entryentity.nckd_periodmonth, nckd_entryentity.nckd_payrollscene", new QFilter[]{filter});
                // 批量更新
                for(DynamicObject bill : billDyns) {
                    DynamicObjectCollection entryDyns = bill.getDynamicObjectCollection("nckd_entryentity");
                    for(DynamicObject entry : entryDyns) {
                        if(entryIdsList.contains(entry.getLong("id"))) {
                            entry.set("nckd_periodmonth", month);
                            entry.set("nckd_payrollscene", payRollScene);
                        }
                    }
                }
                SwcConstant.AGENCYPAYBILL_ENTITY.update(billDyns);
            }
            view.invokeOperation("refresh");
        }
        else if ("selectorg".equals(actionId)) {
            ListSelectedRowCollection listSelectedRowCollection = (ListSelectedRowCollection) closedCallBackEvent.getReturnData();
            if(listSelectedRowCollection != null) {
                if (listSelectedRowCollection.size() > 0) {
                    Object payRollGrpId = listSelectedRowCollection.get(0).getPrimaryKeyValue();
                    // 当前单据界面选择的数据
                    ListSelectedRowCollection selectedRows = listview.getSelectedRows();
                    Object[] entryIds = selectedRows.getEntryPrimaryKeyValues();

                    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.AGENCYPAYBILL_ENTITY.load("id, nckd_entryentity.nckd_payrollgrp", new QFilter[]{filter});
                    // 批量更新
                    for (DynamicObject bill : billDyns) {
                        DynamicObjectCollection entryDyns = bill.getDynamicObjectCollection("nckd_entryentity");
                        for (DynamicObject entry : entryDyns) {
                            if (entryIdsList.contains(entry.getLong("id"))) {
                                entry.set("nckd_payrollgrp", payRollGrpId);
                            }
                        }
                    }
                    SwcConstant.AGENCYPAYBILL_ENTITY.update(billDyns);
                }
            }
            view.invokeOperation("refresh");
        }
    }




}
