package net.northking.cctp.common.feign;

import com.hzbank.testteam.autotest.dependencies.authDependency.entity.User;
import com.hzbank.testteam.autotest.dependencies.authDependency.utils.RequestUtil;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.entity.Log;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.log.LogPublisher;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import net.northking.cctp.common.dto.CpLogAddDto;
import net.northking.cctp.common.util.WebNetUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

@Slf4j
public class MonsterLogServer {

    /**
     * ui删除操作日志记录发送monster
     *
     * @param cpLogAddDto ui 日志记录对象
     * @return
     */
    public static void sendLogToMonster(CpLogAddDto cpLogAddDto){
        Map<String, LogPublisher> beansOfType = SpringUtil.getBeansOfType(LogPublisher.class);
        if (CollectionUtils.isEmpty(beansOfType)) {
            log.info("操作日志发送monster未开启...");
        }
        Collection<LogPublisher> values = beansOfType.values();
        LogPublisher logPublisher = values.stream().findFirst().get();
        Log log = new Log();
        log.setType(Integer.valueOf(cpLogAddDto.getType())); // 日志类型（1-新增 2-修改 3-删除 4-登录 5-登出 6-鉴权 7-系统关闭）
        log.setSource("UI测试");
        log.setModule(cpLogAddDto.getModule());
        log.setContent(cpLogAddDto.getContent());
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        log.setOperatorAddr(WebNetUtils.getClientIPAddress(request));
        User user = RequestUtil.getUser();
        log.setOperatorId(user.getId());
        log.setOperatorAccount(user.getUserName());
        log.setOperatorName(user.getDisplayName());
        log.setOperateTime(new Date());
        logPublisher.sendLog(log);
    }
}
