package com.yem.rf.task;

import com.yem.wm.utils.ContactSignUtils;
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.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @Description: TODO
 * @Date: 2024/9/5 9:38
 * @Created: by ZZSLL
 */

public class ExportDeclarationDataFIxTask extends AbstractTask {

    private static final Logger logger = LoggerFactory.getLogger(ExportDeclarationDataFIxTask.class);

    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        String selectfields = DynamicObjectUtil.getSelectfields("yem_es_declaredocx");
        selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, "yem_es_declaredocx", "yem_es_materialinfo");
        selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, "yem_es_declaredocx", "yem_es_declare_ecd");
        DynamicObject[] load = BusinessDataServiceHelper.load("yem_es_declaredocx", selectfields, null);
        for (DynamicObject b : load) {
            DynamicObjectCollection relation = b.getDynamicObjectCollection("yem_es_declare_ecd");
            DynamicObjectCollection m = b.getDynamicObjectCollection("yem_es_materialinfo");
            for (DynamicObject r : relation) {
                int i = relation.indexOf(r);
                String decsourceentryidtext = r.getString("yem_decsourceentryidtext");
                if ("0".equals(decsourceentryidtext)) {
                    logger.info("修复-{}-{}行", b.getString("billno"), i);
                    int fgseq = r.getInt("yem_fgseq");
                    if (fgseq - 1 <= m.size()) {
                        DynamicObject detail = m.get(fgseq - 1);
                        long id = detail.getLong("id");
                        r.set("yem_decsourceentryidtext", String.valueOf(id));
                        logger.info("new Id: {}", id);
                    }
                }
            }
        }
        SaveServiceHelper.save(load);
    }
}
