package com.yem.wm.im.inquirylist.op;

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.AfterOperationArgs;
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.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class InquirylistAudit extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_billtype");
        fieldKeys.add("yem_multiconfig");
        fieldKeys.add("yem_productstree");
        fieldKeys.add("yem_configurtype");
        fieldKeys.add("yem_configentryid");
        fieldKeys.add("yem_marketprice");
        fieldKeys.add("yem_marketraiseprice");
        fieldKeys.add("yem_im_detailedinfor");
        fieldKeys.add("yem_configurname");
        fieldKeys.add("yem_configurtypeen");
        fieldKeys.add("yem_specification_o");
        fieldKeys.add("yem_arguments");
        fieldKeys.add("yem_remarkss");

    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        String operationKey = e.getOperationKey();
        if (!"audit".equals(operationKey)){
            return;
        }
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie: dataEntities) {
            Boolean billtype = billtype(dataEntitie);
            if (!billtype) continue;
            DynamicObjectCollection imDetailedinforColl = dataEntitie.getDynamicObjectCollection("yem_im_detailedinfor");
            //多功能配置
            for(DynamicObject imDetailedinfor : imDetailedinforColl){
                DynamicObjectCollection yemMulticonfigColl = imDetailedinfor.getDynamicObjectCollection("yem_multiconfig");
                optional(dataEntitie,yemMulticonfigColl);
                replace(dataEntitie,yemMulticonfigColl);
                retrofitting(dataEntitie,yemMulticonfigColl);
            }

        }



    }

    public Boolean billtype(DynamicObject dataEntitie){
        DynamicObject yemBilltype = dataEntitie.getDynamicObject("yem_billtype");
        String number = yemBilltype.getString("number");
        if ("yem_im_inquirylist_ZJ".equals(number)){
            return true;
        }
        return false;
    }


    /**
     * 可加装
     * @param dataEntitie
     * @param yemMulticonfigColl
     */
    public void optional(DynamicObject dataEntitie,DynamicObjectCollection yemMulticonfigColl){
        //获取配置类型为可选装配置
        Predicate<DynamicObject> predicate = dynamicObject -> dynamicObject.getString("yem_configurtype") .equals("A");
        List<DynamicObject> collect = yemMulticonfigColl.stream().filter(predicate).collect(Collectors.toList());
        for (DynamicObject yemMulticonfig: collect) {
            //产品机型
            DynamicObject bdProducts = yemMulticonfig.getDynamicObject("yem_productstree");
            String bdProductNumber = bdProducts.getString("number");
            //配置分录内码
            Long configentryid = yemMulticonfig.getLong("yem_configentryid");
            //标准销售定价
            BigDecimal marketprice = yemMulticonfig.getBigDecimal("yem_marketprice");
            //标准销售加价
            BigDecimal marketraiseprice = yemMulticonfig.getBigDecimal("yem_marketraiseprice");
            //配置名称
            String configurname = yemMulticonfig.getString("yem_configurname");
            //配置英文名称
            String configurtypeen = yemMulticonfig.getString("yem_configurtypeen");
            //规格型号
            String specification_o = yemMulticonfig.getString("yem_specification_o");
            //参数（mm）
            String arguments = yemMulticonfig.getString("yem_arguments");
            //备注
            String remarkss = yemMulticonfig.getString("yem_remarkss");



            //价格库
            DynamicObject pricelibrary = BusinessDataServiceHelper.loadSingle("yem_pricelibrary", new QFilter[]{new QFilter("yem_bd_products.number", QCP.equals, bdProductNumber), new QFilter("billstatus", "=", "C")});
            if (pricelibrary==null){
                continue;
            }
            DynamicObjectCollection optionalColl = pricelibrary.getDynamicObjectCollection("yem_optional");
            Predicate<DynamicObject> filter = dynamicObject -> dynamicObject.getLong("yem_entryid_o")== configentryid;
            List<DynamicObject> collects = optionalColl.stream().filter(filter).collect(Collectors.toList());
            if (collects.size()>0){
                //更新
                DynamicObject dynamicObject = collects.get(0);
                dynamicObject.set("yem_salesprice_o",marketprice);
                dynamicObject.set("yem_salesmarkup_o",marketraiseprice);
                SaveServiceHelper.update(pricelibrary);
            }else {
                DynamicObject dynamicObject = new DynamicObject(optionalColl.getDynamicObjectType());
                dynamicObject.set("yem_profilename_o",configurname);
                dynamicObject.set("yem_english",configurtypeen);
                dynamicObject.set("yem_specification_o",specification_o);
                dynamicObject.set("yem_salesprice_o",marketprice);
                dynamicObject.set("yem_salesmarkup_o",marketraiseprice);
                dynamicObject.set("yem_parameter_o",arguments);
                dynamicObject.set("yem_remark_o",remarkss);
                dynamicObject.set("yem_entryid_o",configentryid);
                optionalColl.add(dynamicObject);
                SaveServiceHelper.save(new DynamicObject[]{pricelibrary});
            }
        }
    }



    /**
     * 可替换
     * @param dataEntitie
     * @param yemMulticonfigColl
     */
    public void replace(DynamicObject dataEntitie,DynamicObjectCollection yemMulticonfigColl){
        //获取配置类型为可选装配置
        Predicate<DynamicObject> predicate = dynamicObject -> dynamicObject.getString("yem_configurtype") .equals("B");
        List<DynamicObject> collect = yemMulticonfigColl.stream().filter(predicate).collect(Collectors.toList());
        for (DynamicObject yemMulticonfig: collect) {
            //产品机型
            DynamicObject bdProducts = yemMulticonfig.getDynamicObject("yem_productstree");
            String bdProductNumber = bdProducts.getString("number");
            //配置分录内码
            Long configentryid = yemMulticonfig.getLong("yem_configentryid");
            //标准销售定价
            BigDecimal marketprice = yemMulticonfig.getBigDecimal("yem_marketprice");
            //标准销售加价
            BigDecimal marketraiseprice = yemMulticonfig.getBigDecimal("yem_marketraiseprice");
            //配置名称
            String configurname = yemMulticonfig.getString("yem_configurname");
            //配置英文名称
            String configurtypeen = yemMulticonfig.getString("yem_configurtypeen");
            //规格型号
            String specification_o = yemMulticonfig.getString("yem_specification_o");
            //参数（mm）
            String arguments = yemMulticonfig.getString("yem_arguments");
            //备注
            String remarkss = yemMulticonfig.getString("yem_remarkss");



            //价格库
            DynamicObject pricelibrary = BusinessDataServiceHelper.loadSingle("yem_pricelibrary", new QFilter[]{new QFilter("yem_bd_products.number", QCP.equals, bdProductNumber), new QFilter("billstatus", "=", "C")});
            if (pricelibrary==null){
                continue;
            }
            DynamicObjectCollection optionalColl = pricelibrary.getDynamicObjectCollection("yem_replaceable");
            Predicate<DynamicObject> filter = dynamicObject -> dynamicObject.getLong("yem_entryid_re")== configentryid;
            List<DynamicObject> collects = optionalColl.stream().filter(filter).collect(Collectors.toList());
            if (collects.size()>0){
                //更新
                DynamicObject dynamicObject = collects.get(0);
                dynamicObject.set("yem_salesprice_re",marketprice);
                dynamicObject.set("yem_salesmarkup_re",marketraiseprice);
                SaveServiceHelper.update(pricelibrary);
            }else {
                DynamicObject dynamicObject = new DynamicObject(optionalColl.getDynamicObjectType());
                dynamicObject.set("yem_profilename_re",configurname);
                dynamicObject.set("yem_englishname",configurtypeen);
                dynamicObject.set("yem_specification_re",specification_o);
                dynamicObject.set("yem_salesprice_re",marketprice);
                dynamicObject.set("yem_salesmarkup_re",marketraiseprice);
                dynamicObject.set("yem_remark_re",remarkss);
                dynamicObject.set("yem_entryid_re",configentryid);
                optionalColl.add(dynamicObject);
                SaveServiceHelper.save(new DynamicObject[]{pricelibrary});
            }
        }
    }

    /**
     * 可加装
     * @param dataEntitie
     * @param yemMulticonfigColl
     */
    public void retrofitting(DynamicObject dataEntitie,DynamicObjectCollection yemMulticonfigColl){
        //获取配置类型为可选装配置
        Predicate<DynamicObject> predicate = dynamicObject -> dynamicObject.getString("yem_configurtype") .equals("C");
        List<DynamicObject> collect = yemMulticonfigColl.stream().filter(predicate).collect(Collectors.toList());
        for (DynamicObject yemMulticonfig: collect) {
            //产品机型
            DynamicObject bdProducts = yemMulticonfig.getDynamicObject("yem_productstree");
            String bdProductNumber = bdProducts.getString("number");
            //配置分录内码
            Long configentryid = yemMulticonfig.getLong("yem_configentryid");
            //标准销售定价
            BigDecimal marketprice = yemMulticonfig.getBigDecimal("yem_marketprice");
            //标准销售加价
            BigDecimal marketraiseprice = yemMulticonfig.getBigDecimal("yem_marketraiseprice");
            //配置名称
            String configurname = yemMulticonfig.getString("yem_configurname");
            //配置英文名称
            String configurtypeen = yemMulticonfig.getString("yem_configurtypeen");
            //规格型号
            String specification_o = yemMulticonfig.getString("yem_specification_o");
            //参数（mm）
            String arguments = yemMulticonfig.getString("yem_arguments");
            //备注
            String remarkss = yemMulticonfig.getString("yem_remarkss");



            //价格库
            DynamicObject pricelibrary = BusinessDataServiceHelper.loadSingle("yem_pricelibrary", new QFilter[]{new QFilter("yem_bd_products.number", QCP.equals, bdProductNumber), new QFilter("billstatus", "=", "C")});
            if (pricelibrary==null){
                continue;
            }
            DynamicObjectCollection optionalColl = pricelibrary.getDynamicObjectCollection("yem_retrofitting");
            Predicate<DynamicObject> filter = dynamicObject -> dynamicObject.getLong("yem_entryid_rt")== configentryid;
            List<DynamicObject> collects = optionalColl.stream().filter(filter).collect(Collectors.toList());
            if (collects.size()>0){
                //更新
                DynamicObject dynamicObject = collects.get(0);
                dynamicObject.set("yem_salesprice_rt",marketprice);
                dynamicObject.set("yem_salesmarkup_rt",marketraiseprice);
                SaveServiceHelper.update(pricelibrary);
            }else {
                DynamicObject dynamicObject = new DynamicObject(optionalColl.getDynamicObjectType());
                dynamicObject.set("yem_profilename_rt",configurname);
                dynamicObject.set("yem_englishnames",configurtypeen);
                dynamicObject.set("yem_specification_rt",specification_o);
                dynamicObject.set("yem_salesprice_rt",marketprice);
                dynamicObject.set("yem_salesmarkup_rt",marketraiseprice);
                dynamicObject.set("yem_remark_rt",remarkss);
                dynamicObject.set("yem_entryid_rt",configentryid);
                optionalColl.add(dynamicObject);
                SaveServiceHelper.save(new DynamicObject[]{pricelibrary});
            }
        }
    }

}
