package com.sv.api.controller;

import com.sv.entity.MemberCard;
import com.sv.service.api.MemberCardService;
import com.ydd.framework.core.annotation.AccessToken;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.controller.BaseApiController;
import com.ydd.framework.core.entity.enums.StatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Controller - 用户会员卡
 *
 * @author lihong
 * @since 2018-08-03
 */
@RestController
public class MemberCardController extends BaseApiController {

	private final Logger logger = LoggerFactory.getLogger(MemberCardController.class);
	
	@Resource
	private MemberCardService memberCardService;

	/**
	 * 分页查询用户会员卡
	 */
	@RequestMapping(value = "/member/cards", method = RequestMethod.GET)
	public ResponseDTO findPage(Pagination pagination, Integer memberId) {
		return ResponseDTO.ok()
				.setPagination(memberCardService.findPage(pagination, memberId));
	}

	/**
	 * 查询用户会员卡信息
	 */
	@RequestMapping(value = "/member/card/{id}", method = RequestMethod.GET)
	public ResponseDTO findById(@PathVariable("id") Integer id) {
		return ResponseDTO.ok()
				.addAttribute("memberCard", memberCardService.findById(id));
	}

	/**
	 * 保存用户会员卡
	 */
	@RequestMapping(value = "/member/card", method = RequestMethod.POST)
	public ResponseDTO save(MemberCard memberCard) {
		memberCardService.save(memberCard);
		return ResponseDTO.ok("保存成功");
	}

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

	/**
	 * 我的会员卡
	 */
	@RequestMapping(value = "/member/venue/card", method = RequestMethod.POST)
	@AccessToken
	public ResponseDTO findMyVenueCard(){
		return ResponseDTO.ok().addAttribute("cards",memberCardService.findMyVenueCard(getLoginMemberId()));
	}

	/**
	 * 设置默认会员卡
	 */
	@RequestMapping(value = "/member/venue/updateCard", method = RequestMethod.GET)
	public ResponseDTO updateCardStatus(@RequestParam("cardId") Integer cardId){
		memberCardService.updateCardStatus(getMemberIdByAccessToken(),cardId);
		return ResponseDTO.ok();
	}
}
