package com.ym.mec.teacher.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.TenantPaymentOrder;
import com.ym.mec.biz.service.TenantPaymentOrderService;
import com.ym.mec.common.controller.BaseController;

@Api(tags = "租户订单服务")
@RequestMapping("tenantPaymentOrder")
@RestController
public class TenantPaymentOrderController extends BaseController {

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Autowired
	private TenantPaymentOrderService tenantPaymentOrderService;

	@ApiOperation(value = "下单")
	@PostMapping("/createOrder")
	public Object createOrder(Integer activitiesId, Boolean isContinuePay) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		if (isContinuePay == null) {
			isContinuePay = false;
		}
		return tenantPaymentOrderService.createOrder(sysUser.getId(), activitiesId, isContinuePay);
	}

	@ApiOperation(value = "查询订单")
	@GetMapping("/query")
	public Object query(long id) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		TenantPaymentOrder order = tenantPaymentOrderService.get(id);
		if (order == null) {
			return failed("订单不存在");
		}
		return succeed(order);
	}

	@ApiOperation(value = "查询订单")
	@GetMapping("/queryByOrderNo")
	public Object query(String orderNo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		TenantPaymentOrder order = tenantPaymentOrderService.queryByOrderNo(orderNo);
		if (order == null) {
			return failed("订单不存在");
		}
		return succeed(order);
	}

}
