package com.yc.ship.module.trade.framework.common;


import cn.hutool.core.map.MapUtil;
import com.yc.ship.module.trade.framework.aop.TradeOrderLogAspect;

import java.util.Map;

/**
 * 交易订单的操作日志 Utils
 *
 */
public class TradeOrderLogUtils {

    public static void setOrderInfo(Long id, Integer beforeStatus, Integer afterStatus) {
        TradeOrderLogAspect.setOrderInfo(id, beforeStatus, afterStatus, null);
    }

    public static void setOrderInfo(Long id, Integer beforeStatus, Integer afterStatus,Map<String, Object> exts) {
        TradeOrderLogAspect.setOrderInfo(id, beforeStatus, afterStatus, exts);
    }

    public static void setOrderInfo(Long id, Integer beforeStatus, Integer afterStatus,Map<String, Object> exts,boolean isAppend) {
        if(isAppend){
            exts = appendExts(exts);
        }
        TradeOrderLogAspect.setOrderInfo(id, beforeStatus, afterStatus, exts);
    }


    public static void setUserInfo(Long userId, Integer userType) {
        TradeOrderLogAspect.setUserInfo(userId, userType);
    }

    public static Map<String, Object> append(String key,String content) {
        Map<String, Object> map = getAndSetExts(null);
        String cacheContent = MapUtil.getStr(map,key,"");
        cacheContent += content;
        map.put(key,cacheContent);
        TradeOrderLogAspect.setExts(map);
        return map;
    }

    public static Map<String, Object> appendExts(Map<String, Object> exts) {
        Map<String, Object> map = getAndSetExts(exts);
        TradeOrderLogAspect.setExts(map);
        return map;
    }

    private static Map<String, Object> getAndSetExts(Map<String, Object> exts) {
        Map<String, Object> map = TradeOrderLogAspect.getExts();
        if(map == null){
            map = MapUtil.<String, Object>builder().build();
        }
        if(exts!=null){
            map.putAll(exts);
        }
        return map;
    }

}
