package com.buy507.mall.portal.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.mapper.DmsMemberBankAccountMapper;
import com.buy507.mall.model.DefaultStatus;
import com.buy507.mall.model.DmsMemberBankAccount;
import com.buy507.mall.model.UmsMember;
import com.buy507.mall.portal.domain.MemberBankAccount;
import com.buy507.mall.portal.service.MemberBankAccountService;
import com.buy507.mall.portal.service.UmsMemberService;

@Service
public class MemberBankAccountServiceImpl implements MemberBankAccountService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MemberBankAccountServiceImpl.class);
	
	@Autowired
    private UmsMemberService memberService;
	
	@Autowired
	private DmsMemberBankAccountMapper memberBankAccountMapper;

	@Override
	public CommonResult<String> add(MemberBankAccount bankAccount) {
		UmsMember currentMember = memberService.getCurrentMember();
		LOGGER.debug("Add member bank account, memberId: {}", currentMember.getId());
		
		String message = varifyParam(bankAccount);
		if(!StringUtils.isEmpty(message)) {
			return CommonResult.failed(message);
		}
		
		List<DmsMemberBankAccount> list = memberBankAccountMapper.selectByMemberId(currentMember.getId());
		if(list.size() >= 5) {
			return CommonResult.failed("最多只能添加5条银行信息");
		}
		
		DmsMemberBankAccount dbBankAccount = new DmsMemberBankAccount();
		BeanUtils.copyProperties(bankAccount, dbBankAccount);
		
		Date date = new Date();
		dbBankAccount.setId(null);
		dbBankAccount.setMemberId(currentMember.getId());
		dbBankAccount.setCreateTime(date);
		
		memberBankAccountMapper.insert(dbBankAccount);
		
		if(DefaultStatus.Yes.equals(bankAccount.getDefaultStatus())) {
			for(DmsMemberBankAccount account : list) {
				if(DefaultStatus.Yes.equals(account.getDefaultStatus())) {
					account.setDefaultStatus(DefaultStatus.No);
					memberBankAccountMapper.updateById(account);
				}
			}
		}
		
		return CommonResult.success(null, "添加成功");
	}

	@Override
	public CommonResult<String> delete(Long id) {
		UmsMember currentMember = memberService.getCurrentMember();
		DmsMemberBankAccount bankAccount = memberBankAccountMapper.selectOneById(id);
		if(bankAccount != null && currentMember.getId().equals(bankAccount.getMemberId())) {
			memberBankAccountMapper.deleteById(bankAccount.getId());
		}
		return CommonResult.success(null, "删除成功");
	}

	@Override
	public CommonResult<String> update(MemberBankAccount bankAccount) {
		
		if(bankAccount.getId() == null) {
			return CommonResult.failed("ID不能为空");
		}
		
		String message = varifyParam(bankAccount);
		if(!StringUtils.isEmpty(message)) {
			return CommonResult.failed(message);
		}
		
		UmsMember currentMember = memberService.getCurrentMember();
		DmsMemberBankAccount dbBankAccount = memberBankAccountMapper.selectOneById(bankAccount.getId());
		if(dbBankAccount != null && currentMember.getId().equals(dbBankAccount.getMemberId())) {
			
			if(DefaultStatus.Yes.equals(bankAccount.getDefaultStatus())) {
				DmsMemberBankAccount defaultBankAccount = memberBankAccountMapper.selectOneByMemberIdAndDefaultStatus(currentMember.getId(), DefaultStatus.Yes.getValue());
				if(defaultBankAccount != null) {
					defaultBankAccount.setDefaultStatus(DefaultStatus.No);
					memberBankAccountMapper.updateById(defaultBankAccount);
				}
			}
			
			BeanUtils.copyProperties(bankAccount, dbBankAccount);
			dbBankAccount.setUpdateTime(new Date());
			memberBankAccountMapper.updateById(dbBankAccount);
		}
		
		return CommonResult.success(null, "更新成功");
	}

	@Override
	public CommonResult<MemberBankAccount> get(Long id) {
		UmsMember currentMember = memberService.getCurrentMember();
		DmsMemberBankAccount dbBankAccount = memberBankAccountMapper.selectOneById(id);
		if(dbBankAccount != null && currentMember.getId().equals(dbBankAccount.getMemberId())) {
			MemberBankAccount bankAccount = new MemberBankAccount();
			BeanUtils.copyProperties(dbBankAccount, bankAccount);
			return CommonResult.success(bankAccount);
		}
		return CommonResult.success(null);
	}
	
	@Override
	public CommonResult<MemberBankAccount> getDefaultBankAccount() {
		UmsMember currentMember = memberService.getCurrentMember();
		DmsMemberBankAccount defaultBankAccount = memberBankAccountMapper.selectOneByMemberIdAndDefaultStatus(currentMember.getId(), DefaultStatus.Yes.getValue());
		MemberBankAccount bankAccount = null;
		
		if(defaultBankAccount == null) {
			List<DmsMemberBankAccount> list = memberBankAccountMapper.selectByMemberId(currentMember.getId());
			if(list != null && !list.isEmpty()) {
				defaultBankAccount = list.get(0);
			}
		}
		
		if(defaultBankAccount != null) {
			bankAccount = new MemberBankAccount();
			BeanUtils.copyProperties(defaultBankAccount, bankAccount);
		}
		return CommonResult.success(bankAccount);
	}

	@Override
	public CommonResult<List<MemberBankAccount>> list() {
		UmsMember currentMember = memberService.getCurrentMember();
		List<DmsMemberBankAccount> list = memberBankAccountMapper.selectByMemberId(currentMember.getId());
		List<MemberBankAccount> result = new ArrayList<>();
		MemberBankAccount bankAccount = null;
		for(DmsMemberBankAccount account : list) {
			bankAccount = new MemberBankAccount();
			BeanUtils.copyProperties(account, bankAccount);
			result.add(bankAccount);
		}
		return CommonResult.success(result);
	}
	
	private String varifyParam(MemberBankAccount bankAccount) {
		String message = "";
		if(StringUtils.isEmpty(bankAccount.getBankName())) {
			return "银行名称不能为空";
		}
		if(StringUtils.isEmpty(bankAccount.getAccountName())) {
			return "账户名不能为空";
		}
		if(StringUtils.isEmpty(bankAccount.getBankCardNum())) {
			return "银行卡号不能为空";
		}
		if(StringUtils.isEmpty(bankAccount.getOpenBankAddress())) {
			return "开户行地址不能为空";
		}
		if(bankAccount.getDefaultStatus() == null) {
			return "是否默认不能为空";
		}
		return message;
	}
	
}
