package com.yem.wm.im.priceLibrary.form;


import com.alibaba.fastjson.JSONObject;
import com.yem.em.utils.BigDecimalUtils;
import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.FunctionalCommon;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.property.AmountProp;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.field.AmountEdit;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description 选装动态表单插件
 */
public class OptionalFormPlugIn extends AbstractFormPlugin implements Plugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IFormView view = this.getView();
        FormShowParameter fsp = view.getFormShowParameter();
        Map<String, Object> customParams = fsp.getCustomParams();
        String type = (String) customParams.get("type");
        if ("A".equals(type)) {
            priceLibraryFunction(customParams);
        }
        if ("B".equals(type)) {
            billFunction(customParams);
        }

    }


    public void priceLibraryFunction(Map<String, Object> customParams) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        view.setVisible(false, "yem_productmodel", "yem_basemodel", "yem_typeconfiguration");
        JSONObject modelObj = (JSONObject) customParams.get("modelObj");
        List<Long> ids = (List<Long>) customParams.get("ids");
        QFilter qFilter = new QFilter("yem_bd_products.id", QCP.equals, modelObj.getLong("id"));
        qFilter.and(new QFilter("status",QCP.equals,"C"));
        qFilter.and(new QFilter("enable",QCP.equals,"1"));
        DynamicObject[] productsgroup = BusinessDataServiceHelper.load("yem_bd_mechanical",
                "id", new QFilter[]{qFilter}, null);
        if (productsgroup.length > 0) {
            DynamicObject load = BusinessDataServiceHelper.loadSingle(productsgroup[0].getLong("id"), "yem_bd_mechanical");
            DynamicObjectCollection mechanical_c = load.getDynamicObjectCollection("yem_bd_mechanical_c");
            for (DynamicObject mechanical : mechanical_c) {
                Long id = mechanical.getLong("id");
                if (ids.contains(id)) {
                    continue;
                }
                String status = mechanical.getString("yem_entrystatus_c");
                if (!"A".equals(status)) {
                    continue;
                }
                int createRow = model.createNewEntryRow("yem_optionalentry");
                model.setValue("yem_profilenamea", mechanical.getString("yem_profilenamec"), createRow);
                model.setValue("yem_configure", mechanical.getString("yem_configure"), createRow);
                model.setValue("yem_specification", mechanical.getString("yem_specification_o"), createRow);
                model.setValue("yem_parameter", mechanical.getString("yem_parameter"), createRow);
                model.setValue("yem_remarke", mechanical.getString("yem_remarke"), createRow);
                model.setValue("yem_entryid", id, createRow);
            }
        }
    }


    public void billFunction(Map<String, Object> customParams) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        List<QFilter> qFilters = new ArrayList<>();
        JSONObject productModel = (JSONObject)customParams.get("productModel");
        Long products = (Long) customParams.get("products");
        List<Long> ids = (List<Long>) customParams.get("ids");
        qFilters.add(new QFilter("yem_bd_products", QCP.equals, products));
        qFilters.add(new QFilter("status", QCP.equals, "C"));
        DynamicObject mechanical = BusinessDataServiceHelper.loadSingle("yem_bd_mechanical", "id", qFilters.toArray(new QFilter[qFilters.size()]));
        if (mechanical != null) {
            mechanical = BusinessDataServiceHelper.loadSingle(mechanical.getPkValue(), mechanical.getDynamicObjectType().getName());
            DynamicObjectCollection optionals = mechanical.getDynamicObjectCollection("yem_bd_mechanical_c");
            for (DynamicObject optional : optionals) {
                Long id = optional.getLong("id");
                if (ids.contains(id)) {
                    continue;
                }
                String status = optional.getString("yem_entrystatus_c");
                if (!"A".equals(status)) {
                    continue;
                }
                int entryRow = model.createNewEntryRow("yem_optionalentry");
                    model.setValue("yem_basemodel", productModel.getLong("id"), entryRow);
                    model.setValue("yem_productmodel", productModel.getJSONObject("yem_products").getLong("id"), entryRow);
                model.setValue("yem_profilenamea", optional.getString("yem_profilenamec"), entryRow);
                model.setValue("yem_configure", optional.getString("yem_configure"), entryRow);
                model.setValue("yem_parameter", optional.getString("yem_parameter"), entryRow);
                model.setValue("yem_unitsetnumber", optional.getBigDecimal("yem_unitsetnumber_c"), entryRow);
                model.setValue("yem_specification", optional.getString("yem_specification_o"), entryRow);
                model.setValue("yem_remarke", optional.getString("yem_remarke"), entryRow);
                model.setValue("yem_entryid", optional.getPkValue(), entryRow);
            }
        }

    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok");
    }

    @Override
    public void click(EventObject e) {
        Control source = (Control) e.getSource();
        String key = source.getKey();
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        FormShowParameter fsp = view.getFormShowParameter();
        Map<String, Object> customParams = fsp.getCustomParams();
        String type = (String) customParams.get("type");
        if ("btnok".equals(key)) {
            if ("A".equals(type)) {
                EntryGrid optionalEntry = view.getControl("yem_optionalentry");
                int[] selectRows = optionalEntry.getSelectRows();
                for (int selectRow : selectRows) {
                    int optionalRow = parentModel.createNewEntryRow("yem_optional");
                    parentModel.setValue("yem_profilename_o", model.getValue("yem_profilenamea", selectRow), optionalRow);
                    parentModel.setValue("yem_english", model.getValue("yem_configure", selectRow), optionalRow);
                    parentModel.setValue("yem_specification_o", model.getValue("yem_specification", selectRow), optionalRow);
                    parentModel.setValue("yem_parameter_o", model.getValue("yem_parameter", selectRow), optionalRow);
                    parentModel.setValue("yem_remark_o", model.getValue("yem_remarke", selectRow), optionalRow);
                    parentModel.setValue("yem_entryid_o", model.getValue("yem_entryid", selectRow), optionalRow);
                }
            }
            if ("B".equals(type)) {
//                Object exrateTable = parentModel.getValue("yem_exratetable");
//                Object sourceCurrency = null;
//                Object targetCurrency = parentModel.getValue("yem_currency");
//                Date exrateDate = (Date) parentModel.getValue("yem_exratedate");
//                String marketPriceCurrName = FunctionalCommon.getAmountFieldCurrencyKey(parentView, "yem_marketprice");
//                sourceCurrency = YEM.isNotEmpty(marketPriceCurrName)?parentModel.getValue(marketPriceCurrName):targetCurrency;
//                BigDecimal marketPriceExchangeRate = YEM.getExchangeRate(exrateTable, sourceCurrency, targetCurrency, exrateDate);
//                String marketRaisePriceCurrName = FunctionalCommon.getAmountFieldCurrencyKey(parentView, "yem_marketprice");
//                sourceCurrency = YEM.isNotEmpty(marketRaisePriceCurrName)?parentModel.getValue(marketRaisePriceCurrName):targetCurrency;
//                BigDecimal marketRaisePriceExchangeRate = YEM.getExchangeRate(exrateTable, sourceCurrency, targetCurrency, exrateDate);
                EntryGrid optionalEntry = view.getControl("yem_optionalentry");
                int[] selectRows = optionalEntry.getSelectRows();
                for (int selectRow : selectRows) {
                     int optionalRow = parentModel.createNewEntryRow("yem_multiconfig");
//                    BigDecimal marketPrice = (BigDecimal) model.getValue("yem_marketprice", selectRow);
//                    BigDecimal marketRaisePrice = (BigDecimal) model.getValue("yem_marketraiseprice", selectRow);
//                    marketPrice = marketPriceExchangeRate.compareTo(BigDecimal.ZERO) > 0 ? marketPrice.multiply(marketPriceExchangeRate) : marketPrice;
//                    marketRaisePrice = marketRaisePriceExchangeRate.compareTo(BigDecimal.ZERO) > 0 ? marketRaisePrice.multiply(marketRaisePriceExchangeRate) : marketRaisePrice;
                    parentModel.setValue("yem_configurtype", "A", optionalRow);
                    parentModel.setValue("yem_configurname", model.getValue("yem_profilenamea", selectRow), optionalRow);
                    parentModel.setValue("yem_configurtypeen", model.getValue("yem_configure", selectRow), optionalRow);
                    parentModel.setValue("yem_specification_o", model.getValue("yem_specification", selectRow), optionalRow);
//                    parentModel.setValue("yem_marketprice", marketPrice, optionalRow);
//                    parentModel.setValue("yem_marketraiseprice", marketRaisePrice, optionalRow);
                    parentModel.setValue("yem_unitsetnumber", model.getValue("yem_unitsetnumber", selectRow), optionalRow);
                    parentModel.setValue("yem_arguments", model.getValue("yem_parameter", selectRow), optionalRow);
                    parentModel.setValue("yem_remarkss", model.getValue("yem_remarke", selectRow), optionalRow);
                    parentModel.setValue("yem_configentryid", model.getValue("yem_entryid", selectRow), optionalRow);
                }
            }
        }
        view.sendFormAction(parentView);
        view.close();
    }
}