package nckd.jxccl.hr.psms.plugin.form.adjust;

import kd.bos.common.enums.EnableEnum;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* 已生成动态调整-查询列表插件
* @author W.Y.C
* @date 2025/10/12 17:51
* @version 1.0
*/
public class DynamicAdjustQueryListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void setFilter(SetFilterEvent e) {
        e.addCustomQFilter(new QFilter(PositionStructureConstant.NCKD_DISABLE, QCP.equals, EnableEnum.NO.getCode()));
        //只查询动态调整的数据
        e.addCustomQFilter(new QFilter(PositionStructureConstant.NCKD_TYPESTATE, QCP.equals, "4"));
    }

    @Override
    public void packageData(PackageDataEvent e) {
        String colKey = e.getColKey();
        if("nckd_postallowancediff".equalsIgnoreCase(colKey)){
            BigDecimal itemAmtEx = e.getRowData().getBigDecimal("nckd_postallowance");
            BigDecimal itemAmt = e.getRowData().getBigDecimal("nckd_lastpersonposfile.nckd_postallowance");

            // 空值处理
            if (itemAmtEx == null) {
                itemAmtEx = BigDecimal.ZERO;
            }
            if (itemAmt == null) {
                itemAmt = BigDecimal.ZERO;
            }

            e.setFormatValue("¥"+itemAmtEx.subtract(itemAmt).setScale(2, RoundingMode.HALF_UP).toString());
        }
    }
}