package nckd.jxccl.sit.hcsi.formplugin.web.annualincome;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.sit.hcsi.business.annualincome.AnnualIncomeCalculateService;

import java.util.Date;
import java.util.EventObject;

/**
 * Tyx 2025-11-20
 * 社保年收入统计列表点击计算打开的界面
 * 【nckd_incomecalselectform】
 */
public class IncomeCalSelectFormPlugin extends AbstractFormPlugin implements Plugin {


    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
        String key = afterDoOperationEventArgs.getOperateKey();
        switch(key) {
            case "donothing_calculate":
                doCalculate();
        }
    }

    /**
     * 执行计算
     */
    private void doCalculate() {
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        DynamicObject dyn = model.getDataEntity();
        DynamicObject welfarePayer = dyn.getDynamicObject("nckd_welfarepayer");
        Date year = dyn.getDate("nckd_year");
        AnnualIncomeCalculateService service = new AnnualIncomeCalculateService(welfarePayer, year, model, view);
        int successCount = service.calculate();
        view.returnDataToParent(successCount);
        view.close();
    }
}