package com.wechat.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wechat.dao.MdeFriendRelationDao;
import com.wechat.model.dbEntity.MdeFriendRelation;

@Service
public class MdeFriendRelationService {

	@Resource
	private MdeFriendRelationDao friendRelationDao;

	/**
	 * 添加好友
	 * 
	 * @param userCode1
	 *            申请人
	 * @param userCode2
	 *            接受人
	 * @return
	 */
	public int makeFriends(String userCode1, String userCode2) {
		MdeFriendRelation friendRelation = new MdeFriendRelation();
		friendRelation.setUserId1(userCode1);
		friendRelation.setUserId2(userCode2);
		friendRelation.setToBeConfirmed(userCode2 + "");
		friendRelation.setDelFlag(0);
		friendRelation.setVersionNo(1);
		Date d = new Date();
		friendRelation.setCreateDate(d);
		friendRelation.setUpdateDate(d);
		friendRelation.setCreateBy(friendRelation.getUserId1() + "");
		friendRelation.setUpdateBy(friendRelation.getUserId1() + "");
		return friendRelationDao.insert(friendRelation);
	}

	/**
	 * 好友关系确认
	 * 
	 * @param userCode1
	 *            申请人
	 * @param userCode2
	 *            当前用户
	 * @return
	 */
	public int update(MdeFriendRelation friendRelation) {
		return friendRelationDao.update(friendRelation);
	}
	
	public int deleteRecords(MdeFriendRelation friendRelation) {
		return friendRelationDao.deleteRecords(friendRelation);
	}

	/**
	 * 根据Code查找好友申请记录
	 * 
	 * @param userCode1 申请人
	 * @param userCode2 当前用户
	 * @return
	 */
	public MdeFriendRelation findRequestRecordByCodes(String userCode1, String userCode2) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId1", userCode1);
		map.put("userId2", userCode2);
		return friendRelationDao.findRequestRecordByCodes(map);
	}
	/**
	 * 删除好友
	 * @param userCode
	 * @param userCode1
	 * @return
	 */
	public int delFriends(String userCode, String userCode1) {
		MdeFriendRelation relation = new MdeFriendRelation();
		relation.setUserId1(userCode);
		relation.setUserId2(userCode1);
		return friendRelationDao.delFriends(relation);
	}

	public MdeFriendRelation getInfoByCode(String userCode1, String userCode2) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId1", userCode1);
		map.put("userId2", userCode2);
		return friendRelationDao.getInfoByCode(map);
	}

}
