package com.sv.api.controller;

import com.sv.api.context.PlatformContext;
import com.sv.entity.MemberCardOrder;
import com.sv.service.api.MemberCardOrderService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.common.utils.RequestUtils;
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-03
 */
@RestController
public class MemberCardOrderController  extends BaseApiController {

	private final Logger logger = LoggerFactory.getLogger(MemberCardOrderController.class);
	
	@Resource
	private MemberCardOrderService memberCardOrderService;

	/**
	 * 分页查询用户会员卡订单
	 */
	@RequestMapping(value = "/member/card/orders", method = RequestMethod.GET)
	public ResponseDTO findPage(Pagination pagination) {
		return ResponseDTO.ok()
				.setPagination(memberCardOrderService.findPage(pagination));
	}

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

	/**
	 * 保存用户会员卡订单
	 */
	@RequestMapping(value = "/member/card/order", method = RequestMethod.POST)
	public ResponseDTO save(MemberCardOrder memberCardOrder) {
		memberCardOrderService.save(memberCardOrder);
		return ResponseDTO.ok("保存成功");
	}

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

	/**
	 * 用户购买会员卡
	 * @param cardType 卡类型
	 * @param type 场馆类型
	 * @param venueId 场馆ID
	 * @return
	 */
    @RequestMapping(value = "/member/card/order/create", method = RequestMethod.POST)
    public ResponseDTO create(@RequestParam("cardType") Integer cardType,
                              @RequestParam("type")Integer type,
                              @RequestParam("venueId") Integer venueId,
							  @RequestParam("num") Integer num){
        return ResponseDTO.ok().
				addAttribute("pay",memberCardOrderService.createCardOrder(getMemberIdByAccessToken(),cardType,type,venueId,num,PlatformContext.getValue(),RequestUtils.getIp()));
    }
}
