package shkd.repc.recon.opplugin;

import com.alibaba.fastjson.JSONObject;
import kd.bos.entity.api.ApiResult;
import kd.bos.entity.plugin.ImportLogger;
import kd.bos.form.plugin.impt.BatchImportPlugin;
import kd.bos.form.plugin.impt.ImportBillData;
import kd.bos.util.StringUtils;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

/**
 * 合同清单导入功能(校验)
 */
public class IntroduceContractPlugin extends BatchImportPlugin {
    @Override
    protected ApiResult save(List<ImportBillData> rowdatas, ImportLogger logger) {
        Iterator<ImportBillData> iterator = rowdatas.iterator();
        while (iterator.hasNext()){
            ImportBillData importBillData = iterator.next();
            JSONObject data = importBillData.getData();
            String qeug_decimalqty = (String) data.get("qeug_decimalqty");//工程量
            String qeug_preofpro = (String) data.get("qeug_preofpro");//当前进度百分比
            String qeug_cumulativepreofpro = (String) data.get("qeug_cumulativepreofpro");//累计进度百分比
            BigDecimal decimalqty =null;
            BigDecimal preofpro =null;
            if (qeug_decimalqty != null && qeug_preofpro!= null) {
                //校验 工程量&进度百分比 数据格式是否正确
                try {
                    decimalqty = new BigDecimal(qeug_decimalqty);
                    preofpro = new BigDecimal(qeug_preofpro);
                } catch (NumberFormatException e) {
                    logger.log(importBillData.getStartIndex(),"工程量数据格式有误").fail();
                    iterator.remove();
                }
                // 本次完工量 = 工程量 * (当前进度百分比-累计完成百分比)
                BigDecimal bd1 = new BigDecimal(qeug_decimalqty);
                BigDecimal bd2 = new BigDecimal(qeug_preofpro);
                BigDecimal bd3;
                if (StringUtils.isEmpty(qeug_cumulativepreofpro)) {
                    bd3 = BigDecimal.ZERO; // 如果 qeug_cumulativepreofpro 为空,则赋值为 0
                } else {
                    bd3 = new BigDecimal(qeug_cumulativepreofpro);
                }
                BigDecimal bd4 = bd2.subtract(bd3);
                data.put("qeug_thisprogress",bd4);//本次进度百分比(%)
                BigDecimal result = bd1.multiply(bd4).multiply(BigDecimal.valueOf(0.01));
                data.put("qeug_bcdecimalqtys",result);//本次完工量
                if (StringUtils.isNotEmpty(qeug_preofpro)) {
                    preofpro = new BigDecimal(qeug_preofpro);
                }
                if (preofpro != null && preofpro.compareTo(BigDecimal.valueOf(100)) > 0) {

                    logger.log(importBillData.getStartIndex(), "进度百分比不能大于100").fail();
                    iterator.remove();
                }
                if (bd3.compareTo(bd2) > 0) {
                    logger.log(importBillData.getStartIndex(), "当前进度百分比不能小于累计完成百分比").fail();
                    iterator.remove();
                }
                data.put("qeug_cumulativepreofpro",bd2);
            }

        }
        return super.save(rowdatas, logger);
    }
}