package com.engine.organization.transmethod;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import org.apache.commons.lang.StringUtils;
import weaver.systeminfo.SystemEnv;

/**
 * @author:dxfeng
 * @createTime: 2022/07/05
 * @version: 1.0
 */
public class LogViewTransMethod {

    public String getOperateDetail(String message, String languageId) {
        if("8".equals(languageId)){
            message=message.replace(":由\"",": Change from \"").replace("\"修改为\"","\" to \"");
        }
        return message;
    }

    public String getOperateModuleName(String moduleName, String languageId) {
        Integer language = Convert.toInt(languageId, -1);
        if (-1 == language) {
            return moduleName;
        }
        if (StringUtils.isNotBlank(moduleName)) {
            LogModuleNameEnum moduleNameEnum = LogModuleNameEnum.getByName(moduleName);
            if (null == moduleNameEnum) {
                return moduleName;
            }
            return SystemEnv.getHtmlLabelName(moduleNameEnum.getLabelId(), language);
        }
        return "";
    }

    public String getOperateType(String operateType, String languageId) {
        if (StringUtils.isNotBlank(operateType)) {
            OperateTypeEnum operateTypeEnum = OperateTypeEnum.getByValue(operateType);
            if (null == operateTypeEnum) {
                return "";
            }
            Integer language = Convert.toInt(languageId, -1);
            if (-1 == language) {
                return operateTypeEnum.getLabel();
            }
            return SystemEnv.getHtmlLabelName(operateTypeEnum.getLabelId(), language);
        }
        return "";
    }

    public String getDateTimeFormat(String dateTime) {
        if (StringUtils.isNotBlank(dateTime)) {
            dateTime = dateTime.substring(0, 19);
        }
        return dateTime;
    }

    public static String getFormatJson(String jsonStr) {
        if (StringUtils.isNotBlank(jsonStr)) {
            try {
                JSONObject object = JSONObject.parseObject(jsonStr);
                jsonStr = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                        SerializerFeature.WriteDateUseDateFormat);
                jsonStr = jsonStr.replace("\n", "<br/>").replace("\t", "&nbsp&nbsp&nbsp&nbsp");
            } catch (Exception e) {
                return jsonStr;
            }
        }
        return jsonStr;
    }
}
