package com.sv.api.controller;

import com.sv.api.context.PlatformContext;
import com.sv.entity.MemberBankCard;
import com.sv.service.api.MemberBankCardService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.controller.BaseApiController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Controller - 用户银行卡
 *
 * @author lihong
 * @since 2018-08-10
 */
@RestController
public class MemberBankCardController extends BaseApiController {

	private final Logger logger = LoggerFactory.getLogger(MemberBankCardController.class);
	
	@Resource
	private MemberBankCardService memberBankCardService;

	/**
	 * 分页查询用户银行卡
	 */
	@RequestMapping(value = "/member/bank/cards", method = RequestMethod.GET)
	public ResponseDTO findPage(Pagination pagination) {
		return ResponseDTO.ok()
				.setPagination(memberBankCardService.findPage(pagination));
	}

	/**
	 * 查询用户银行卡信息
	 */
	@RequestMapping(value = "/member/bank/card/{id}", method = RequestMethod.GET)
	public ResponseDTO findById(@PathVariable("id") Integer id) {
		return ResponseDTO.ok()
				.addAttribute("memberBankCard", memberBankCardService.findById(id));
	}

	/**
	 * 保存用户银行卡
	 */
	@RequestMapping(value = "/member/bank/card", method = RequestMethod.POST)
	public ResponseDTO save(MemberBankCard memberBankCard) {
		memberBankCardService.save(memberBankCard);
		return ResponseDTO.ok("保存成功");
	}

	/**
	 * 删除用户银行卡
	 */
	@RequestMapping(value = "/member/bank/card/delete", method = RequestMethod.POST)
	public ResponseDTO delete(@RequestParam("id[]") Integer[] ids) {
		memberBankCardService.deleteByIds(ids);
		return ResponseDTO.ok("删除成功");
	}

	/**
	 * 点击提现按钮
	 */
	@RequestMapping(value = "/member/bank/show", method = RequestMethod.POST)
	public ResponseDTO showBank(){
		return ResponseDTO.ok().addAttribute("bank",memberBankCardService.findByMemberId(getMemberIdByAccessToken()));
	}

	/**
	 * 绑定银行卡
	 */
	@RequestMapping(value = "/member/bank/bind", method = RequestMethod.POST)
	public ResponseDTO bind(@RequestParam("bankName") String bankName,@RequestParam("bankNo") String bankNo,@RequestParam("name")String name){
		return ResponseDTO.ok().
				addAttribute("bankId",memberBankCardService.bindBank(bankName,bankNo,name,getMemberIdByAccessToken(),PlatformContext.getValue()));
	}
}
