package com.rongwei.bscommon.sys.utils;

import com.rongwei.bscommon.sys.fegin.CommonFeginClient;
import com.rongwei.commonservice.service.RedisService;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.rwcommonentity.commonservers.vo.SysNotifyAnnounceVo;
import com.rongwei.safecommon.utils.CXCommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class FileSystemUtils {
    private static final Logger log = LoggerFactory.getLogger(FileSystemUtils.class.getName());
    @Autowired
    private RedisService autoRedisService;
    @Autowired
    private CommonFeginClient autoCommonFeginClient;
    private static RedisService redisService;

    private static CommonFeginClient commonFeginClient;

    @PostConstruct
    public void info(){
        redisService = autoRedisService;
        commonFeginClient = autoCommonFeginClient;
    }

    /**
     * 获取当前登录用户
     *
     * @return
     */
    public static SysUserVo getCurrentUser() {
        // 获取当前登录人信息
        SysUserVo currUser = null;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String token = request.getHeader("token");
            if (com.rongwei.rwcommon.utils.StringUtils.isNotEmpty(token)) {
                currUser = redisService.getLoginUser(token);
            }
        }
        return currUser == null ? new SysUserVo() : currUser;
    }

    /**
     * 发送通知信息
     */
    public static void sendNotify(String title, String content, String remark, List<String> recipientIds) {
        sendNotify(title, content, remark, recipientIds, null);
    }

    /**
     * 发送通知信息
     */
    public static void sendNotify(String title, String content, String remark,
                                  List<String> recipientIds, String roption) {
        if (recipientIds == null || recipientIds.isEmpty()) {
            log.debug("接收人ID为空");
            return;
        }
        SysNotifyAnnounceVo sysNotifyAnnounceVo = new SysNotifyAnnounceVo();
        sysNotifyAnnounceVo.setId(SecurityUtil.getUUID());
        sysNotifyAnnounceVo.setSenderid("8672bf72ab274bec83052868ae336b38");
        sysNotifyAnnounceVo.setNotifytype("notify");
        sysNotifyAnnounceVo.setNotifystatus("3");
        sysNotifyAnnounceVo.setNotifytitle(title);
        sysNotifyAnnounceVo.setNotifycontent(content);
        sysNotifyAnnounceVo.setRemark(remark);
        // 特殊处理
        if (StringUtils.isNotBlank(roption)) {
            sysNotifyAnnounceVo.setRoption(roption);
        }

//        sysNotifyAnnounceVo.setCreatedate(new Date());
        sysNotifyAnnounceVo.setUserid(recipientIds.stream().distinct().collect(Collectors.joining(",")));
        log.debug("开始通过fegin发送消息通知: {}", sysNotifyAnnounceVo);
        CXCommonUtils.sendNotify(sysNotifyAnnounceVo.getTitle(), sysNotifyAnnounceVo.getNotifycontent(), sysNotifyAnnounceVo.getRemark(), recipientIds, sysNotifyAnnounceVo.getRoption(), sysNotifyAnnounceVo.getNotifystatus());
//        commonFeginClient.sendNotify(sysNotifyAnnounceVo);
    }


}
