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.ITaUserLicenseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;

/**
 * <p>
 * 月租车缴费订单表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-13
 */
@RestController
@RequestMapping("/")
@Api(value = "月租车缴费订单表", description = "月租车缴费订单表")
public class TaUserLicenseOrderController extends BaseController {

    @Autowired
    private ITaUserLicenseOrderService iTaUserLicenseOrderService;

    @RequestMapping(value = "/user/license/order", method = RequestMethod.GET)
    @ApiOperation(value = "获取月租车缴费记录", notes = "获取月租车缴费记录")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "header", name = "X-Auth-Token", value = "Token"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "licensePlate", value = "车牌号"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "orderNumber", value = "订单号"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "paymentName", value = "缴费人姓名"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "paymentTel", value = "缴费人手机号"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "pageNum", value = "第几页"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "pageSize", value = "一页多少条"),
    })
    public ResponseBean getAll(@RequestParam(value = "id",required = false) Integer id,
                               @RequestParam(value = "licensePlate",required = false) String licensePlate,
                               @RequestParam(value = "orderNumber",required = false) String orderNumber,
                               @RequestParam(value = "paymentName",required = false) String paymentName,
                               @RequestParam(value = "paymentTel",required = false) String paymentTel,
                               @RequestParam(value = "pageNum",defaultValue = "1", required = false) Integer pageNum,
                               @RequestParam(value = "pageSize" ,defaultValue = "10", required = false) Integer pageSize,
                               HttpSession session) {

        UserElement userElement = getUserElement(session);
        ResponseBean responseBean = iTaUserLicenseOrderService.getAll(id, licensePlate, orderNumber, paymentName, paymentTel, pageNum, pageSize, userElement);
        return responseBean;
    }


    @ApiOperation(value = "月租车导出数据", notes = "月租车导出数据")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "header", name = "X-Auth-Token", value = "token")
    })
    @RequestMapping(value = "/user/license/export", method = RequestMethod.GET)
    public void exportDate(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        UserElement userElement = getUserElement(session);
        ResponseBean responseBean = iTaUserLicenseOrderService.exportDate(userElement);
        Workbook workbook = (Workbook) responseBean.getData();
        this.setResponseHeader(response, "月租租车.xlsx");
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
