package com.community.huiju.controller;


import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.service.IBillStatementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;

/**
 * <p>
 * 账单流水表 前端控制器
 * </p>
 *
 * @author weiximei
 * @since 2019-02-13
 */
@RestController
@RequestMapping("/")
@RefreshScope
@Api(value = "流水/订单 信息 API", description = "流水/订单 信息 API")
public class BillStatementController extends BaseController {

    @Autowired
    private IBillStatementService iBillStatementService;

    @ApiOperation(value = "获取订单信息", notes = "获取订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "payPhone订单号," +
                    "billId缴费项编号,billName缴费项名称,billInvoiceId缴费单编号,payName缴费人"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "header", name = "X-Auth-Token", value = "token")
    })
    @RequestMapping(value = "/bill/statement/all", method = RequestMethod.POST)
    public ResponseBean getBillStatement(HttpSession session, @RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillStatementService.getBillStatement(userElement, parameter);
        return responseBean;
    }

    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/statement/exportExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response, HttpSession session) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        HSSFWorkbook workbook = (HSSFWorkbook) iBillStatementService.exportExcel(userElement).getData();
        this.setResponseHeader(response, "流水信息.xls");
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
