package com.yem.wm.task;

import com.yem.wm.utils.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.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 java.util.Date;
import java.util.Map;

public class StoraGetransToDeviceFilesTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        DynamicObject[] load = BusinessDataServiceHelper.load("yem_es_storagetrans", "id,billno,yem_tddate,yem_es_materialinfo" +
                        ",yem_es_materialinfo.yem_es_salesorder_z,yem_es_salesorder_z.yem_resourcescode,yem_es_salesorder_z.yem_vehicleno,"
                , new QFilter[]{new QFilter("billstatus", QCP.equals, "C")});
        if (load.length > 0) {
            for (DynamicObject dynamicObject : load) {
                Date yemTddate = dynamicObject.getDate("yem_tddate");
                DynamicObjectCollection yemEsMaterialinfo = dynamicObject.getDynamicObjectCollection("yem_es_materialinfo");
                if (yemEsMaterialinfo.size() > 0) {
                    for (DynamicObject YemEsmat : yemEsMaterialinfo) {
                        DynamicObjectCollection yemEsSalesorderZ = YemEsmat.getDynamicObjectCollection("yem_es_salesorder_z");
                        if (yemEsSalesorderZ.size() > 0) {
                            for (DynamicObject object : yemEsSalesorderZ) {
                                DynamicObject yemResourcescode = object.getDynamicObject("yem_resourcescode");
                                String yemVehicleno = object.getString("yem_vehicleno");
                                if (yemResourcescode != null) {
                                    long yemResourcescodeLong = yemResourcescode.getLong("id");
                                    String yemEmDevicefiles = DynamicObjectUtil.getSelectfields("yem_em_devicefiles", false);
                                    QFilter qFilter = new QFilter("yem_framenumber", QCP.equals, yemVehicleno);
                                    qFilter.and("yem_model.id", QCP.equals, yemResourcescodeLong)
                                            .and("billstatus", QCP.equals, "C");
                                    DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load("yem_em_devicefiles", yemEmDevicefiles
                                            , new QFilter[]{qFilter});
                                    if (dynamicObjects.length > 0) {
                                        for (DynamicObject dynamic : dynamicObjects) {
                                            dynamic.set("yem_dateofmanufacture", yemTddate);
                                        }
                                        SaveServiceHelper.update(dynamicObjects);
                                        SaveServiceHelper.save(dynamicObjects);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
