package com.yem.wm.bd.mechanical.op;

import com.yem.wm.utils.DynamicObjectUtil;
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.util.ArrayList;
import java.util.List;

/**
 * @author xwudd
 * @Description 产品机型多功能配置表审核插件
 * @date 10:32
 **/
public class MechanicalAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("yem_checkboxfield");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject mechanical : dataEntities){
            addPriceEntryRow(mechanical);
            mechanical.set("yem_checkboxfield",true);
            SaveServiceHelper.update(mechanical);
        }
    }

    /**
     * @description 审核自动将价格库未 选装、替换、加装行添加到对应价格库中
     * @author xwudd
     * @param[1] mechanical
     * return
     * @time 2023/10/26 14:31
     */
    public void addPriceEntryRow(DynamicObject mechanical){
        mechanical = BusinessDataServiceHelper.loadSingle(mechanical.getPkValue(),mechanical.getDynamicObjectType().getName());
        DynamicObject products = mechanical.getDynamicObject("yem_bd_products");
        if(YEM.isNotEmpty(products)){
            String selectFields = DynamicObjectUtil.getSelectfields("yem_pricelibrary", false);
            selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_pricelibrary","yem_basicmodel",false);
            selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_pricelibrary","yem_optional",false);
            selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_pricelibrary","yem_replaceable",false);
            selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_pricelibrary","yem_retrofitting",false);
            DynamicObject priceLibrary = BusinessDataServiceHelper.loadSingle("yem_pricelibrary", selectFields,
                    new QFilter[]{new QFilter("yem_bd_products", QCP.equals, products.getLong("id"))});
            if(YEM.isNotEmpty(priceLibrary)){
                List<Long> priceList = new ArrayList<>();
                DynamicObjectCollection mechanicalEntrys = mechanical.getDynamicObjectCollection("yem_bd_mechanical_c");
                DynamicObjectCollection priceEntrys = priceLibrary.getDynamicObjectCollection("yem_optional");
                for(DynamicObject priceEntry : priceEntrys){
                    priceList.add(priceEntry.getLong("yem_entryid_o"));
                }
                for(DynamicObject mechanicalEntry : mechanicalEntrys){
                    long id = mechanicalEntry.getLong("id");
                    if(priceList.contains(id)){
                        continue;
                    }
                    DynamicObject addNew = priceEntrys.addNew();
                    addNew.set("yem_profilename_o",mechanicalEntry.get("yem_profilenamec"));//配置名称
                    addNew.set("yem_english",mechanicalEntry.get("yem_configure"));//英文名称
                    addNew.set("yem_specification_o",mechanicalEntry.get("yem_specification_o"));//规格型号
                    addNew.set("yem_parameter_o",mechanicalEntry.get("yem_parameter"));//参数（mm）
                    addNew.set("yem_remark_o",mechanicalEntry.get("yem_remarke"));//备注n
                    addNew.set("yem_entryid_o",id);//
                }
                priceList.clear();
                mechanicalEntrys = mechanical.getDynamicObjectCollection("yem_bd_mechanical_r");
                priceEntrys = priceLibrary.getDynamicObjectCollection("yem_replaceable");
                for(DynamicObject priceEntry : priceEntrys){
                    priceList.add(priceEntry.getLong("yem_entryid_re"));
                }
                for(DynamicObject mechanicalEntry : mechanicalEntrys){
                    long id = mechanicalEntry.getLong("id");
                    if(priceList.contains(id)){
                        continue;
                    }
                    DynamicObject addNew = priceEntrys.addNew();
                    addNew.set("yem_profilename_re",mechanicalEntry.get("yem_profilenamer"));//配置名称
                    addNew.set("yem_englishname",mechanicalEntry.get("yem_configurename"));//英文名称
                    addNew.set("yem_specification_re",mechanicalEntry.get("yem_specification_r"));//规格型号
                    addNew.set("yem_remark_re",mechanicalEntry.get("yem_remarkr"));//备注n
                    addNew.set("yem_entryid_re",id);//
                }
                priceList.clear();
                mechanicalEntrys = mechanical.getDynamicObjectCollection("yem_bd_mechanical_a");
                priceEntrys = priceLibrary.getDynamicObjectCollection("yem_retrofitting");
                for(DynamicObject priceEntry : priceEntrys){
                    priceList.add(priceEntry.getLong("yem_entryid_rt"));
                }
                for(DynamicObject mechanicalEntry : mechanicalEntrys){
                    long id = mechanicalEntry.getLong("id");
                    if(priceList.contains(id)){
                        continue;
                    }
                    DynamicObject addNew = priceEntrys.addNew();
                    addNew.set("yem_profilename_rt",mechanicalEntry.get("yem_profilenamea"));//配置名称
                    addNew.set("yem_englishnames",mechanicalEntry.get("yem_configures"));//英文名称
                    addNew.set("yem_specification_rt",mechanicalEntry.get("yem_specification_a"));//规格型号
                    addNew.set("yem_remark_rt",mechanicalEntry.get("yem_remarka"));//备注n
                    addNew.set("yem_entryid_rt",id);//
                    priceEntrys.add(priceEntrys.size(),addNew);
                }
            }
        }
    }
}
