package com.ym.mec.biz.service.impl;

import java.util.Date;
import java.util.List;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.common.entity.ImUserModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ym.mec.biz.dal.dao.ImGroupDao;
import com.ym.mec.biz.dal.dao.ImGroupMemberDao;
import com.ym.mec.biz.dal.dao.ImGroupNoticeDao;
import com.ym.mec.biz.dal.dto.ImGroupMemberDto;
import com.ym.mec.biz.dal.entity.ImGroup;
import com.ym.mec.biz.service.ImGroupMemberService;
import com.ym.mec.biz.service.ImGroupService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.im.ImFeignService;
import com.ym.mec.im.entity.GroupMember;
import com.ym.mec.im.entity.GroupModel;

@Service
public class ImGroupServiceImpl extends BaseServiceImpl<Long, ImGroup> implements ImGroupService {

	@Autowired
	private ImGroupDao imGroupDao;

	@Autowired
	private ImGroupMemberDao imGroupMemberDao;

	@Autowired
	private ImGroupNoticeDao imGroupNoticeDao;

	@Autowired
	private ImGroupMemberService imGroupMemberService;

	@Autowired
	private ImFeignService imFeignService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Long, ImGroup> getDAO() {
		return imGroupDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public ImGroup create(Long id, Integer userId, String name, String introduce, String memo, String tags, String img, String type) {
		ImGroup imGroup = imGroupDao.get(id);
		if (imGroup != null) {
			return imGroup;
		}

		imGroup = new ImGroup();

		Date date = new Date();

		imGroup.setId(id);
		imGroup.setCreateTime(date);
		imGroup.setIntroduce(introduce);
		
		if(userId != null){
			imGroup.setMemberNum(1);
		}else{
			imGroup.setMemberNum(0);
		}
		imGroup.setMemo(memo);
		imGroup.setName(name);
		imGroup.setTags(tags);
		imGroup.setImg(img);
		imGroup.setType(type);
		imGroup.setUpdateTime(date);
		imGroupDao.insert(imGroup);

		String groupId = imGroup.getId().toString();
		
		if(userId != null){
			imGroupMemberService.join(imGroup.getId(), userId, null, true);

			GroupMember groupMember = new GroupMember(userId.toString(), groupId);

			imFeignService.groupCreate(new GroupModel(groupId, new GroupMember[] { groupMember }, name));
		}else{
			imFeignService.groupCreate(new GroupModel(groupId, null, name));
		}

		return imGroup;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean cancel(Long imGroupId) {

		// 删除群成员
		imGroupMemberDao.deleteByImGroupId(imGroupId);
		// 删除群公告
		imGroupNoticeDao.deleteByImGroupId(imGroupId);
		// 删除群信息
		imGroupDao.delete(imGroupId);

		String groupId = imGroupId.toString();
		imFeignService.groupDismiss(new GroupModel(groupId, null));

		return true;
	}

	@Override
	public List<ImGroup> queryByUserId(Integer userId, String search) {
		return imGroupDao.queryByUserId(userId, search);
	}

	@Override
	public List<ImGroupMemberDto> queryMemberById(Long imGroupId) {
		return imGroupDao.queryMemberById(imGroupId);
	}

	@Override
	public ImGroupMemberDto queryMember(Long imGroupId, Integer userId) {
		return imGroupDao.queryMember(imGroupId, userId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateNickName(Integer userId, String nickName) {
		//修改群成员备注
		int i = imGroupDao.updateNickname(userId, nickName);
		imGroupDao.updateUserFriendNickname(userId, nickName);
		return i;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateNickName1(Integer userId, String nickName) {
		//修改群成员备注
		imGroupDao.updateNickname(userId, nickName);
		imGroupDao.updateUserFriendNickname(userId, nickName);
		//修改sysUser名称，如果包含学员的角色，那么修改userName。否则修改realName
		SysUser sysUser = sysUserFeignService.queryUserById(userId);
		if(sysUser.getUserType().contains("STUDENT")){
			sysUser.setUsername(nickName);
		}else {
			sysUser.setRealName(nickName);
		}
		sysUserFeignService.updateSysUser(sysUser);
		//同步融云基本信息
		imFeignService.update(new ImUserModel(sysUser.getId().toString(),nickName,sysUser.getAvatar()));
	}

}