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

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

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;

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

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean 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 true;
		}

		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 true;
	}

	@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);
	}

}