PassengerStatisticController.java 3.84 KB
package com.bsth.controller.report;

import com.bsth.controller.BaseController;
import com.bsth.entity.report.PassengerStatistic;
import com.bsth.service.report.PassengerStatisticService;
import com.bsth.service.report.ReportService;
import com.bsth.util.ReportUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
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 java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api/passenger-statistic")
public class PassengerStatisticController extends BaseController<PassengerStatistic, Integer> {

    @Autowired
    private PassengerStatisticService passengerStatisticService;

    private ObjectMapper mapper = new ObjectMapper();

    @RequestMapping(value = "/groupByStation",method = RequestMethod.GET)
    public List<PassengerStatistic> groupByStation(PassengerStatistic passengerStatistic) {
        return passengerStatisticService.groupByStation(passengerStatistic);
    }

    @RequestMapping(value = "/groupByStation/export",method = RequestMethod.GET)
    public Map<String, Object> groupByStationExport(PassengerStatistic passengerStatistic) {
        List<Iterator<?>> iterators = new ArrayList<>();
        ReportUtils reportUtils = new ReportUtils();
        List<PassengerStatistic> list =  passengerStatisticService.groupByStation(passengerStatistic);
        List<Map<String, Object>> maps = new ArrayList<>();
        int count = 1;
        for (PassengerStatistic ps : list) {
            Map<String, Object> map = mapper.convertValue(ps, Map.class);
            map.put("i", count++);
            maps.add(map);
        }

        try {
            iterators.add(maps.iterator());
            String path = this.getClass().getResource("/").getPath() + "static/pages/forms";
            reportUtils.excelReplace(iterators, new Object[] { new HashMap<>() }, String.format("%s/mould/passenger-statistic-station.xls", path),
                    String.format("%s/export/%s至%s-客流统计(站点).xls", path, passengerStatistic.getRqBegin(), passengerStatistic.getRqEnd()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new HashMap<>();
    }

    @RequestMapping(value = "/groupByVehicle",method = RequestMethod.GET)
    public List<PassengerStatistic> groupByVehicle(PassengerStatistic passengerStatistic) {
        return passengerStatisticService.groupByVehicle(passengerStatistic);
    }

    @RequestMapping(value = "/groupByVehicle/export",method = RequestMethod.GET)
    public Map<String, Object> groupByVehicleExport(PassengerStatistic passengerStatistic) {
        List<Iterator<?>> iterators = new ArrayList<>();
        ReportUtils reportUtils = new ReportUtils();
        List<PassengerStatistic> list =  passengerStatisticService.groupByVehicle(passengerStatistic);
        List<Map<String, Object>> maps = new ArrayList<>();
        int count = 1;
        for (PassengerStatistic ps : list) {
            Map<String, Object> map = mapper.convertValue(ps, Map.class);
            map.put("i", count++);
            maps.add(map);
        }

        try {
            iterators.add(maps.iterator());
            String path = this.getClass().getResource("/").getPath() + "static/pages/forms";
            reportUtils.excelReplace(iterators, new Object[] { new HashMap<>() }, String.format("%s/mould/passenger-statistic-vehicle.xls", path),
                    String.format("%s/export/%s至%s-客流统计(车辆).xls", path, passengerStatistic.getRqBegin(), passengerStatistic.getRqEnd()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new HashMap<>();
    }
}