package com.zksy.web.controller.WaterSupply;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.WaterSupply.WaterPipeInfo.domain.WaterPipeInfo;
import com.zksy.WaterSupply.WaterPipeInfo.service.IWaterPipeInfoService;
import com.zksy.WaterSupply.WaterPlantInfo.domain.WaterPlantInfo;
import com.zksy.WaterSupply.WaterPlantInfo.service.IWaterPlantInfoService;
import com.zksy.WaterSupply.WaterPumpStation.domain.WaterPumpStation;
import com.zksy.WaterSupply.WaterPumpStation.service.IWaterPumpStationService;
import com.zksy.WaterSupply.WaterSourceInfo.domain.WaterSourceInfo;
import com.zksy.WaterSupply.WaterSourceInfo.service.IWaterSourceInfoService;
import com.zksy.WaterSupply.WaterUserInfo.domain.WaterUserInfo;
import com.zksy.WaterSupply.WaterUserInfo.service.IWaterUserInfoService;
import com.zksy.WaterSupply.export.*;
import com.zksy.common.annotation.Log;
import com.zksy.common.annotation.Excel;
import com.zksy.common.enums.BusinessType;
import com.zksy.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/water")
public class WaterFacilityExportController {
    @Resource private IWaterSourceInfoService sourceService;
    @Resource private IWaterPlantInfoService plantService;
    @Resource private IWaterPumpStationService pumpService;
    @Resource private IWaterPipeInfoService pipeService;
    @Resource private IWaterUserInfoService userService;

    @GetMapping("/sourceInfo/export")
    @Log(title = "导出水源地信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:waterSource:export')")
    public void sourceExport(HttpServletResponse response, @RequestParam(required = false) String sourceId, @RequestParam(required = false) String sourceName, @RequestParam(required = false) String status, @RequestParam(required = false) String unit, @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime endTime) {
        var page = sourceService.queryPage(new Page<WaterSourceInfo>(1, Integer.MAX_VALUE), sourceId, sourceName, status, unit, startTime, endTime);
        new ExcelUtil<>(WaterSourceExportDTO.class).exportExcel(response, page.getRecords().stream().map(WaterSourceExportDTO::from).collect(Collectors.toList()), "水源地信息");
    }

    @GetMapping("/plantInfo/export")
    @Log(title = "导出水厂信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:waterPlant:export')")
    public void plantExport(HttpServletResponse response, @RequestParam(required = false) String plantCode, @RequestParam(required = false) String plantName, @RequestParam(required = false) String status, @RequestParam(required = false) String principal, @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime endTime) {
        var page = plantService.queryPage(new Page<WaterPlantInfo>(1, Integer.MAX_VALUE), plantCode, plantName, status, principal, startTime, endTime);
        new ExcelUtil<>(WaterPlantExportDTO.class).exportExcel(response, page.getRecords().stream().map(WaterPlantExportDTO::from).collect(Collectors.toList()), "水厂信息");
    }

    @GetMapping("/pumpStation/export")
    @Log(title = "导出泵站信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:waterPumpStation:export')")
    public void pumpExport(HttpServletResponse response, @RequestParam(required = false) String stationCode, @RequestParam(required = false) String stationName, @RequestParam(required = false) String status, @RequestParam(required = false) String principal, @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime endTime) {
        var page = pumpService.queryPage(new Page<WaterPumpStation>(1, Integer.MAX_VALUE), stationCode, stationName, status, principal, startTime, endTime);
        new ExcelUtil<>(WaterPumpStationExportDTO.class).exportExcel(response, page.getRecords().stream().map(WaterPumpStationExportDTO::from).collect(Collectors.toList()), "泵站信息");
    }

    @GetMapping("/pipe/export")
    @Log(title = "导出管网信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:waterPipe:export')")
    public void pipeExport(HttpServletResponse response, @RequestParam(required = false) String pipeCode, @RequestParam(required = false) String pipeName, @RequestParam(required = false) String pipeMaterial, @RequestParam(required = false) String status, @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime endTime) {
        var page = pipeService.queryPage(new Page<WaterPipeInfo>(1, Integer.MAX_VALUE), pipeCode, pipeName, pipeMaterial, status, startTime, endTime);
        new ExcelUtil<>(WaterPipeExportDTO.class).exportExcel(response, page.getRecords().stream().map(WaterPipeExportDTO::from).collect(Collectors.toList()), "管网信息");
    }

    @GetMapping("/userInfo/export")
    @Log(title = "导出用水户信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:waterUser:export')")
    public void userExport(HttpServletResponse response, @RequestParam(required = false) String userCode, @RequestParam(required = false) String userName, @RequestParam(required = false) String status, @RequestParam(required = false) String userType, @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime endTime) {
        var page = userService.queryPage(new Page<WaterUserInfo>(1, Integer.MAX_VALUE), userCode, userName, status, userType, startTime, endTime);
        new ExcelUtil<>(WaterUserExportDTO.class).exportExcel(response, page.getRecords().stream().map(WaterUserExportDTO::from).collect(Collectors.toList()), "用水户信息");
    }
}
