package com.ssj.service.sys.schoolmatecircle.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.schoolmatecircle.domain.SSJUserFriendApply;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.dao.sys.schoolmatecircle.dao.SSJUserFriendApplyDao;
import com.ssj.dao.sys.schoolmatecircle.dao.SSJUserFriendQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.conch.student.dto.NewUserFriendsApplyDto;
import com.ssj.service.conch.student.dto.UserFriendsApplyDto;
import com.ssj.service.sys.easemob.service.EasemobService;
import com.ssj.service.sys.schoolmatecircle.service.SSJUserFriendApplyService;
import com.ssj.service.weixin.user.service.UserService;
@Service(version = "1.0")
@Transactional
public class SSJUserFriendApplyServiceImpl extends BaseServiceImpl<SSJUserFriendApply, String> implements SSJUserFriendApplyService {
    protected static final Logger logger = LoggerFactory.getLogger(SSJUserFriendApplyServiceImpl.class);

    @Autowired
    private SSJUserFriendApplyDao dao;
    
    @Autowired
    private SSJUserFriendQueryDao userFriendQueryDao;
    
    @Autowired
	private UserService userService;
    
    @Autowired
	private EasemobService easemobService;

    @Override
    public PagingAndSortingRepository<SSJUserFriendApply, String> getDao() {
        return dao;
    }

	@Override
	public void addFriendApply(String userId, String friendUserId) {
		SSJUserFriendApply apply = dao.findByOwnerUserIdAndFriendUserId(userId, friendUserId);
		if(apply == null) {
			apply = new SSJUserFriendApply();
			apply.setOwnerUserId(userId);
			apply.setFriendUserId(friendUserId);
			apply.setStatus(0);
			apply.setCreateTime(new Date());
			dao.save(apply);
		}
	}

	@Override
	public List<NewUserFriendsApplyDto> friendsApplyInfo(String userId) {
		
		//查询自己申请添加好友和好友申请添加自己的记录
		List<SSJUserFriendApply> applyList = dao.findByOwnerUserIdOrFriendUserId(userId, userId);
		List<NewUserFriendsApplyDto> dtoList = new ArrayList<NewUserFriendsApplyDto>();
		for (SSJUserFriendApply ssjUserFriendApply : applyList) {
			NewUserFriendsApplyDto dto = new NewUserFriendsApplyDto();
			if(ssjUserFriendApply.getOwnerUserId().equals(userId)) {
				//表示是自己添加好友的记录
				dto.setApplyStatus(1);
				User user = userService.getById(ssjUserFriendApply.getFriendUserId());
				dto.setFriendUserId(user.getId());
				dto.setFriendUserName(user.getUserName());
				dto.setImgUrl(user.getPhoto());
				if(user.getSex() != null) {
					dto.setSex(user.getSex().intValue());
				}
				dto.setEasemobName(user.getEasemobName());
				dto.setStatus(String.valueOf(ssjUserFriendApply.getStatus()));
			}else {
				//表示是好友添加自己的记录
				dto.setApplyStatus(2);
				User user = userService.getById(ssjUserFriendApply.getOwnerUserId());
				dto.setFriendUserId(user.getId());
				dto.setFriendUserName(user.getUserName());
				dto.setImgUrl(user.getPhoto());
				if(user.getSex() != null) {
					dto.setSex(user.getSex().intValue());
				}
				dto.setEasemobName(user.getEasemobName());
				dto.setStatus(String.valueOf(ssjUserFriendApply.getStatus()));
			}
			dtoList.add(dto);
		}
		return dtoList;
	}

	@Override
	public List<UserFriendsApplyDto> historyFriendsApply(String userId) {
		List<Map<String,Object>> list = userFriendQueryDao.historyFriendsApply(userId);
		List<UserFriendsApplyDto> dtoList = new ArrayList<UserFriendsApplyDto>();
		for (Map<String, Object> map : list) {
			UserFriendsApplyDto dto = new UserFriendsApplyDto();
			dto.setFriendUserId((String)map.get("id"));
			dto.setFriendUserName((String)map.get("user_name"));
			dto.setImgUrl((String)map.get("photo"));
			if(map.get("sex") != null) {
				dto.setSex(Integer.valueOf(map.get("sex").toString()));
			}
			dto.setEasemobName((String)map.get("easemob_name"));
			dto.setStatus(String.valueOf(map.get("status")));
			dtoList.add(dto);
		}
		return dtoList;
	}

	@Override
	public List<UserFriendsApplyDto> searchFriends(String userId,String friendUserName) {
		List<Map<String,Object>> list = userFriendQueryDao.searchFriends(userId,friendUserName);
		List<UserFriendsApplyDto> dtoList = new ArrayList<UserFriendsApplyDto>();
		for (Map<String, Object> map : list) {
			UserFriendsApplyDto dto = new UserFriendsApplyDto();
			dto.setFriendUserId((String)map.get("id"));
			dto.setFriendUserName((String)map.get("user_name"));
			dto.setImgUrl((String)map.get("photo"));
			if(map.get("sex") != null) {
				dto.setSex(Integer.valueOf(map.get("sex").toString()));
			}
			String easemobName = (String)map.get("easemob_name");
			//如果好友的环信id不存在，则创建环信账号
			if(StringUtils.isBlank(easemobName)) {
				User user = userService.getById((String)map.get("id"));
				easemobName = userService.createUniqueEasemobName();
				user.setEasemobName(easemobName);
				userService.save(user);
				//创建小孩环信账号
				easemobService.createUserSingle(user.getEasemobName(), user.getUserName());
			}
			dto.setEasemobName(easemobName);
			
			if(map.get("status") != null) {
				dto.setStatus(String.valueOf(map.get("status")));
			}
			dtoList.add(dto);
		}
		return dtoList;
	}
}

