package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.ImGroupMemberAuditDao;
import com.yonge.cooleshow.biz.dal.entity.ImGroup;
import com.yonge.cooleshow.biz.dal.entity.ImGroupMemberAudit;
import com.yonge.cooleshow.biz.dal.enums.AuditStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.ImGroupMemberRoleType;
import com.yonge.cooleshow.biz.dal.enums.MessageTypeEnum;
import com.yonge.cooleshow.biz.dal.queryInfo.ImGroupMemberAuditQueryInfo;
import com.yonge.cooleshow.biz.dal.service.ImGroupMemberAuditService;
import com.yonge.cooleshow.biz.dal.service.ImGroupMemberService;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.vo.AuditUserInfo;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;
import io.rong.models.group.GroupMember;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (ImGroupMemberAudit)表服务实现类
 *
 * @author zx
 * @since 2022-03-22 17:18:51
 */
@Service("imGroupMemberAuditService")
public class ImGroupMemberAuditServiceImpl extends ServiceImpl<ImGroupMemberAuditDao, ImGroupMemberAudit> implements ImGroupMemberAuditService {

    private final static Logger log = LoggerFactory.getLogger(ImGroupMemberAuditServiceImpl.class);

    @Resource
    private ImGroupService imGroupService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private ImGroupMemberService imGroupMemberService;
    @Resource
    private SysMessageServiceImpl sysMessageService;

    @Override
    public ImGroupMemberAuditDao getDao() {
        return this.baseMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void apply(ImGroupMemberAudit imGroupMemberAudit) throws Exception {
        String groupId = imGroupMemberAudit.getGroupId();
        ImGroup imGroup = imGroupService.getById(groupId);
        if(Objects.isNull(imGroup)){
            throw new BizException("群组信息不存在");
        }
        //是否在群
        Map<String,Object> groupMemberParams = new HashMap<>(2);
        groupMemberParams.put("groupId",groupId);
        groupMemberParams.put("userId",imGroupMemberAudit.getUserId());
        if(Objects.nonNull(imGroupMemberService.getDao().findOne(groupMemberParams))){
            throw new BizException("您已经在群聊中，请勿重复添加");
        }
        //是否有待审核的申请
        Map<String,Object> auditParams = new HashMap<>(3);
        auditParams.put("groupId",groupId);
        auditParams.put("userId",imGroupMemberAudit.getUserId());
        auditParams.put("auditStatus",AuditStatusEnum.AUDITING.getCode());
        if(Objects.nonNull(baseMapper.findOne(auditParams))){
            throw new BizException("您有待审核的申请，请勿重复提交");
        }
        Date date = new Date();
        //是否自动通过审核
        imGroupMemberAudit.setAuditStatus(imGroup.getAutoPassFlag()?AuditStatusEnum.OPEN:AuditStatusEnum.AUDITING);
        imGroupMemberAudit.setUpdateTime(date);
        imGroupMemberAudit.setCreateTime(date);
        baseMapper.insert(imGroupMemberAudit);
        if(imGroup.getAutoPassFlag()){
            //处理本地群成员
            List<GroupMember> groupMembers = imGroupMemberService.initGroupMember(imGroup.getId(),
                    imGroupMemberAudit.getUserId(), false,
                    imGroupMemberAudit.getRoleType());
            //同步群成员数量
            imGroupService.getDao().updateMemberNum(imGroup.getId());
            //加入融云群
            imGroupMemberService.join(groupMembers,groupId);
        } else {
            Map<Long,String> receivers = new HashMap<>(1);
            // 群创建人
            SysUser user = sysUserService.findUserById(imGroup.getCreateBy());
            // 申请人
            SysUser auditUser = sysUserService.findUserById(imGroupMemberAudit.getUserId());

            receivers.put(user.getId(), user.getPhone());
            try {
                sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.STUDENT_JOIN_FANSGROUP,
                           receivers, null, 0, null, ClientEnum.TEACHER.getCode(), auditUser.getUsername(),imGroup.getName());
            } catch (Exception e) {
                log.warn("学生入群消息发送失败，{}",e.getMessage());
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyAudit(String auditIds, AuditStatusEnum auditStatus,String groupId) throws Exception {
        Optional.of(auditStatus).filter(e->e != AuditStatusEnum.AUDITING).orElseThrow(()->new BizException("操作失败：审核状态异常"));
        ImGroup imGroup = Optional.ofNullable(imGroupService.getById(groupId)).orElseThrow(() ->new BizException("群组信息不存在"));
        Optional.of(sysUserService.getUserId()).filter(imGroup.getCreateBy()::equals).orElseThrow(()->new BizException("操作失败：您没有审核权限"));
        //修改审核状态
        baseMapper.batchUpdateAuditStatus(auditIds,auditStatus.getCode());
        if(auditStatus == AuditStatusEnum.OPEN){
            List<ImGroupMemberAudit> imGroupMemberAudit = baseMapper.findByIds(auditIds);
            Set<Long> userIds = imGroupMemberAudit.stream().map(ImGroupMemberAudit::getUserId).collect(Collectors.toSet());
            List<GroupMember> groupMembers = imGroupMemberService.initGroupMembers(groupId,userIds, ImGroupMemberRoleType.STUDENT);
            //同步群成员数量
            imGroupService.getDao().updateMemberNum(imGroup.getId());
            //加入融云群
            imGroupMemberService.join(groupMembers,imGroup.getId());
            sendMessage(auditIds);
        }
    }

    /**
     * 发送通过消息
     *
     * @param auditIds
     */
    private void sendMessage(String auditIds) {
        List<AuditUserInfo> auditUserInfoList = baseMapper.selectAuditUserInfo(auditIds);
        if (CollectionUtils.isEmpty(auditUserInfoList)) {
            return;
        }
        auditUserInfoList.stream().forEach(this::sendMessage);
    }

    private void sendMessage(AuditUserInfo auditUserInfo){
        Map<Long,String> receivers = new HashMap<>(1);
        receivers.put(auditUserInfo.getAuditUserId(), auditUserInfo.getAuditUserPhone());
        try {
            sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.FANSGROUP_APPLY_SUCCESS,
                    receivers, null, 0, null, ClientEnum.STUDENT.getCode(), auditUserInfo.getGroupUsername(),
                    auditUserInfo.getGroupName());
        } catch (Exception e) {
            log.warn("学生入群申请通过消息发送失败，{}",e.getMessage());
        }
    }

    @Override
    public List<ImGroupMemberAudit> queryAll(ImGroupMemberAuditQueryInfo queryInfo) {
        return baseMapper.queryAll(queryInfo);
    }
}

