package com.sv.api.controller;

import com.sv.api.context.PlatformContext;
import com.sv.entity.Order;
import com.sv.service.api.OrderService;
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 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 OrderController extends BaseApiController {

	private final Logger logger = LoggerFactory.getLogger(OrderController.class);
	
	@Resource
	private OrderService orderService;

	/**
	 * 分页查询订单
	 */
	@RequestMapping(value = "/orders", method = RequestMethod.GET)
	public ResponseDTO findPage(Pagination pagination) {
		return ResponseDTO.ok()
				.setPagination(orderService.findPage(pagination));
	}

	/**
	 * 查询订单信息
	 */
	@RequestMapping(value = "/order/{id}", method = RequestMethod.GET)
	public ResponseDTO findById(@PathVariable("id") Integer id) {
		return ResponseDTO.ok()
				.addAttribute("order", orderService.findById(id));
	}
//
//	/**
//	 * 保存订单
//	 */
//	@RequestMapping(value = "/order", method = RequestMethod.POST)
//	public ResponseDTO save(Order order) {
//		orderService.save(order);
//		return ResponseDTO.ok("保存成功");
//	}
//
//	/**
//	 * 删除订单
//	 */
//	@RequestMapping(value = "/order/delete", method = RequestMethod.POST)
//	public ResponseDTO delete(@RequestParam("id[]") Integer[] ids) {
//		orderService.deleteByIds(ids);
//		return ResponseDTO.ok("删除成功");
//	}


	/**
	 * 余额充值
	 */
    @RequestMapping(value = "/order/recharge", method = RequestMethod.POST)
    @AccessToken
    public ResponseDTO recharge(@RequestParam("rechargeId") Integer rechargeId){
        return ResponseDTO.ok().
        addAttribute("pay",orderService.createRecharge(getLoginMemberId(),PlatformContext.getValue(),rechargeId,RequestUtils.getIp()));
	}
}
