package com.huiju.estateagents.property.controller;


import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.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.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 账单流水表 前端控制器
 * </p>
 *
 * @author weiximei
 * @since 2019-02-13
 */
@RestController
@RequestMapping("/")

@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(HttpServletRequest request, @RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        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, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        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();
        }
    }


}
