package com.yem.wm.im.delivernotice.botp;

import com.yem.wm.im.delivernotice.utils.AmountCalculation;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.botp.ConvertRuleElement;
import kd.bos.entity.botp.plugin.AbstractConvertPlugIn;
import kd.bos.entity.botp.plugin.args.AfterConvertEventArgs;
import kd.bos.exception.KDBizException;
import kd.bos.metadata.botp.ConvertRuleMetadata;
import kd.bos.metadata.botp.ConvertRuleReader;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.ConvertServiceHelper;

import java.util.List;

/**
 * @author zhouc
 * @date 2024/4/15 17:06
 * @className ShippingdetailsToDelivernoticeDJD
 * @description 出运明细下推其他出库单（贷记单）
 */
public class ShippingdetailsToDelivernoticeDJD extends AbstractConvertPlugIn {
    @Override
    public void afterConvert(AfterConvertEventArgs e) {
        String tgtName = this.getTgtMainType().getName();
        String name = this.getSrcMainType().getName();
        ExtendedDataEntity[] findByEntityKeys = e.getTargetExtDataEntitySet().FindByEntityKey(tgtName);
        for (ExtendedDataEntity findByEntityKey : findByEntityKeys) {
            DynamicObject dataEntity = findByEntityKey.getDataEntity();
            //核心单据id
            long yem_corebillid = dataEntity.getLong("yem_corebillid");
            QFilter qFilter = new QFilter("id", QCP.equals, yem_corebillid);
            qFilter.and(new QFilter("yem_billtype.number", QCP.equals, "yem_es_salesorder_PJ")
                    .or(new QFilter("yem_billtype.number", QCP.equals, "yem_es_salesorder_nmpj")));
            qFilter.and(new QFilter("yem_billbusstype.number", QCP.not_equals, "BBT-2"));
            qFilter.and(new QFilter("yem_fundusetype", QCP.equals, "B"));
            qFilter.and(new QFilter("yem_accfunddetail.yem_debcredtype.number", QCP.equals, "JDJLX-0003"));
            DynamicObject billType = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", "id", qFilter.toArray());
            if (billType == null) {

                String msg = "当前单据对应的合同【" + dataEntity.getString("yem_corebillno") + "】不满足使用单据转换【"+getBOTPName("1928929957191371776")+"】，" +
                        "请手动选择【"+getBOTPName("1797887963242168320")+"】后重试！";
                throw new KDBizException(msg);
            }
            AmountCalculation.Amount(dataEntity);
        }
    }

    /**
     * 根据BOTPid 获取转换规则名称
     *
     * @param id
     * @return
     */
    public String getBOTPName(String id) {
        String Name = "";
        ConvertRuleReader read = new ConvertRuleReader();
        List<ConvertRuleElement> convertRuleElements = read.loadRules("yem_shippingdetails", "yem_es_delivernotice");
        if (convertRuleElements.size() > 0) {
            for (int i = 0; i < convertRuleElements.size(); i++) {
                ConvertRuleElement convertRuleElement = convertRuleElements.get(i);
                String botpid = convertRuleElement.getId();
                if (botpid.equals(id)) {
                    Name = String.valueOf(convertRuleElement.getName());
                }
            }
        }
        return Name;
    }
}
