package com.wechat.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wechat.common.Constants;
import com.wechat.common.utils.StringsUtils;
import com.wechat.dao.MdeEducationExperienceDao;
import com.wechat.dao.MdeUserCvDao;
import com.wechat.dao.MdeUserDao;
import com.wechat.global.CustomException;
import com.wechat.global.base.dao.Pagination;
import com.wechat.global.base.dao.PagingResult;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeEducationExperience;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.dbEntity.MdeUserCv;
import com.wechat.model.dto.UserAndIsFriendDto;
import com.wechat.model.responseDto.ResultEntity;

@Service
public class MdeUserService {

	@Autowired
	private MdeUserDao mdeUserDao;
	@Autowired
	private CommonService commonService;
	
	@Autowired
	MdeEducationExperienceDao educationExperienceDao;
	
	@Autowired
	MdeUserCvDao userCVDao;


	// 根据关键词查询用户
	public PagingResult<MdeUser> findByKeyWord(Pagination pagination) {
		return mdeUserDao.findByKeyWord(pagination);
	}

	// 根据用户userCode查询用户
	public MdeUser getUserInfoByCode(String userCode) {
		return mdeUserDao.getUserInfoByCode(userCode);
	}
	
	// 根据用户userCode查询用户
	public MdeUser getFullInfo(String userCode) {
		return mdeUserDao.getFullInfo(userCode);
	}

	//查找好友
	public PagingResult<MdeUser> getFriends(Pagination page){
		PagingResult<MdeUser> pageReault = mdeUserDao.getFriends(page);
		pageReault.setResultList(commonService.addDomainUrl2UserImg(pageReault.getResultList()));
		return pageReault;
	}

	// 用戶注冊
	public int insertUserInfo(MdeUser mdeUser) {
		//先解绑该用户
		mdeUserDao.unbind(mdeUser);
		return mdeUserDao.insertWithUserCode(mdeUser);
	}

	// 领英注册
	public int linkedInRegister(MdeUser mdeUser) {
		return mdeUserDao.linkedInRegister(mdeUser);
	}

	// 微信注册
	public int insertWetchRegister(MdeUser mdeUser) {
		return mdeUserDao.insertWetchRegister(mdeUser);
	}

	// 根据登陆名查询用户
	public MdeUser findByUserName(String userName) {
		return mdeUserDao.findByUserName(userName);
	}

	// 判断用户是否注册
	public MdeUser checkUserNameIsRegester(String userName) {
		return findByUserName(userName);

	}

	// 修改密码
	public int changePassword(MdeUser mdeUser) {
		return mdeUserDao.changePassword(mdeUser);
	}

	public int update(MdeUser mdeUser) {
		return mdeUserDao.update(mdeUser);
	}
	
	// 先解绑第三方信息再绑定
    public int bindInfo(MdeUser mdeUser) {
        mdeUserDao.unbind(mdeUser);
        return mdeUserDao.updateBind(mdeUser);
    }
    
	// 先解绑第三方信息
    public int unbind(MdeUser mdeUser) {
    	return mdeUserDao.unbind(mdeUser);
    }

	public int updateUser(MdeUser mdeUser) {
		return mdeUserDao.update(mdeUser);
	}

	// 根据unionId查询用户
	public MdeUser findByUnionId(String unionId) {
		return mdeUserDao.findByUnionId(unionId);
	}


	// 根据领英id 查询用户
	public MdeUser findByLinkedId(String linkedId) {
		return mdeUserDao.findByLinkedId(linkedId);
	}

	public int updateLikeOrShareList(MdeUser user) {
		return mdeUserDao.updateLikeOrShareList(user);
	}

	public int updateUserPicture(MdeUser user) {
		return mdeUserDao.updateUserPicture(user);
	}
	
	/** 
	 * 根据用户userCode查询用户,并获取查询人和被查询人是否好友
	 * @param beViewed 被查询人
	 * @param theSearch 查询人
	 */
	public UserAndIsFriendDto getUserAndIsFriend(String beViewed,String theSearch,String lang) {
		return mdeUserDao.getUserAndIsFriend(beViewed,theSearch, lang);
	}
	
	
	
	/**
	 *  用户信息完整度验证
	 * @param userCode
	 * @return
	 */
	public ResultEntity<Boolean> checkUserInfo(String userCode) {
		ResultEntity<Boolean> rest = new ResultEntity<Boolean>();
		boolean checkRseult = true;
		InfoMsg msg = InfoMsg.SUCCESS_REQUEST;

		MdeUser user = commonService.getUserInfo(userCode);
		
		if(StringsUtils.isEmpty(user.getAddrProvince()))
		{
			//意向工作地为空
			checkRseult = false;
			msg=InfoMsg.ERROR_USERINFO_PREFERRED;
		}
		if(checkRseult && StringsUtils.isEmpty(user.getBirthday()))
		{
			//生日为空
			checkRseult = false;
			msg=InfoMsg.ERROR_USERINFO_BIRTHDAY;
		}
		
		if(checkRseult)
		{
			List<MdeEducationExperience> educationExperience = educationExperienceDao.getEducationExperienceByUserCode(userCode,Constants.LANG_CN);
			if(educationExperience.size() < 1)
			{
				//教育经历为空
				checkRseult = false;
				msg=InfoMsg.ERROR_USERINFO_EDUCATION;
			} else {
				List<MdeUserCv> userCV = userCVDao.getUserCVList(userCode);
				if(userCV.size() < 1)
				{
					//简历附件未上传
					checkRseult = false;
					msg=InfoMsg.ERROR_USERINFO_CVFILES;
				}
			}
		}

		rest.setMsg(msg);
		rest.setResult(checkRseult);
		return rest;
	}
	
	
	/**
	 * 修改用户隐私标识
	 * 
	 * @return
	 */
	@Transactional
	public int updatePrivateFlag(String userCode,String privateFlag) {
		MdeUser dto = new MdeUser();
		dto.setPrivateFlag(privateFlag);
		dto.setUserCode(userCode);
		return mdeUserDao.updatePrivateFlag(dto);
	}
	
	
	
	/**
	 * 修改订阅标识
	 * 
	 * @param modifyInterestedAdds
	 * @return
	 */
	@Transactional
	public InfoMsg modifySubscribedFlag(String userCode,String subdFlag) {
		InfoMsg msg = InfoMsg.SUCCESS_REQUEST;
		if(Constants.SUBSCRIBED_YES.equals(subdFlag)) 
		{
			//订阅时验证用户是否关注公众号
			msg = commonService.checkBindingAndSubscribe(userCode);
		} 
		
		if(InfoMsg.SUCCESS_REQUEST.getCode().equals(msg.getCode()))
		{
			//验证通过的情况下 修改职位订阅标识
			int i =  mdeUserDao.modifySubscribedFlag(userCode,subdFlag);
			//数据存储0件的情况下，回滚操作
			if(i<1) 
			{
				msg = InfoMsg.ERROR_DATA_UPDATE;
			}
		}

		return msg;
	}

}
