package com.yem.em.ass.Devicefiles.task;

import com.yem.rf.utils.RFUtils;
import com.yem.wm.es.salesorder.from.SalesOrderZJEdit;
import com.yem.wm.utils.YEM;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Map;

import static com.yem.wm.im.delivernotice.op.DeliverNoticeAssAuditOpEdit.Getanalysistdate;

/**
 * @Description: 更新整机档案 料单分析时间
 * @Date: 20/11/2024 3:47 pm
 * @Created: by ZZSLL
 */

public class UpdateAnalysisTimeTask extends AbstractTask {

    private static final Logger log = LoggerFactory.getLogger(UpdateAnalysisTimeTask.class);

    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        QFilter qFilter = new QFilter("yem_analysistdate", QCP.is_null, "");
        qFilter.and("yem_framenumber", QCP.is_notnull, "");
        qFilter.and("yem_model", QCP.is_notnull, "");
        DynamicObject[] load = BusinessDataServiceHelper.load("yem_em_devicefiles", "id, number, yem_framenumber, yem_analysistdate, yem_model.yem_productsgroup", qFilter.toArray());
        log.info("load data {}", load.length);
        for (DynamicObject d : load) {
            String framenumber = d.getString("yem_framenumber");
            DynamicObject resource = d.getDynamicObject("yem_model");
            if (YEM.isNotEmpty(resource)) {
                DynamicObject productsgroup = resource.getDynamicObject("yem_productsgroup");
                if (YEM.isNotEmpty(productsgroup)) {
                    Date analysistdate = Getanalysistdate(framenumber, resource);
                    d.set("yem_analysistdate", analysistdate);
                    log.info("update {} {} date {}", d.getString("number"), framenumber, analysistdate);
                }
            }
        }
        SaveServiceHelper.save(load);
    }
}
