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.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;

import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhouc
 * @date 2023/10/30 15:53
 * @className AssequipmentTaskEdit
 * @description 生成总成配件图册定时任务
 */
public class AssequipmentTaskEdit extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        Log log = LogFactory.getLog(AssequipmentTaskEdit.class);
        //log.info("打印日志测试：" + df.format(new Date()));
        List list = new ArrayList<>();
//        list.add("YX635H9A1B1X1A302");
//        list.add("YX635H9A1B1X1A301");
//        list.add("YX656H3B2B1X1A200");
        list.add("CPCD-G30");
        QFilter filter = new QFilter("number", QCP.in, list);
//        QFilter filter = new QFilter("yem_isgetatlas", QCP.equals, false);
        DynamicObjectCollection yem_bd_basemodelib = QueryServiceHelper.query("yem_bd_basemodelib",
                "id,name,number,createorg,yem_products.number,yem_productsgroup.number,yem_isgetatlas"
                , new QFilter[]{filter}, null);
        if (yem_bd_basemodelib.size() > 0) {
            Addassequipment(yem_bd_basemodelib);
        }
    }

    /**
     * 生成总成配件图册
     *
     * @param yem_bd_basemodelib 产品资源明细对象
     */
    public void Addassequipment(DynamicObjectCollection yem_bd_basemodelib) {
        for (int i = 0; i < yem_bd_basemodelib.size(); i++) {
            DynamicObject basemodelib = yem_bd_basemodelib.get(i);
            DynamicObject assequipment = BusinessDataServiceHelper.newDynamicObject("yem_em_assequipment");
            assequipment.set("enable", "1");
            assequipment.set("status", "C");
            assequipment.set("name", basemodelib.getString("name"));
            assequipment.set("number", basemodelib.getString("number"));
            QFilter qFilter = new QFilter("number", QCP.equals, "110");
            DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "id", qFilter.toArray());
            assequipment.set("createorg", bos_org);
            assequipment.set("yem_brandmodel", AtlasUtils.getparent(basemodelib.getString("yem_products.number")));
            OperationResult Operate = OperationServiceHelper.executeOperate("save",
                    assequipment.getDynamicObjectType().getName(), new DynamicObject[]{assequipment}, OperateOption.create());
            if (Operate.isSuccess()) {
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(basemodelib.getLong("id"), "yem_bd_basemodelib");
                dynamicObject.set("yem_isgetatlas", true);
                Object[] save = SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
            }
        }
    }
}
