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

import com.yem.wm.utils.RequestEAS;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
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.json.JSONObject;
import org.json.XML;

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

/**
 * 定时任务分配EAS客户
 */
public class AssignEASCustomerTask 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_isassigneas", QCP.equals, false),
                        new QFilter("billstatus", QCP.equals, "C"),
                        new QFilter("yem_issyneas", QCP.equals, true)});
        DynamicObject masterOrg = BusinessDataServiceHelper.loadSingle(100000l, "bos_org");
        for (DynamicObject bill : bills) {
            ArrayList<String> list = new ArrayList<>();
            bill = BusinessDataServiceHelper.loadSingle(bill.getLong("id"), "yem_merchantapply");
            DynamicObjectCollection assignColl = bill.getDynamicObjectCollection("yem_merchantapply_a");
            for (DynamicObject assObj : assignColl) {
                DynamicObject useOrg = BusinessDataServiceHelper.loadSingle(
                        assObj.getDynamicObject("yem_assignorg").get("id"),
                        assObj.getDynamicObject("yem_assignorg").getDataEntityType().getName()).getDynamicObject("org");
                if(assObj.getBoolean("yem_isassign")&&!assObj.getBoolean("yem_isassigneas")){
                    list.add(useOrg.getString("number"));
                }
            }
            String xml = "<AssignData>"+
                    "   <AssignHead>"+
                    "       <sourceCU>"+masterOrg.getString("number")+"</sourceCU>"+
                    "       <dataNumber>"+bill.getString("yem_easnumber")+"</dataNumber>"+
                    "   </AssignHead>"+
                    "   <AssignCUS>";
            for (String s : list) {
                xml = xml + "        <AssignCU>"+
                        "             <cuNumber> "+s+"</cuNumber>"+
                        "        </AssignCU>";
            }
            xml = xml +"   </AssignCUS>"+
                    "</AssignData>";
            String s = RequestEAS.assignCustomer(xml);
            JSONObject json = XML.toJSONObject(s);
            int retstatus = json.getJSONObject("Result").getInt("retstatus");
            if(0!=retstatus){
                continue;
            }else{
                for (DynamicObject assignObj : assignColl) {
                    assignObj.set("yem_isassigneas",true);
                }
            }
            SaveServiceHelper.update(new DynamicObject[]{bill});
        }
    }
    private boolean parseXml(String retXml, ExtendedDataEntity dataEntity) {
        JSONObject json = XML.toJSONObject(retXml);
        int retstatus = json.getJSONObject("Result").getInt("retstatus");
        if(0!=retstatus){
            String content = json.getJSONObject("Result").getJSONObject("errorMessages").getJSONObject("errorMessage").getString("content");
            return false;
        }
        return true;
    }
}
