package com.yem.wm.im.delivernotice.utils;

import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhouc
 * @date 2024/1/16 9:40
 * @className DeliverNoticeUtilsEdit
 * @description 发货通知单公共类
 */
public class DeliverNoticeUtilsEdit {

    /**
     * 根据合同号、资源号、整机编号获取整机生产信息
     *
     * @param contractnumber 合同号
     * @param resourcescode  资源号
     * @param vehicleno      整机编号
     * @param entityName     类型 yem_multiconfig 多功能 yem_retrofitting 加装
     * @return 根据不同类型返回不同的集合
     */
    public static Map<String,DynamicObjectCollection> Gettracktaskconsole(String contractnumber, DynamicObject resourcescode, String vehicleno, String entityName) {
        Map<String,DynamicObjectCollection> map = new HashMap<>();
        DynamicObjectCollection dynamicObject = new DynamicObjectCollection();
        DynamicObjectCollection dynamicObject3 = new DynamicObjectCollection();
        String yem_resourcescode = resourcescode.getString("number");
        QFilter qFilter = new QFilter("yem_billtype.number", QCP.equals, "yem_tracktaskconsole_zj");
        qFilter.and(new QFilter("yem_contractnumber", QCP.equals, contractnumber));
        qFilter.and(new QFilter("yem_es_salesorder_r.yem_es_salesorder_z.yem_resourcescode.id", QCP.equals, resourcescode.getLong("id")));
        qFilter.and(new QFilter("yem_es_salesorder_r.yem_es_salesorder_z.yem_vehicleno", QCP.equals, vehicleno));
        DynamicObjectCollection data = QueryServiceHelper.query("yem_tracktaskconsole", "id,yem_es_salesorder_r.yem_entrtyid", new QFilter[]{qFilter}, null);
        if (data.size() > 0) {
            DynamicObject dynamicObject1 = data.get(0);
            if (dynamicObject1 != null) {
                long yem_entrtyid = dynamicObject1.getLong("yem_es_salesorder_r.yem_entrtyid");
                DynamicObject yem_tracktaskconsole = BusinessDataServiceHelper.loadSingle(dynamicObject1.getLong("id"), "yem_tracktaskconsole");
                if (yem_tracktaskconsole != null) {
                    //商品信息
                    DynamicObjectCollection yem_es_materialinfo = yem_tracktaskconsole.getDynamicObjectCollection("yem_es_materialinfo");
                    for (int i = 0; i < yem_es_materialinfo.size(); i++) {
                        DynamicObject materialinfo = yem_es_materialinfo.get(i);
                        DynamicObject yem_resourcenog = materialinfo.getDynamicObject("yem_resourcenog");
                        if (yem_resourcenog != null) {
//                            if (yem_resourcenog.getString("number").equals(yem_resourcescode)) {
                                if(yem_entrtyid==materialinfo.getLong("id")){
                                //多功能
                                DynamicObjectCollection dynamicObjectCollection = materialinfo.getDynamicObjectCollection("yem_multiconfig");
                                for (int j = 0; j < dynamicObjectCollection.size(); j++) {
                                    DynamicObject dynamicObject2 = dynamicObjectCollection.get(j);
                                    dynamicObject.add(dynamicObject2);
                                }
                                map.put("yem_multiconfig",dynamicObject);
                                DynamicObjectCollection retrofittingdynamicObjectCollection = materialinfo.getDynamicObjectCollection("yem_retrofitting");
                                for (int j = 0; j < retrofittingdynamicObjectCollection.size(); j++) {
                                    DynamicObject dynamicObject2 = retrofittingdynamicObjectCollection.get(j);
                                    dynamicObject3.add(dynamicObject2);
                                }
                                map.put("yem_retrofitting",dynamicObject3);
                            }
                        }
                    }
                }
            }
        }
        return map;
    }
}
