package com.yem.em.bd.electronicgroup.task;

import com.yem.wm.utils.RequestCmmp;
import com.yem.wm.utils.YEM;
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.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.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.Map;

public class UpdateSourceBaseTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        ORM orm = ORM.create();
        DynamicObjectCollection collection = orm.query("yem_em_electronicgroup", null);
        for (DynamicObject dynamicObject : collection) {
            String sourcetype = dynamicObject.getString("yem_sourcetype");
            long sourceid_preset = dynamicObject.getLong("yem_sourceid");
            String source_number = dynamicObject.getString("number");
            if (YEM.isNotEmpty(sourcetype) && YEM.isNotEmpty(source_number)) {
                dynamicObject.set("yem_sourcetypebase", sourcetype);
                DynamicObjectCollection sourceList = orm.query(sourcetype, new QFilter("number", QCP.equals, source_number).toArray());
                if (sourceList.isEmpty()) {
                    sourceList = orm.query(sourcetype, new QFilter("id", QCP.equals, sourceid_preset).toArray());
                }
                if (sourceList.isEmpty()) {
                    continue;
                }
                DynamicObject source = sourceList.get(0);
                Long sourceid = source.getLong("id");
                dynamicObject.set("yem_sourcebase", sourceid);
                // 产品机型
                if ("yem_bd_productstree".equals(sourcetype)) {
                    DynamicObject group = source.getDynamicObject("group");
                    if (group == null) {
                        continue;
                    }
                    DynamicObject top = getTopLevelGroup(group, sourcetype);
                    top = BusinessDataServiceHelper.loadSingle(top.getPkValue(), "yem_bd_productsgroup");
                    dynamicObject.set("yem_model", top.getString("number"));
                    dynamicObject.set("yem_modelname", top.getString("name"));
                    dynamicObject.set("yem_modelenname", top.getString("yem_enname"));
                }
                // 产品类型
                if ("yem_bd_productsgroup".equals(sourcetype)) {
                    DynamicObject top = getTopLevelGroup(source, sourcetype);
                    top = BusinessDataServiceHelper.loadSingle(top.getPkValue(), "yem_bd_productsgroup");
                    dynamicObject.set("yem_model", top.getString("number"));
                    dynamicObject.set("yem_modelname", top.getString("name"));
                    dynamicObject.set("yem_modelenname", top.getString("yem_enname"));
                }
            }
        }
        SaveServiceHelper.update(collection.toArray(new DynamicObject[0]));
    }

    private DynamicObject getTopLevelGroup(DynamicObject group, String sourcetype) {
        group = BusinessDataServiceHelper.loadSingle(group.getLong("id"), "yem_bd_productsgroup");
        DynamicObject parent = group.getDynamicObject("parent");
        if (parent == null) {
            return group;
        }
        return getTopLevelGroup(parent, sourcetype);
    }
}
