package com.buy507.mall.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.buy507.mall.common.api.CommonPage;
import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.dto.ChartDescriptionResult;
import com.buy507.mall.dto.HomeResult;
import com.buy507.mall.dto.MemberShoppingInfoResult;
import com.buy507.mall.dto.MemberTeamInfoResult;
import com.buy507.mall.dto.OrderChartResult;
import com.buy507.mall.dto.WithdrawChartResult;
import com.buy507.mall.service.DmsQueryService;

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

@RestController
@Api(tags = "DmsQueryController", description = "统计查询管理")
@RequestMapping("/query")
public class DmsQueryController {

	@Autowired
	private DmsQueryService queryService;
	
	@ApiOperation("查询首页信息")
    @RequestMapping(value = "/getHomeInfo", method = RequestMethod.GET)
	public CommonResult<HomeResult> getHomeInfo() {
		return CommonResult.success(queryService.getHomeInfo());
	}
	
	
	@ApiOperation("获取订单图表")
    @RequestMapping(value = "/getOrderChart", method = RequestMethod.GET)
	public CommonResult<List<OrderChartResult>> getOrderChart(
			@RequestParam(value = "startTime") String startTime,
			@RequestParam(value = "endTime") String endTime) {
		return CommonResult.success(queryService.getOrderChart(startTime, endTime));
	}
	
	
	@ApiOperation("获取图表描述")
    @RequestMapping(value = "/getChartDescription", method = RequestMethod.GET)
	public CommonResult<ChartDescriptionResult> getChartDescription() {
		return CommonResult.success(queryService.getChartDescription());
	}
	
	
	@ApiOperation("获取提现图表")
    @RequestMapping(value = "/getWithdrawChart", method = RequestMethod.GET)
	public CommonResult<List<WithdrawChartResult>> getWithdrawChart(
			@RequestParam(value = "startTime") String startTime,
			@RequestParam(value = "endTime") String endTime) {
		return CommonResult.success(queryService.getWithdrawChart(startTime, endTime));
	}
	
	
	@ApiOperation("分页查询会员团队信息")
    @RequestMapping(value = "/memberTeamInfo", method = RequestMethod.GET)
    public CommonResult<CommonPage<MemberTeamInfoResult>> memberTeamInfo(
    		@RequestParam(value = "nickname", required = false) String nickname,
    		@RequestParam(value = "inviterName", required = false) String inviterName,
    		@RequestParam(value = "inviterId", required = false) Long inviterId,
    		@RequestParam(value = "memberLevel", required = false) Integer memberLevel,
			@RequestParam(value = "storeStatus", required = false) Integer storeStatus,
			@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		
		List<MemberTeamInfoResult> list = queryService.memberTeamInfo(nickname, inviterName, inviterId, memberLevel, storeStatus, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }
	
	@ApiOperation("查询会员购物信息")
    @RequestMapping(value = "/getMemberShoppingInfo", method = RequestMethod.GET)
    public CommonResult<CommonPage<MemberShoppingInfoResult>> getMemberShoppingInfo(
    		@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime,
			@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		
		List<MemberShoppingInfoResult> list = queryService.getMemberShoppingInfo(startTime, endTime, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }
	
	@ApiOperation("导出会员购物信息")
	@RequestMapping(value = "/exportExcel")
    public ResponseEntity<byte[]> exportExcel(HttpServletResponse response,
    		@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime) throws IOException {
    	
    	try {
    		Workbook wb = queryService.exportExcel(startTime, endTime);
            //设置Http响应头告诉浏览器下载这个附件
    		HttpHeaders headers = new HttpHeaders();
    		headers.add("Content-Disposition", "attachment;Filename=" + System.currentTimeMillis() + ".xls");
    		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    		ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            wb.write(outByteStream);
            outByteStream.close();
            
            return new ResponseEntity<byte[]>(outByteStream.toByteArray(), headers, HttpStatus.OK);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IOException("导出Excel出现严重异常!");
        }
    	
    }
	
}
