package com.yem.wm.es.encasement.op;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.yem.wm.bc.companytitle.form.CompanyTitleEdit;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 张立攀
 * @version 1.0
 * @description 装箱单-审核
 * @date 2023-08-25 9:45
 */
public class EncaseMentEditAudit extends AbstractOperationServicePlugIn
{
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e)
    {
//        DynamicObject[] dataEntities = e.getDataEntities();
//        for (DynamicObject entity : dataEntities) {
//            DynamicObjectCollection collection = entity.getDynamicObjectCollection("yem_backentry");
//
//            Map<Long, BigDecimal> map = new LinkedHashMap<>();
//            for (DynamicObject dynamicObject : collection) {
//                long resourceid = dynamicObject.getLong("yem_resourceid_bac.id");
//                if (!map.containsKey(resourceid)) {
//                    map.put(resourceid, dynamicObject.getBigDecimal("yem_totalweight"));
//                } else {
//                    BigDecimal totalweight = dynamicObject.getBigDecimal("yem_totalweight");
//                    if (map.get(resourceid).compareTo(totalweight) > 0) {
//                        map.put(resourceid, totalweight);
//                    }
//                }
//            }
//
//            for (Map.Entry<Long, BigDecimal> entry : map.entrySet()) {
//                Long k = entry.getKey();
//                BigDecimal v = entry.getValue();
//
//                DynamicObjectCollection backentry = entity.getDynamicObjectCollection("yem_backentry");
//
//                BigDecimal totalweight = BigDecimal.ZERO;
//                for (DynamicObject dynamicObject : backentry) {
//                    long resourceid = dynamicObject.getLong("yem_resourceid_bac.id");
//                    if (k == resourceid) {
//                        BigDecimal weight = dynamicObject.getBigDecimal("yem_weight");
//                        BigDecimal staqty = dynamicObject.getBigDecimal("yem_staqty");
//                        totalweight = totalweight.add(weight.multiply(staqty));
//                    }
//                }
//
//                if (v.compareTo(totalweight) != 0) {
//                    throw new KDBizException("商品明细可拆卸信息中【标准数量】*【重量】需等于【整机重量】");
//                }
//            }
//        }
    }

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e)
    {
        e.getFieldKeys().add("yem_staqty");
        e.getFieldKeys().add("yem_backentry");
        e.getFieldKeys().add("yem_weight");
        e.getFieldKeys().add("yem_totalweight");
        e.getFieldKeys().add("yem_resourceid_bac");
    }
}
