package jnpf.notice.utils;

import cn.hutool.json.JSONUtil;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.model.notice.domain.FtbNoticeAnnouncementsLog;
import jnpf.model.notice.dto.FtbNoticeAnnouncementsDto;
import jnpf.notice.service.FtbNoticeAnnouncementsLogService;
import jnpf.notice.service.FtbNoticeAnnouncementsReceiveService;
import jnpf.notice.service.FtbNoticeFilesService;
import jnpf.notice.service.FtbNoticeUserGroupMembersService;
import jnpf.utils.FeignHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class NoticeAsyncDealUtil {
    @Autowired
    @Lazy
    private FtbNoticeUserGroupMembersService ftbNoticeUserGroupMembersService;

    @Lazy
    @Autowired
    private FtbNoticeAnnouncementsReceiveService ftbNoticeAnnouncementsReceiveService;

    @Lazy
    @Autowired
    private FtbNoticeFilesService ftbNoticeFilesService;

    @Lazy
    @Autowired
    private FtbNoticeAnnouncementsLogService ftbNoticeAnnouncementsLogService;

    @Lazy
    @Autowired
    private NoticeIMUtils noticeIMUtils;

    /**
     * 异步添加用户分组成员数据
     *
     * @param tenantCode 租户
     * @param groupId    分组id
     * @param userList   人员列表
     */
    @Async
    public void asyncAddUserGroupMember(String tenantCode, String groupId, List<String> userList, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeUserGroupMembersService.batchAddGroupUser(groupId, userList);
        } catch (Exception e) {
            log.error("异步添加用户分组成员异常:tenantCode={},groupId={},data={}", tenantCode, groupId, JSONUtil.toJsonStr(userList));
        } finally {
            FeignHolder.clear();
        }
    }

    /**
     * 异步修改用户分组成员数据
     *
     * @param tenantCode 租户
     * @param groupId    分组ID
     * @param userList   人员列表
     */
    @Async
    public void asyncUpdateUserGroupMember(String tenantCode, String groupId, List<String> userList, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeUserGroupMembersService.batchDeleteGroupUser(groupId);
            ftbNoticeUserGroupMembersService.batchAddGroupUser(groupId, userList);
        } catch (Exception e) {
            log.error("异步添加用户分组成员异常:tenantCode={},groupId={},data={}", tenantCode, groupId, JSONUtil.toJsonStr(userList));
        } finally {
            FeignHolder.clear();
        }
    }


    /**
     * 给通知公告异步添加接收用户
     *
     * @param tenantCode     租户
     * @param announcementId 公告ID
     * @param userList       接收用户
     * @param headers
     */
    @Async
    public void asyncAddAnnouncementMember(String tenantCode, String announcementId, List<String> userList, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeAnnouncementsReceiveService.addReceiveUser(announcementId, userList);
        } catch (Exception e) {
            log.error("异步添加公告接收人异常:tenantCode={},announcementId={},data={}", tenantCode, announcementId, JSONUtil.toJsonStr(userList));
        } finally {
            FeignHolder.clear();
        }
    }

    /**
     * 撤销公告用户接收用户
     *
     * @param tenantCode     租户
     * @param announcementId 公告ID
     * @param headers
     */
    @Async
    public void resetReadAndAlertAndWebCancel(String tenantCode, String announcementId, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeAnnouncementsReceiveService.resetReadAndAlertAndWebCancel(announcementId);
        } catch (Exception e) {
            log.error("异步撤销公告接收人异常:tenantCode={},announcementId={},data={}", tenantCode, announcementId);
        } finally {
            FeignHolder.clear();
        }
    }

    /**
     * 异步删除公告接收用户
     *
     * @param tenantCode 租户
     * @param announcementId 公告ID
     * @param headers
     */
    @Async
    public void asyncDeleteAnnouncementMember(String tenantCode, String announcementId, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeAnnouncementsReceiveService.deleteReceiveUser(announcementId);
        } catch (Exception e) {
            log.error("异步删除公告接收人异常:tenantCode={},announcementId={},data={}", tenantCode, announcementId);
        } finally {
            FeignHolder.clear();
        }
    }

    /**
     * 异步删除公告附件
     *
     * @param tenantCode 租户
     * @param announcementId 公告ID
     * @param headers
     */
    @Async
    public void asyncDeleteAnnouncementFiles(String tenantCode, String announcementId, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeFilesService.deleteFiles(announcementId);
        } catch (Exception e) {
            log.error("异步删除公告附件异常:tenantCode={},announcementId={},data={}", tenantCode, announcementId);
        } finally {
            FeignHolder.clear();
        }
    }


    /**
     * 异步记录通知公告操作日志
     *
     * @param tenantCode 租户
     * @param recordLog  操作日志
     * @param headers
     */
    @Async
    public void asyncRecordAnnouncementLogs(String tenantCode, FtbNoticeAnnouncementsLog recordLog, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeAnnouncementsLogService.recordLog(recordLog);
        } catch (Exception e) {
            log.error("异步添加通知公告日志失败:tenantCode={},data={}", tenantCode, JSONUtil.toJsonStr(recordLog));
        } finally {
            FeignHolder.clear();
        }
    }

    /**
     * 发送IM
     *
     * @param tenantCode 租户
     * @param userList 用户列表
     * @param dto 公告信息
     * @param headers
     */
    @Async
    public void asyncSendIm(String tenantCode, List<String> userList, FtbNoticeAnnouncementsDto dto, Map<String, String> headers) {
        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            noticeIMUtils.sendMsg(tenantCode, userList, dto);
        } catch (Exception e) {
            log.error("异步发送IM失败:tenantCode={},data={}", tenantCode, JSONUtil.toJsonStr(dto));
        } finally {
            FeignHolder.clear();
        }
    }

    /**
     * 异步删除分组中的人员
     *
     * @param tenantCode
     * @param groupId
     * @param headers
     */
    @Async
    public void asyncDeleteUserGroupMember(String tenantCode, String groupId, Map<String, String> headers) {

        try {
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            ftbNoticeUserGroupMembersService.batchDeleteGroupUser(groupId);
        } catch (Exception e) {
            log.error("异步删除用户分组成员异常:tenantCode={},groupId={}", tenantCode, groupId);
        } finally {
            FeignHolder.clear();
        }
    }
}
