package yem.wmc.imc.utils;
import com.huawei.shade.com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.logorm.LogORM;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouc
 * @date 2023/6/12 9:45
 * @className CommonUtils
 * @description 发票云接口工具类
 */
public class CommonUtils {
//    public static String AK = "d6ce1d102c3ea0d8";
//    public static String SK = "c33f202130742d4d";

    public static String AK = "018a8f23597c0208";//同步接口-全电同步自主登录
    public static String SK = "bd59a31557a6ceb2";//同步接口-全电同步自主登录

    /**
     * 获取登录令牌
     * @return 令牌字符串
     */
    public static String getToken() {
        String Token = "";
        try {
            JSONObject JObject = new JSONObject();
            JObject.put("akString", AK);
//            JObject.put("secretString", DigestUtil.md5Hex(AK+SK));
            JObject.put("secretString",DigestUtils.md5Hex(AK+SK));
            JObject.put("type", "2");
            String param = JObject.toString();
            Map<String, String> map = new HashMap<>();
            map.put("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.24");
            map.put("ContentType", "application/json;charset=UTF-8");
            String TokenStr = HttpUtils.doPostWithHeader(APIAddressUtils.GetTokenUrl, param, map);
            if (TokenStr != null) {
                JSONObject data = JSONObject.parseObject(TokenStr);
                String code = data.getString("code");
                if (code.equals("200")) {
                    Token = data.getString("data");
                } else {
                    //记录日志
                    String message = data.getString("message");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            //记录日志
        }
        return Token;
    }

    /**
     * 单点登录获取SSOToken
     * @param Token 令牌
     * @return SSOToken字符串
     */
    public static String Login(String Token) {
        String SSOToken = "";
        Map<String, String> map = new HashMap<>();
        map.put("ContentType", "application/json;charset=UTF-8");
        map.put("X-Access-Token", Token);
        String JsonObject="";
        try {
            JsonObject = HttpUtils.doGet(APIAddressUtils.LoginUrl, map);
            if (JsonObject != null) {
                JSONObject json = JSONObject.parseObject(JsonObject);
                String code = json.getString("code");
                if (code.equals("200")) {
                    SSOToken = json.getString("data");
                } else {
                    String message = json.getString("message");
                }
            }
        }catch (IOException e){

        }
        return SSOToken;
    }


    public void invoicelog(String opname,String opdesc,String request,String returnresults,
                       String issuccessful) {
        MainEntityType entityType = EntityMetadataCache.getDataEntityType("yem_invoicelog");
        DynamicObject dynamicObject = new DynamicObject(entityType);
        dynamicObject.set("username", CommonUtils.getCurrentUser().getString("name"));//操作人
        dynamicObject.set("opname", opname);//操作名称
        dynamicObject.set("opdesc", opdesc);//操作描述
        dynamicObject.set("yem_request",request);//请求
        dynamicObject.set("yem_returnresults", returnresults);//返回结果
        dynamicObject.set("yem_issuccessful", issuccessful);//是否成功
        // 批量插入，需要保证objects中为同一个单据类型的DynamicObject
        List<DynamicObject> objects = new ArrayList<>();
        objects.add(dynamicObject);
        LogORM logORM = LogORM.create();
        // 批量插入
        logORM.insert(objects);
    }

    /**
     * 获取当前登录用户对象
     *
     * @return
     */
    public static DynamicObject getCurrentUser() {
        final Long currentUserId = Long.valueOf(RequestContext.get().getUserId());
        return BusinessDataServiceHelper.loadSingleFromCache((Object) currentUserId, "bos_user");
    }
}
