package com.xhpc.charging.station.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.xhpc.charging.station.service.IXhpcBarrierGateRecordService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.dto.XhpcBarrierGateRecordExcel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.Charsets;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/barrierGateRecord")
public class XhpcBarrierGateRecordController extends BaseController {

    @Resource
    private IXhpcBarrierGateRecordService xhpcBarrierGateRecordService;


    @GetMapping("/list")
    public TableDataInfo list(String barrierGateCompany, String chargingStationName, String phone,
                              String plateNum, String serialNumber, String internetSerialNumber,
                              Integer status, String startTime, String endTime){
        Map<String, Object> params = new HashMap<>();
        params.put("barrierGateCompany", barrierGateCompany);
        params.put("chargingStationName", chargingStationName);
        params.put("phone", phone);
        params.put("plateNum", plateNum);
        params.put("serialNumber", serialNumber);
        params.put("internetSerialNumber", internetSerialNumber);
        params.put("status", status);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        List<Map<String, Object>> list = xhpcBarrierGateRecordService.getLsit(params);
        return getDataTable(list);
    }

    @GetMapping("/getXhpcBarrierGateRecordById")
    public R getXhpcBarrierGateRecordById(Long barrierGateRecordId) {
        Map<String, Object> map = xhpcBarrierGateRecordService.getXhpcBarrierGateRecordById(barrierGateRecordId);
        return R.ok(map);
    }

    /**
     * 导出数据（单位）
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出数据", notes = "传入channelInfo")
    public void export(String barrierGateCompany, String chargingStationName, String phone,
                       String plateNum, String serialNumber, String internetSerialNumber,
                       Integer status, String startTime, String endTime, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        params.put("barrierGateCompany", barrierGateCompany);
        params.put("chargingStationName", chargingStationName);
        params.put("phone", phone);
        params.put("plateNum", plateNum);
        params.put("serialNumber", serialNumber);
        params.put("internetSerialNumber", internetSerialNumber);
        params.put("status", status);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        List<XhpcBarrierGateRecordExcel> list = xhpcBarrierGateRecordService.export(params);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            String fileName = URLEncoder.encode("道闸记录", Charsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), XhpcBarrierGateRecordExcel.class).sheet("道闸记录").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
