package com.bizzan.bitrade.service;

import java.util.List;

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

import com.bizzan.bitrade.dao.MemberWalletAddressBackupDao;
import com.bizzan.bitrade.entity.MemberWalletAddressBackup;
import com.bizzan.bitrade.service.Base.BaseService;

@Service
public class MemberWalletAddressBackupService extends BaseService {
	@Autowired
    private MemberWalletAddressBackupDao memberWalletAddressBackupDao;
	
	public void saveMemberWalletAddressBackup(Long memberId, String address, String unit) {
		MemberWalletAddressBackup existingMemberWalletAddressBackup = memberWalletAddressBackupDao.findByCoinIdAndMemberId(unit, memberId);
		if(existingMemberWalletAddressBackup == null) {
			MemberWalletAddressBackup memberWalletAddressBackup = new MemberWalletAddressBackup();
			memberWalletAddressBackup.setMemberId(memberId);
			memberWalletAddressBackup.setAddress(address);
			memberWalletAddressBackup.setCoinId(unit);
			memberWalletAddressBackupDao.saveAndFlush(memberWalletAddressBackup);
		} else {
			existingMemberWalletAddressBackup.setAddress(address);
			memberWalletAddressBackupDao.saveAndFlush(existingMemberWalletAddressBackup);
		}
		
	}
	
	public MemberWalletAddressBackup findByCoinUnitAndMemberId(String unit, Long memberId) {
		return memberWalletAddressBackupDao.findByCoinIdAndMemberId(unit, memberId);
	}
	
	public List<MemberWalletAddressBackup> findAllByMemberId(Long memberId) {
		return memberWalletAddressBackupDao.findAllByMemberId(memberId);
	}
}
