package com.yem.or.task;

import com.yem.or.utils.OrderApiUtils;
import com.yem.wm.bd.customer.from.Validator.CustomerValidator;
import com.yem.wm.task.YunZhiJaGetOpenidTask;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
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.operation.SaveServiceHelper;

import java.util.Map;

/**
 * ljw
 * 同步数据至下单平台
 */
public class CustomerTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        Log log = LogFactory.getLog(YunZhiJaGetOpenidTask.class);
        QFilter qFilter = new QFilter("yem_checkboxfield", QCP.equals, false);
        qFilter.and("status", QCP.equals, "C");
        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load("bd_customer", "id,number,name,yem_checkboxfield,yem_torderid,group"
                , new QFilter[]{qFilter});
        for (DynamicObject dynamicObject : dynamicObjects) {
            DynamicObject group = dynamicObject.getDynamicObject("group");
            if (group != null) {
                long groupid = group.getLong("id");
                DynamicObject[] bd_customergroup = BusinessDataServiceHelper.load("bd_customergroup", "id,number,parent"
                        , new QFilter[]{new QFilter("parent.number", QCP.equals, "11602"), new QFilter("id", QCP.equals, groupid)});
                if (bd_customergroup.length > 0) {
                    for (int i = 0; i < bd_customergroup.length; i++) {
                        String number = dynamicObject.getString("number");
                        String replace = number.replace("-", "");
                        String name = dynamicObject.getString("name");
                        Map<String, String> demands = OrderApiUtils.demands(name, replace);
                        String msg = demands.get("msg");
                        if (msg == null) {
                            String id = demands.get("id");
                            if (!"".equals(id)) {
                                dynamicObject.set("yem_torderid", id);
                                dynamicObject.set("yem_checkboxfield", true);
                                SaveServiceHelper.update(dynamicObject);
                                SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                            }
                        }
                    }
                }
            }
        }

    }
}
