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

import com.yem.wm.es.salesorder.utils.SalesOrderUtils;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

/**
 * @author xwudd
 * @Description
 * @date 14:54
 **/
public class SaleOrderUnSubmitOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_billtype");
        fieldKeys.add("yem_newprice");
        fieldKeys.add("yem_newsalediffprice");
        fieldKeys.add("yem_debcrednoteno");
        fieldKeys.add("yem_customer");
        fieldKeys.add("yem_bd_products");
        fieldKeys.add("yem_cn");
        fieldKeys.add("yem_client");
        fieldKeys.add("yem_productcategorie");
        fieldKeys.add("billstatus");
        fieldKeys.add("yem_im_entryentity_m");
        fieldKeys.add("org");
        fieldKeys.add("yem_fundusetype");
        fieldKeys.add("yem_currency");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject dataEntitie : dataEntities){
            unSetNewpPrice(dataEntitie);
        }

        updatePartsmerchant(e);
    }

    /**
     * 更新配件客商额度申请单
     * @param e
     */
    private void updatePartsmerchant(EndOperationTransactionArgs e)
    {
        String operationKey = e.getOperationKey();
        if ("unsubmit".equals(operationKey)) {
            DynamicObject[] entities = e.getDataEntities();
            for (DynamicObject entity : entities) {
                String fundusetype = entity.getString("yem_fundusetype");
                if (!"A".equals(fundusetype)) {
                    return;
                }
                // 公司
                long orgId = entity.getLong("org.id");
                // 客户ID
                long customerId = entity.getLong("yem_customer.id");
                // 产品分类
                long productsId = entity.getLong("yem_bd_products.id");
                // 结算币别
                long currency = entity.getLong("yem_currency.id");
                // 业务组织 = 公司
                QFilter qFilters = new QFilter("org.id", QCP.equals, orgId);
                // 客户名称 = 客户名称
                qFilters.and(new QFilter("yem_client.id", QCP.equals, customerId));
                // 产品分类 = 产品分类
                qFilters.and(new QFilter("yem_productcategorie", QCP.equals, productsId));
                // 单据状态 = 已审核
                qFilters.and(new QFilter("billstatus", QCP.equals, "C"));
                // 合同上结算币别 = 额度申请上 额度币别
                qFilters.and(new QFilter("yem_currency.id", QCP.equals, currency));
                // 日期降序
//            qFilters.and(new QFilter("yem_bizdate", QCP.equals));
                // 根据组织+客户+产品分类，查询 配件客商额度申请单
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("yem_im_partsmerchant", qFilters.toArray());
                if (dynamicObject != null) {
                    // 配件客商额度申请单 - 分录
                    DynamicObjectCollection collection = dynamicObject.getDynamicObjectCollection("yem_im_entryentity_m");
                    // 删除分录中对应的外销合同
                    Iterator<DynamicObject> iterator = collection.iterator();
                    while (iterator.hasNext()) {
                        DynamicObject item = iterator.next();
                        long yem_cn = item.getLong("yem_cn.id");
                        if (yem_cn != 0) {
                            if (yem_cn == entity.getLong("id")) {
                                iterator.remove();
                            }
                        }
                    }
                    dynamicObject.set("yem_im_entryentity_m", collection);
                    SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                    SalesOrderUtils.calcCreditAmount(dynamicObject.getLong("id"));
                }
            }
        }
    }

    public void unSetNewpPrice(DynamicObject dataEntitie){
        DynamicObject billType = dataEntitie.getDynamicObject("yem_billtype");
        String billTypeNum = YEM.isNotEmpty(billType)?billType.getString("number"):"";
        if("yem_es_salesorder_PJ".equals(billTypeNum) || "yem_es_salesorder_nmpj".equals(billTypeNum)){
            DynamicObjectCollection materialInfos = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");
            for(DynamicObject materialInfo : materialInfos){
                materialInfo.set("yem_newprice",null);
                materialInfo.set("yem_newsalediffprice",null);
            }
            DynamicObjectCollection accFundDetails = dataEntitie.getDynamicObjectCollection("yem_accfunddetail");
            for(DynamicObject accFundDetail : accFundDetails){
                accFundDetail.set("yem_debcrednoteno",null);
            }
            SaveServiceHelper.update(dataEntitie);
        }
    }
}
