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.PathVariable;
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.MemberWithdrawApplyResult;
import com.buy507.mall.service.DmsMemberWithdrawApplyService;

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

@RestController
@Api(tags = "DmsMemberWithdrawApplyController", description = "会员提现申请管理")
@RequestMapping("/member/withdraw/apply")
public class DmsMemberWithdrawApplyController {
	
	@Autowired
	private DmsMemberWithdrawApplyService service;
	
	@ApiOperation("分页查询提现申请")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MemberWithdrawApplyResult>> list(
    		@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime,
			@RequestParam(value = "paymentStatus", required = false) Integer paymentStatus,
			@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		
		List<MemberWithdrawApplyResult> list = service.list(startTime, endTime, paymentStatus, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }
	
	@ApiOperation("批量确认打款")
    @RequestMapping(value = "/payment", method = RequestMethod.POST)
    public CommonResult<String> payment(@RequestParam("ids") List<Long> ids) {
		service.payment(ids);
        return CommonResult.success(null);
    }

    @ApiOperation("打款失败")
    @RequestMapping(value = "/payment/failed/{id}", method = RequestMethod.POST)
    public CommonResult<String> paymentFailed(@PathVariable Long id, @RequestParam("failedReason") String failedReason) {
    	service.paymentFailed(id, failedReason);
        return CommonResult.success(null);
    }
    
    @RequestMapping(value = "/exportExcel")
    public ResponseEntity<byte[]> exportExcel(HttpServletResponse response,
    		@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime,
			@RequestParam(value = "paymentStatus", required = false) Integer paymentStatus) throws IOException {
    	
    	try {
    		Workbook wb = service.exportExcel(startTime, endTime, paymentStatus);
            //设置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出现严重异常!");
        }
    	
    }

}
