OpsClientService.java 3.4 KB
package com.viontech.fanxing.task.service;

import com.alibaba.fastjson.JSONObject;
import com.viontech.fanxing.commons.constant.LogType;
import com.viontech.fanxing.commons.exception.FanXingException;
import com.viontech.fanxing.commons.feign.OpsClient;
import com.viontech.fanxing.commons.model.Channel;
import com.viontech.fanxing.commons.model.Content;
import com.viontech.fanxing.commons.model.DictCode;
import com.viontech.fanxing.commons.vo.LogVo;
import com.viontech.keliu.util.JsonMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/9/28
 */

@Service
@Slf4j
public class OpsClientService {

    @Resource
    private OpsClient opsClient;

    public String getStoreConfigById(Long id) {
        JSONObject res = opsClient.getStoreConfigById(id);
        if (!res.getBoolean("success")) {
            throw new FanXingException(res.getString("msg"));
        } else {
            JSONObject data = res.getJSONObject("data");
            if (data == null) {
                return null;
            } else {
                return data.getString("config");
            }
        }
    }


    public String getContentByName(String name) {
        JsonMessageUtil.JsonMessage<List<Content>> res = opsClient.getContentByName(name);
        if (res.getCode() != 200) {
            throw new FanXingException(res.getMsg());
        } else {
            List<Content> data = res.getData();
            return (data == null || data.size() == 0) ? null : data.get(0).getContent();
        }
    }

    public Channel getChannelByChannelUnid(String channelUnid) {
        JsonMessageUtil.JsonMessage<List<Channel>> res = opsClient.getChannelByChannelUnid(channelUnid);
        if (res.getCode() != 200) {
            throw new FanXingException(res.getMsg());
        } else {
            List<Channel> data = res.getData();
            return (data == null || data.size() == 0) ? null : data.get(0);
        }
    }

    public DictCode getDictCodeByUnid(String unid) {
        JsonMessageUtil.JsonMessage<List<DictCode>> res = opsClient.getDictCodeByUnid(unid);
        if (res.getCode() != 200) {
            throw new FanXingException(res.getMsg());
        } else {
            List<DictCode> data = res.getData();
            return (data == null || data.size() == 0) ? null : data.get(0);
        }
    }

    public void addLog(String content) {
        try {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            String username = null;
            if (requestAttributes != null) {
                HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
                username = URLDecoder.decode(request.getHeader("username"), "utf8");
            }
            LogVo logVo = new LogVo();
            logVo.setUsername(username);
            logVo.setLogType(LogType.TASK.value);
            logVo.setContent(content);
            opsClient.addLog(logVo);

        } catch (Exception e) {
            log.error("添加日志失败:", e);
        }
    }

}