package yem.wmc.imc.utils;

//import json.JSONObject;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.id.ID;
import kd.bos.logorm.LogORM;

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

/**
 * 接口日志工具类
 *
 * @author zhouc
 */
public class ApilogUtils {

    /**
     * @param resultMap
     * @param billno
     * @param opname
     * @param opdesc
     * @param interfacetype 接口类型 AddCusDec
     */
    public static void Createlog(Map<String, Object> resultMap, String billno, String opname, String opdesc, String interfacetype, JSONObject jsonData) {
        //写入日志信息
        // 根据实体名构建元数据并创建DynamicObject
        MainEntityType entityType = EntityMetadataCache.getDataEntityType("yem_ca_apilog");
        DynamicObject dynamicObject = new DynamicObject(entityType);
        dynamicObject.getDataEntityType().getPrimaryKey().setValueFast(dynamicObject, ID.genLongId());
        dynamicObject.set("username", CommonUtils.getCurrentUser().getString("name"));//操作人
        dynamicObject.set("opname", opname);//操作名称
        dynamicObject.set("opdesc", opdesc);
        dynamicObject.set("opdate", new Date());
        if (resultMap.get("result") == Boolean.TRUE) {
            dynamicObject.set("yem_description", "同步成功");
            dynamicObject.set("yem_issuccess", "success");//是否成功
        } else {
            dynamicObject.set("yem_description", "同步失败请查看详细信息");//详细信息
            dynamicObject.set("yem_issuccess", "fail");//是否成功
        }
        dynamicObject.set("yem_description_tag", resultMap.get("billno") + ":" + resultMap.get("description"));
        dynamicObject.set("yem_datajson", billno);//相关信息
        dynamicObject.set("yem_datajson_tag", jsonData.toJSONString());
        dynamicObject.set("yem_interfacetype", interfacetype);//接口类型
        // 批量插入，需要保证objects中为同一个单据类型的DynamicObject
        List<DynamicObject> objects = new ArrayList<>();
        objects.add(dynamicObject);

        // 初始化LogORM实例
        LogORM logORM = LogORM.create();
        // 批量插入
        logORM.insert(objects);
    }
}
