package com.sv.service.api;

import com.github.pagehelper.PageHelper;
import com.sv.entity.Member;
import com.sv.entity.MemberBankCard;
import com.sv.mapper.MemberBankCardMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.utils.ValidationUtils;
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;
import java.util.List;

/**
 * Service - 用户银行卡
 *
 * @author lihong
 * @since 2018-08-10
 */
@Service
@Transactional
public class MemberBankCardService extends BaseServiceImpl {

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

	@Resource
	private MemberBankCardMapper memberBankCardMapper;
	@Resource
	private MemberService memberService;

	/**
	 * 创建用户银行卡
	 *
	 * @param memberBankCard 用户银行卡
	 */
	@Transactional
    public void save(MemberBankCard memberBankCard) {
		if (memberBankCard.getId() != null && memberBankCard.getId() > 0) {
			// 更新
			memberBankCardMapper.update(memberBankCard);
		} else {
			// 新建
			memberBankCardMapper.insert(memberBankCard);
		}
	}

	/**
	 * 更新用户银行卡
 	 *
	 * @param memberBankCard 用户银行卡
	 */
	@Transactional
    public void update(MemberBankCard memberBankCard) {
		memberBankCardMapper.update(memberBankCard);
	}

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

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

	/**
	 * 查询用户银行卡
	 *
	 * @param id 编号
	 * @return 用户银行卡
	 */
	public MemberBankCard findById(Integer id) {
		return memberBankCardMapper.findById(id);
	}

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

	/**
	 * 根据用户id查找用户银行卡
	 */
	public List<MemberBankCard> findByMemberId(Integer memberId){
		// 判断用户是否登录
		if (memberId == null){
			throw new ServiceException(com.ydd.framework.core.exception.ExceptionCodeTemplate.NEED_LOGIN);
		}
		Member member = memberService.findById(memberId);
		memberService.verify(member);
		List<MemberBankCard> memberBankCards = memberBankCardMapper.findByMemberId(memberId);
		if (memberBankCards.size() == 0){
			throw new ServiceException("您还未绑定银行卡");
		}
		return memberBankCards;
	}

	/**
	 * 绑定银行卡
	 */
	public Integer bindBank(String bankName,String bankNo,String name,Integer memberId,Integer platformId){
		// 判断用户是否登录
		if (memberId == null){
			throw new ServiceException(com.ydd.framework.core.exception.ExceptionCodeTemplate.NEED_LOGIN);
		}
		Member member = memberService.findById(memberId);
		memberService.verify(member);

		//参数校验
		ValidationUtils.assertNotNull(bankName);
		ValidationUtils.assertNotNull(bankNo);
		ValidationUtils.assertNotNull(name);

		//创建记录
		MemberBankCard memberBankCard = new MemberBankCard();
		memberBankCard.setBankName(bankName);
		memberBankCard.setBankNo(bankNo);
		memberBankCard.setName(name);
		memberBankCard.setMemberId(memberId);
		memberBankCard.setPlatformId(platformId);
		memberBankCardMapper.insert(memberBankCard);

		return memberBankCard.getId();
	}
}

