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.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 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);
        }
    }

    /**
     *
     * @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 calRule = (DynamicObject) map.get("calrule");
                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_calrule", 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_calrule", calRule);
                        }
                    }
                }
                SwcConstant.AGENCYPAYBILL_ENTITY.update(billDyns);
            }
            view.invokeOperation("refresh");
        }
    }




}
