package com.yem.wm.im.merchantapply.task;

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.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
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.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

/**
 * 定时任务分配外贸客户
 */
public class AssignCosmicCustomerTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {

        DynamicObject[] bills = BusinessDataServiceHelper.load("yem_merchantapply", "id",
                new QFilter[]{new QFilter("yem_merchantapply_a.yem_isassign", QCP.equals, false),
                        new QFilter("billstatus", QCP.equals, "C")});
        DynamicObject masterOrg = BusinessDataServiceHelper.loadSingle(100000l, "bos_org");
        for (DynamicObject bill : bills) {
            bill = BusinessDataServiceHelper.loadSingle(bill.getLong("id"), "yem_merchantapply");
            DynamicObject cusObj = BusinessDataServiceHelper.loadSingle("bd_customer",
                    new QFilter[]{new QFilter("yem_merchantapplyid", QCP.equals, bill.getLong("id"))});
            if (YEM.isEmpty(cusObj)) continue;
            bill.set("yem_easnumber", cusObj.getString("number"));
            DynamicObjectCollection assColl = bill.getDynamicObjectCollection("yem_merchantapply_a");
            for (DynamicObject assObj : assColl) {
                //判断客户苍穹分配记录是否已生成成功
                //维度为分配组织，根组织，消费成功，客户
                DynamicObject useOrg = BusinessDataServiceHelper.loadSingle(
                        assObj.getDynamicObject("yem_assignorg").get("id"),
                        assObj.getDynamicObject("yem_assignorg").getDataEntityType().getName());
                boolean exists = QueryServiceHelper.exists("yem_bd_customerurc", new QFilter[]{
                        new QFilter("useorg.id", QCP.equals, useOrg.getDynamicObject("org").get("id")),//分配组织
                        new QFilter("assignorg.id", QCP.equals, 100000),//根组织
                        new QFilter("consumestatus.value", QCP.equals, 2),//消费成功
                        new QFilter("data.id", QCP.equals, cusObj.get("id"))});//客户
                if (exists) {
                    assObj.set("yem_isassign", true);
                }
                if (!assObj.getBoolean("yem_isassign")&&!QueryServiceHelper.exists("yem_bd_customerurc", new QFilter[]{
                        new QFilter("assignorg.id", QCP.equals, 100000l),//根组织
                        new QFilter("useorg.id", QCP.equals, useOrg
                                .getDynamicObject("org").get("id")),//分配组织
                        new QFilter("consumestatus.value", QCP.equals, 0),//未消费
                        new QFilter("data.id", QCP.equals, cusObj.get("id"))})) {//客户
                    // 生成客户苍穹分配记录
                    MainEntityType entityType = EntityMetadataCache.getDataEntityType("yem_bd_customerurc");
                    DynamicObject cusassign = new DynamicObject(entityType);
                    cusassign.set("assignorg", masterOrg);
                    cusassign.set("data", cusObj);
                    cusassign.set("useorg", useOrg
                            .getDynamicObject("org"));
                    cusassign.set("syntype", 1);
                    cusassign.set("createtime", new Date());
                    cusassign.set("consumestatus", 0);
                    cusassign.set("valid", "1");
                    OperationServiceHelper.executeOperate("save",
                            "yem_bd_customerurc", new DynamicObject[]{cusassign}, OperateOption.create());
                }
            }
            SaveServiceHelper.update(new DynamicObject[]{bill});
        }
    }
}
