package com.sv.api.controller;

import com.sv.api.context.PlatformContext;
import com.sv.dto.api.LessonTicketOrderDTO;
import com.sv.entity.MemberLessonTicket;
import com.sv.service.api.MemberLessonTicketService;
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.common.utils.RequestUtils;
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-06
 */
@RestController
public class MemberLessonTicketController extends BaseApiController {

	private final Logger logger = LoggerFactory.getLogger(MemberLessonTicketController.class);
	
	@Resource
	private MemberLessonTicketService memberLessonTicketService;

	/**
	 * 分页查询用户预约的场馆课程票号
	 */
	@RequestMapping(value = "/member/lesson/tickets", method = RequestMethod.GET)
	public ResponseDTO findPage(Pagination pagination) {
		return ResponseDTO.ok()
				.setPagination(memberLessonTicketService.findPage(pagination));
	}

	/**
	 * 查询用户预约的场馆课程票号信息
	 */
	@RequestMapping(value = "/member/lesson/ticket/{id}", method = RequestMethod.GET)
	public ResponseDTO findById(@PathVariable("id") Integer id) {
		return ResponseDTO.ok()
				.addAttribute("memberLessonTicket", memberLessonTicketService.findById(id));
	}

	/**
	 * 保存用户预约的场馆课程票号
	 */
	@RequestMapping(value = "/member/lesson/ticket", method = RequestMethod.POST)
	public ResponseDTO save(MemberLessonTicket memberLessonTicket) {
		memberLessonTicketService.save(memberLessonTicket);
		return ResponseDTO.ok("保存成功");
	}

	/**
	 * 删除用户预约的场馆课程票号
	 */
	@RequestMapping(value = "/member/lesson/ticket/delete", method = RequestMethod.POST)
	public ResponseDTO delete(@RequestParam("id[]") Integer[] ids) {
		memberLessonTicketService.deleteByIds(ids);
		return ResponseDTO.ok("删除成功");
	}

	/**
	 * 变更用户预约的场馆课程票号状态
	 */
	@RequestMapping(value = "/member/lesson/ticket/status", method = RequestMethod.POST)
	public ResponseDTO updateStatus(@RequestParam("id[]") Integer[] ids, Byte status) {
		memberLessonTicketService.updateStatus(ids, status);
		String operationName = (StatusEnum.ENABLE.value.equals(status) ? "启用" : "禁用") + "成功";
		return ResponseDTO.ok(operationName);
	}

	/**
	 * 预约课程,微信支付
	 */
	@RequestMapping(value = "/member/lesson/ticket/order/wx", method = RequestMethod.POST)
	@AccessToken
	public ResponseDTO ticketOrder(LessonTicketOrderDTO lessonTicketOrderDTO){
		return ResponseDTO.ok().
				addAttribute("pay",memberLessonTicketService.createOrder(getLoginMemberId(),lessonTicketOrderDTO,PlatformContext.getValue(),RequestUtils.getIp()));
	}

	/**
	 * 预约课程，会员卡支付
	 */
	@RequestMapping(value = "/member/lesson/ticket/order/memberCard", method = RequestMethod.POST)
	@AccessToken
	public ResponseDTO ticketOrderByMemberCard(LessonTicketOrderDTO lessonTicketOrderDTO){
		return ResponseDTO.ok()
                .addAttribute("orderSn",memberLessonTicketService.createOrderByMemberCard(getLoginMemberId(),lessonTicketOrderDTO,PlatformContext.getValue()));
	}

    /**
     * 我的预约
     */
    @RequestMapping(value = "/member/lesson/tickets/reserve", method = RequestMethod.POST)
    public ResponseDTO memberLessonTicketReserve(Pagination pagination,@RequestParam("status") Integer status){
        return ResponseDTO.ok().setPagination(memberLessonTicketService.findMemberLessonTicketReserve(getMemberIdByAccessToken(),pagination,status));
    }

    /**
     * 预约详情页
     */
    @RequestMapping(value = "/member/lesson/tickets/detail", method = RequestMethod.POST)
    public ResponseDTO memberLessonTicketDetail(@RequestParam("orderSn") String orderSn){
        return ResponseDTO.ok().addAttribute("memberLessonTicket",memberLessonTicketService.memberLessonTicketDetail(getMemberIdByAccessToken(),orderSn));
    }

	/**
	 * 取消预约课程
	 * 只能取消距离上课时间48小时以外的
	 */
	@RequestMapping(value = "/member/lesson/tickets/cancel", method = RequestMethod.POST)
	@AccessToken
	public ResponseDTO lessonTicketsCancel(@RequestParam("orderSn")String orderSn){
		memberLessonTicketService.lessonTicketsCancel(getLoginMemberId(),orderSn,PlatformContext.getValue());
		return ResponseDTO.ok();
	}

	/**
	 * 签到

	 */
	@RequestMapping(value = "/member/lesson/tickets/sing", method = RequestMethod.POST)
	public ResponseDTO sing (@RequestParam("coachId")Integer lessonId){
		return ResponseDTO.ok().addAttribute("orderSn",memberLessonTicketService.sign(lessonId,getMemberIdByAccessToken()));
	}
}
