package com.sv.service.api;

import com.github.pagehelper.PageHelper;
import com.sv.entity.MemberBankCard;
import com.sv.entity.MemberWithdrawApply;
import com.sv.mapper.MemberWithdrawApplyMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import org.omg.PortableInterceptor.INACTIVE;
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.math.BigDecimal;

/**
 * Service - 用户提现申请
 *
 * @author lihong
 * @since 2018-08-10
 */
@Service("MemberWithdrawApplyServiceApi")
@Transactional(readOnly = true)
public class MemberWithdrawApplyService extends BaseServiceImpl{

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

	@Resource
	private MemberWithdrawApplyMapper memberWithdrawApplyMapper;
	@Resource
	private MemberBankCardService memberBankCardService;

	/**
	 * 创建用户提现申请
	 *
	 * @param memberWithdrawApply 用户提现申请
	 */
	@Transactional
    public void save(MemberWithdrawApply memberWithdrawApply) {
		if (memberWithdrawApply.getId() != null && memberWithdrawApply.getId() > 0) {
			// 更新
			memberWithdrawApplyMapper.update(memberWithdrawApply);
		} else {
			// 新建
			memberWithdrawApplyMapper.insert(memberWithdrawApply);
		}
	}

	/**
	 * 更新用户提现申请
 	 *
	 * @param memberWithdrawApply 用户提现申请
	 */
	@Transactional
    public void update(MemberWithdrawApply memberWithdrawApply) {
		memberWithdrawApplyMapper.update(memberWithdrawApply);
	}

	/**
	 * 删除用户提现申请
	 *
	 * @param id 编号
   	 * @return 删除数量
	 */
	@Transactional
    public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return memberWithdrawApplyMapper.delete(id);
	}

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

	/**
	 * 查询用户提现申请
	 *
	 * @param id 编号
	 * @return 用户提现申请
	 */
	public MemberWithdrawApply findById(Integer id) {
		return memberWithdrawApplyMapper.findById(id);
	}

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

	/**
	 * 生成提现申请记录
	 */
	@Transactional(readOnly = false)
	public void create(Integer memberId,Integer bankId,BigDecimal money,Integer platformId){
		// 1.查询银行卡
		MemberBankCard memberBankCard = memberBankCardService.findById(bankId);
		MemberWithdrawApply apply = new MemberWithdrawApply();
		apply.setMemberId(memberId);
		apply.setBankName(memberBankCard.getBankName());
		apply.setBankNo(memberBankCard.getBankNo());
		apply.setName(memberBankCard.getName());
		apply.setMoney(money);
		memberWithdrawApplyMapper.insert(apply);
	}
}

