package com.sv.service.api;

import com.github.pagehelper.PageHelper;
import com.sv.entity.Member;
import com.sv.entity.MemberFaceInfo;
import com.sv.mapper.MemberFaceInfoMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.exception.ExceptionCodeTemplate;
import com.ydd.framework.core.exception.ServiceException;
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 - 用户人脸识别表
 *
 * @author lihong
 * @since 2018-08-10
 */
@Service
@Transactional(readOnly = true)
public class MemberFaceInfoService extends BaseServiceImpl {

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

	@Resource
	private MemberFaceInfoMapper memberFaceInfoMapper;
	@Resource
	private MemberService memberService;

	/**
	 * 创建用户人脸识别表
	 *
	 * @param memberFaceInfo 用户人脸识别表
	 */
	@Transactional
    public void save(MemberFaceInfo memberFaceInfo) {
		if (memberFaceInfo.getId() != null && memberFaceInfo.getId() > 0) {
			// 更新
			memberFaceInfoMapper.update(memberFaceInfo);
		} else {
			// 新建
			memberFaceInfoMapper.insert(memberFaceInfo);
		}
	}

	/**
	 * 更新用户人脸识别表
 	 *
	 * @param memberFaceInfo 用户人脸识别表
	 */
	@Transactional
    public void update(MemberFaceInfo memberFaceInfo) {
		memberFaceInfoMapper.update(memberFaceInfo);
	}

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

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

	/**
	 * 查询用户人脸识别表
	 *
	 * @param id 编号
	 * @return 用户人脸识别表
	 */
	public MemberFaceInfo findById(Integer id) {
		return memberFaceInfoMapper.findById(id);
	}

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

	/**
	 * api 根据用户ID查找‘用户人脸识别表’
	 */
	public MemberFaceInfo findByMemberId(Integer memberId){
		MemberFaceInfo memberFaceInfo = memberFaceInfoMapper.findByMemberId(memberId);
		return memberFaceInfo;
	}

	/**
	 * 查询用户上次记录
	 */
	public MemberFaceInfo findByMember(Integer memberId){
		// 判断用户信息
		if (memberId == null){
			throw new ServiceException(ExceptionCodeTemplate.NEED_LOGIN);
		}
		Member member = memberService.findById(memberId);
		memberService.verify(member);
		MemberFaceInfo memberFaceInfo = memberFaceInfoMapper.findByMemberId(memberId);
		return memberFaceInfo;
	}

}

