package com.yem.em.task;


import com.yem.em.utils.AtlasUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
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.OperationServiceHelper;

import java.util.Map;

/**
 * @author zhouc
 * @date 2023/12/2 17:53
 * @className BomChangeHisTaskEdit
 * @description BOM变更单修复已有数据状态
 */
public class BomChangeRepTaskEdit extends AbstractTask {

    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        Log log = LogFactory.getLog(BomChangeRepTaskEdit.class);
        QFilter filter = new QFilter("yem_entryentity.yem_isass", QCP.equals, false);
        filter.and(new QFilter("yem_entryentity.yem_isresource", QCP.equals, false));
        DynamicObjectCollection yem_em_bomchange = QueryServiceHelper.query("yem_em_bomchange",
                "id,name,number,yem_entryentity.yem_parentno,yem_entryentity.yem_isass,yem_entryentity.yem_isresource"
                , new QFilter[]{filter}, null);
        if (yem_em_bomchange.size() > 0) {
            for (int i = 0; i < yem_em_bomchange.size(); i++) {
                DynamicObject bomchange = yem_em_bomchange.get(i);
                DynamicObject newbomchange = BusinessDataServiceHelper.loadSingle(bomchange.getLong("id"), "yem_em_bomchange");
                DynamicObjectCollection yem_entryentity = newbomchange.getDynamicObjectCollection("yem_entryentity");
                for (int j = 0; j < yem_entryentity.size(); j++) {
                    DynamicObject dynamicObject = yem_entryentity.get(j);
                    boolean yem_isass = dynamicObject.getBoolean("yem_isass");
                    boolean yem_isresource = dynamicObject.getBoolean("yem_isresource");
                    if (!yem_isresource && !yem_isass) {
                        DynamicObject assatlasdet = AtlasUtils.seelctDyn("yem_em_assatlasdet", "number", dynamicObject.getString("yem_parentno"));
                        if (assatlasdet != null) {
                            dynamicObject.set("yem_isass", true);
                        }
                        DynamicObject basemodelib = AtlasUtils.seelctDyn("yem_bd_basemodelib", "number", dynamicObject.getString("yem_parentno"));
                        if (basemodelib != null) {
                            dynamicObject.set("yem_isresource", true);
                        }
                    }
                }
                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                        newbomchange.getDynamicObjectType().getName(), new DynamicObject[]{newbomchange}, OperateOption.create());
                if (Operate.isSuccess()) {
                    log.info("保存BOM变更单成功！！！");
                }
            }
        }
    }
}
