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

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.model.StoreConfig;
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.util.List;

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

@Service
@Slf4j
public class OpsClientService {

    @Resource
    private OpsClient opsClient;

    public StoreConfig getStoreConfigById(Long id) {
        JsonMessageUtil.JsonMessage<StoreConfig> res = opsClient.getStoreConfigById(id);
        if (res.getCode() != 200) {
            throw new FanXingException(res.getMsg());
        } else {
            return res.getData();
        }
    }


    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 = request.getHeader("username");
            }
            LogVo logVo = new LogVo();
            logVo.setUsername(username);
            logVo.setLogType(LogType.TASK.value);
            logVo.setContent(content);
            opsClient.addLog(logVo);

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

}