package com.sv.oms.controller;
import com.sv.service.oms.OrderService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.oms.controller.OmsController;
import com.sv.entity.Order;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;

/**
 * Controller - 订单
 *
 * @author yechanglin
 * @since 2018-08-02
 */
@RestController
public class OrderController extends OmsController {

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

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

	/**
	 * 查询订单信息
	 */
	@RequestMapping(value = "/order/{id}", method = RequestMethod.GET)
	public ResponseDTO findById(@PathVariable("id") Integer id) {
		return ResponseDTO.ok()
				.addAttribute("order", orderService.findById(id));
	}

	/**
	 * 查询月充值金额i
	 * 报表
	 */
	@RequestMapping(value = "/order/money", method = RequestMethod.GET)
	public ResponseDTO findMoney(@RequestParam("time") String time) {
		return ResponseDTO.ok()
				.addAttribute("money", orderService.findMoney(time));
	}

	/**
	 * 保存订单
	 */
	@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("删除成功");
	}

	/**
	 * 导出订单
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/orders/export", method = RequestMethod.GET)
	public void exportMember(@RequestParam(value = "payStatus",required = false) Integer payStatus,
							 @RequestParam(value = "starTime",required = false) String starTime,
							 @RequestParam(value = "endTime",required = false) String endTime,
							 HttpServletResponse response) throws IOException {
		File excel = orderService.exportOrder(payStatus, starTime, endTime);
		writeExcel(excel,"report",response);
	}

}
