package com.yem.em.task;

import com.yem.em.utils.AtlasUtils;
import com.yem.wm.utils.RequestCmmp;
import com.yem.wm.utils.StringUtils;
import com.yem.wm.utils.YEM;
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.entity.tree.TreeNode;
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 kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.yem.em.utils.AtlasUtils.seelctDyn;

/**
 * @author zhouc
 * @date 2023/11/1 19:59
 * @className CMMPTableTaskEdit
 * @description CMMP部件明细表 作废
 */
public class CMMPTableTaskEdit extends AbstractTask {
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        Log log = LogFactory.getLog(AssequipmentTaskEdit.class);
        List list = new ArrayList<>();
        list.add("YX635H9A1B1X1A302");
        list.add("YX635H9A1B1X1A301");
        list.add("YX656H3B2B1X1A200");
//        QFilter filter = new QFilter("number", QCP.in, list);
        QFilter filter = new QFilter("yem_iscmmp", QCP.equals, false);
        filter.and(new QFilter("enable", QCP.equals, "1"));
        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) {
            CMMPtable(yem_bd_basemodelib);
        }
    }

    /**
     * 生成总成配件图册
     *
     * @param yem_bd_basemodelib 产品资源明细对象
     */
    public void CMMPtable(DynamicObjectCollection yem_bd_basemodelib) {
        DynamicObject yem_bd_productsgroup = seelctDyn("yem_bd_productsgroup", "number", "05");//作废

        for (int i = 0; i < yem_bd_basemodelib.size(); i++) {
            DynamicObject basemodelib = yem_bd_basemodelib.get(i);
            DynamicObject cmmptable = BusinessDataServiceHelper.newDynamicObject("yem_em_cmmptable");
            cmmptable.set("enable", "1");
            cmmptable.set("status", "C");
            cmmptable.set("name", basemodelib.getString("name"));
            cmmptable.set("number", basemodelib.getString("number"));
            QFilter qFilter = new QFilter("number", QCP.equals, "110");
            DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "id", qFilter.toArray());
            cmmptable.set("createorg", bos_org);
            DynamicObjectCollection yem_entryentity = cmmptable.getDynamicObjectCollection("yem_entryentity");
            String sql = "select distinct " + AtlasUtils.Queryfield("ccode", "qty") + "  from VIEW_BASE_ITEMSTRUC_WM where pcode='" + basemodelib.getString("number") + "'";
            List<Map> fromCmmp = RequestCmmp.getFromCmmp(bos_org.getLong("id"), sql, yem_bd_productsgroup.getLong("id"));
            Map<String, String> stringStringMap = AtlasUtils.GetasscomponentName(fromCmmp, "Ass");
            for (int k = 0; k < fromCmmp.size(); k++) {
                Map map = fromCmmp.get(k);
                if (map != null) {
                    DynamicObject dynamicObject = yem_entryentity.addNew();
                    String ccode = map.get("ccode").toString();
                    dynamicObject.set("yem_ccode", ccode);
                    dynamicObject.set("yem_ccodename", stringStringMap.get(ccode));
                    dynamicObject.set("yem_qty", map.get("qty").toString());
                }
            }
            OperationResult Operate = OperationServiceHelper.executeOperate("save",
                    cmmptable.getDynamicObjectType().getName(), new DynamicObject[]{cmmptable}, OperateOption.create());
            if (Operate.isSuccess()) {
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(basemodelib.getLong("id"), "yem_bd_basemodelib");
                dynamicObject.set("yem_iscmmp", true);
                Object[] save = SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
            }
        }
    }


}
