package nckd.jimin.jyyy.hr.swc.hcdm.formplugin.report;

import com.google.common.collect.Sets;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.entity.property.DecimalProp;
import kd.bos.entity.report.ReportColumn;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.sdk.plugin.Plugin;
import kd.swc.hcdm.formplugin.report.AdjRptFormPlugin;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Iterator;

/**
 * Tyx 2025-09-08
 * 定调薪明细表 处理年薪总包字段
 */
public class AdjDetailFormRptEx extends AbstractReportFormPlugin implements Plugin {

    private static final Log log = LogFactory.getLog(AdjDetailFormRptEx.class);


    @Override
    public void processRowData(String gridPK, DynamicObjectCollection rowData, ReportQueryParam queryParam) {
        super.processRowData(gridPK, rowData, queryParam);
        Iterator<DynamicObject> iterator = rowData.iterator();
        while (iterator.hasNext()) {
            DynamicObject row = iterator.next();
            String itemName = row.getString("standarditem.name");
            if(!itemName.equals("年度薪酬标准"))
                continue;
            //金额
            BigDecimal amount = row.getBigDecimal("amount");
            //N薪系数
            BigDecimal nxxs = row.getBigDecimal("hcdm_adjfileinfo.nckd_ncoefficient");
            //年薪总包 = 年度薪酬标准 + 年度薪酬标准 / 12 * N薪系数
            BigDecimal a = amount.divide(new BigDecimal("12"),2, RoundingMode.HALF_UP).multiply(nxxs);
            BigDecimal b = amount.add(a);
            row.set("hcdm_adjfileinfo.nckd_nxzb", b);
        }
    }

    @Override
    public void packageData(PackageDataEvent packageDataEvent) {
        super.packageData(packageDataEvent);
        if (packageDataEvent.getSource() instanceof ReportColumn) {
            ReportColumn reportColumn = (ReportColumn)packageDataEvent.getSource();
            String fieldKey = reportColumn.getFieldKey();
            if(Sets.newHashSet(new String[]{"hcdm_adjfileinfo.nckd_nxzb"}).contains(fieldKey) && reportColumn.getFieldProperty() instanceof DecimalProp) {
                StringBuilder format = new StringBuilder("￥#0,000.00");
                DecimalFormat myFormat = new DecimalFormat(format.toString());
                Object field = packageDataEvent.getRowData().get(fieldKey);
                if (field != null) {
                    String value = myFormat.format(field);
                    packageDataEvent.setFormatValue(value);
                } else {
                    packageDataEvent.setFormatValue((Object)null);
                }
            }
        }
    }
}