package com.sv.service.api;

import com.enums.MemberAuthTypeEnum;
import com.github.pagehelper.PageHelper;
import com.sv.entity.MemberAuth;
import com.sv.mapper.MemberAuthMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service - 用户auth表
 *
 * @author lihong
 * @since 2018-08-02
 */
@Service
@Transactional(readOnly = true)
public class MemberAuthService extends BaseServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(MemberAuthService.class);

	@Resource
	private MemberAuthMapper memberAuthMapper;

	/**
	 * 创建用户auth表
	 *
	 * @param memberAuth 用户auth表
	 */
	@Transactional
    public void save(MemberAuth memberAuth) {
		if (memberAuth.getId() != null && memberAuth.getId() > 0) {
			// 更新
			memberAuthMapper.update(memberAuth);
		} else {
			// 新建
			memberAuthMapper.insert(memberAuth);
		}
	}

	/**
	 * 更新用户auth表
 	 *
	 * @param memberAuth 用户auth表
	 */
	@Transactional
    public void update(MemberAuth memberAuth) {
		memberAuthMapper.update(memberAuth);
	}

	/**
	 * 删除用户auth表
	 *
	 * @param id 编号
   	 * @return 删除数量
	 */
	@Transactional
    public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return memberAuthMapper.delete(id);
	}

	/**
	 * 删除用户auth表
	 *
	 * @param ids 编号数组
	 * @return 删除数量
	 */
	@Transactional
    public Integer deleteByIds(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return 0;
		}
		return memberAuthMapper.deleteByIds(ids);
	}

	/**
	 * 查询用户auth表
	 *
	 * @param id 编号
	 * @return 用户auth表
	 */
	public MemberAuth findById(Integer id) {
		return memberAuthMapper.findById(id);
	}

	/**
	 * 查询用户auth表
	 *
	 * @param pagination 分页信息
	 * @return 分页结果
	 */
	public Pagination findPage(Pagination pagination) {
		PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
		PageHelper.orderBy("id desc");
		pagination.setQueryResult(memberAuthMapper.findAll());
		return pagination;
	}

	/**
	 * 查询登录认证
	 */
	public MemberAuth findByMobile(String mobile) {
		return memberAuthMapper.findByMobile(mobile);
	}

	/**
	 * 创建登录认证
	 */
	@Transactional
	public MemberAuth createByMobile(Integer memberId,Integer platformId, String mobile) {
		MemberAuth memberAuth = new MemberAuth();
		memberAuth.setMemberId(memberId);
		memberAuth.setMobile(mobile);
		memberAuth.setType(MemberAuthTypeEnum.MOBILE.value);
		memberAuth.setAuthId("");
		memberAuth.setPlatformId(platformId);
		memberAuthMapper.insert(memberAuth);
		return memberAuth;
	}

    /**
	 * 微信登录，判断用户是否注册过
	 */
	@Transactional
	public MemberAuth findByAuthId(String openId){
		return memberAuthMapper.findByAuthId(openId);
	}

	/**
	 * 查询用户的登录方式有哪些
	 */
	@Transactional
	public MemberAuth checkLoginWay(Integer memberId,Integer type){
		return memberAuthMapper.checkLoginWay(memberId,type);
	}

	/**
	 * 创建登录认证，存在openId
	 */
	@Transactional
	public MemberAuth createByMobileByOpenId(Integer memberId,Integer platformId, String mobile, String openId) {
		MemberAuth memberAuth = new MemberAuth();
		memberAuth.setMemberId(memberId);
//		memberAuth.setMobile(mobile);
		memberAuth.setType(MemberAuthTypeEnum.WEIXIN.value);
		memberAuth.setAuthId(openId);
		memberAuth.setPlatformId(platformId);
		memberAuthMapper.insert(memberAuth);

		// 判断是否有用手机号登陆过
		MemberAuth auth = memberAuthMapper.findByType(memberId);
		if (auth == null){
			createByMobile( memberId, platformId,  mobile);
		}
		return memberAuth;
	}

	/**
	 * 手机号码是否存在
	 *
	 * @param mobile    手机号码
	 * @param excludeId 排除编号
	 * @return 存在返回 {@code true}，反之 {@code false}
	 */
	public Boolean isMobileExists(String mobile, Integer excludeId) {
		return memberAuthMapper.countByMobile(mobile, excludeId) != 0;
	}

	/**
	 * api 通话用户Id查找用户auth_id
	 */
	public MemberAuth findByMemberId(Integer memberId){
		return memberAuthMapper.findByMemberId(memberId);
	}

	/**
	 * 修改用户的手机号
	 */
	@Transactional
	public void updateMobile(String mobile,Integer memberId){
		memberAuthMapper.updateMobile( mobile,memberId);
	}
}

