package com.yem.or.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.grapecity.documents.excel.L;
import com.yem.or.utils.OrderApiUtils;
import kd.bamp.mbis.common.util.DynamicObjectUtil;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.util.HttpClientUtils;

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

public class DevicefilesTask extends AbstractTask {


    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        QFilter qFilter = new QFilter("status", QCP.equals, "C");
        qFilter.and("yem_customer", QCP.not_equals, null);
        String selectfieldr = DynamicObjectUtil.getSelectfields("yem_em_devicefiles", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfieldr, "yem_em_devicefiles", "yem_multiconfig"
                , false);
        DynamicObject[] yem_em_devicefiles = BusinessDataServiceHelper.load("yem_em_devicefiles", selectfields
                , new QFilter[]{qFilter});
        //list
        List<Map<String, Object>> list = new ArrayList<>();
        if (yem_em_devicefiles.length > 0) {
            for (DynamicObject mechanical : yem_em_devicefiles) {
                Map<String, Object> hashMap = new HashMap<>();
                DynamicObject yemCustomer = mechanical.getDynamicObject("yem_customer");
                if (yemCustomer != null) {
                    String yemSalesorderno = mechanical.getString("yem_salesorderno");//合同号
                    Long mechanicalId = mechanical.getLong("id");
                    DynamicObject yemModel = mechanical.getDynamicObject("yem_model");
                    String yemFramenumber = mechanical.getString("yem_framenumber");
                    String yemColor = mechanical.getString("yem_color");
                    String yemEnginecode = mechanical.getString("yem_enginecode");
                    String yemEnginemodel = mechanical.getString("yem_enginemodel");
                    BigDecimal decimal = BigDecimal.ZERO;
                    DynamicObjectCollection yemMulticonfig = mechanical.getDynamicObjectCollection("yem_multiconfig");
                    if (!"".equals(yemSalesorderno)) {
                        String selectfield = DynamicObjectUtil.getSelectfields("yem_es_salesorder", false);
                        DynamicObject[] yemEsSalesorder = BusinessDataServiceHelper.load("yem_es_salesorder", selectfield
                                , new QFilter[]{new QFilter("billno", QCP.equals, yemSalesorderno)});
                        for (DynamicObject object : yemEsSalesorder) {
                            DynamicObject yemOperator = object.getDynamicObject("yem_operator");
                            DynamicObject org = object.getDynamicObject("org");
                            if (yemOperator != null) {
                                String yemOperatorString = yemOperator.getString("name");
                                hashMap.put("salesman", yemOperatorString);
                            }
                            if (org != null) {
                                String yemOperatorString = org.getString("name");
                                hashMap.put("seller", yemOperatorString);
                            }
                        }
                    }
                    if (!yemMulticonfig.isEmpty()) {
                        for (DynamicObject dynamicObject : yemMulticonfig) {
                            decimal = decimal.add(dynamicObject.getBigDecimal("yem_qty"));
                        }
                    }
                    hashMap.put("userNo", yemCustomer.getString("number"));//客户
                    hashMap.put("salesorderno", yemSalesorderno);
                    if (yemSalesorderno != null) {
                        DynamicObject load = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", "id,billno,yem_signdate"
                                , new QFilter[]{new QFilter("billno", QCP.equals, yemSalesorderno)});
                        if (load != null) {
                            Date yemSigndate = load.getDate("yem_signdate");
                            if (yemSigndate != null) {
                                hashMap.put("orderTime", yemSigndate.getTime());
                            }
                        }
                    }
                    String yem_framenumber = mechanical.getString("yem_framenumber");
                    if (!yem_framenumber.isEmpty()) {
                        DynamicObject[] objects = BusinessDataServiceHelper.load("yem_es_storagetrans", "id,billno,yem_tddate,yem_es_materialinfo.yem_es_salesorder_z" +
                                ",yem_es_salesorder_z.yem_vehicleno", new QFilter[]{new QFilter("yem_es_materialinfo.yem_es_salesorder_z.yem_vehicleno", QCP.equals, yem_framenumber)});
                        if (objects != null) {
                            for (DynamicObject dynamicObject : objects) {
                                Date yemTddate = dynamicObject.getDate("yem_tddate");
                                if (yemTddate != null) {
                                    hashMap.put("dateofmanufacture", yemTddate.getTime());
                                }
                            }
                        }
                    }
                    if (yemModel != null) {
                        DynamicObject yemProductsgroup = yemModel.getDynamicObject("yem_productsgroup");
                        if (yemProductsgroup != null) {
                            String yemProductsString = yemProductsgroup.getString("name");
                            hashMap.put("product", yemProductsString);
                        }
                        DynamicObject yemProducts = yemModel.getDynamicObject("yem_products");
                        if (yemProducts != null) {
                            String yemProductsString = yemProducts.getString("number");
                            hashMap.put("grouptype", yemProductsString);
                        }
                        hashMap.put("model", yemModel.getString("number"));
                        hashMap.put("models", yemModel.getString("number"));
                    }
                    DynamicObject group = mechanical.getDynamicObject("group");
                    if (group != null) {
//                        DynamicObject single = BusinessDataServiceHelper.loadSingle(group.getLong("id"), "yem_em_electronicgroup");
//                        long sourceid = single.getLong("yem_sourceid");
//                        String sourcetype = single.getString("yem_sourcetype");
//                        ORM orm = ORM.create();
//                        DynamicObject products = orm.queryOne(sourcetype, new QFilter("id", QCP.equals, sourceid).toArray());
//                        if (products != null) {
                        String yem_model = group.getString("yem_model");
                        String yem_modelname = group.getString("yem_modelname");
                        String yem_modelenname = group.getString("yem_modelenname");
                        hashMap.put("productnumber", yem_model);
                        hashMap.put("productname", yem_modelname);
                        hashMap.put("productenname", yem_modelenname);
//                        } else {
//                            hashMap.put("productnumber", "");
//                            hashMap.put("productname", "");
//                            hashMap.put("productenname", "");
//                        }
                    }
                    hashMap.put("qty", decimal);
                    hashMap.put("salesordernos", yemSalesorderno);
                    hashMap.put("framenumber", yemFramenumber);
                    hashMap.put("color", yemColor);
                    hashMap.put("enginecode", yemEnginecode);
                    hashMap.put("enginemodel", yemEnginemodel);
                    hashMap.put("kingdeeId", mechanicalId);

                    list.add(hashMap);
                }
            }
            OrderApiUtils.Devicefiles(list);
        }
    }
}
