package com.goldenwater.web.controller.yjjt;

import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.utils.poi.ExcelUtil;
import com.goldenwater.yjjt.domain.YjjtSzData;
import com.goldenwater.yjjt.service.IYjjtSzDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/yjjt/sz")
@Tag(name = "引江济太水质数据管理", description = "引江济太水质数据管理接口")
public class YjjtSzDataController extends BaseController {

    @Resource
    private IYjjtSzDataService yjjtSzDataService;

    @GetMapping("/list")
    @Operation(summary = "查询水质数据列表", description = "查询水质数据列表")
    @PreAuthorize("@ss.hasPermi('yjjtgl:sz:list')")
    public TableDataInfo list(YjjtSzData yjjtSzData) {
        startPage();
        List<YjjtSzData> list = yjjtSzDataService.selectYjjtSzDataList(yjjtSzData);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询水质数据详情", description = "查询水质数据详情")
    @PreAuthorize("@ss.hasPermi('yjjtgl:sz:query')")
    public AjaxResult getInfo(@Parameter(description = "数据ID") @PathVariable("id") Long id) {
        return AjaxResult.success(yjjtSzDataService.selectYjjtSzDataById(id));
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "删除水质数据", description = "删除水质数据")
    @PreAuthorize("@ss.hasPermi('yjjtgl:sz:remove')")
    public AjaxResult remove(@Parameter(description = "数据ID列表") @PathVariable Long[] ids) {
        return toAjax(yjjtSzDataService.deleteYjjtSzDataByIds(ids));
    }

    @PostMapping("/upload")
    @Operation(summary = "上传水质数据", description = "上传水质数据Excel文件")
    @PreAuthorize("@ss.hasPermi('yjjtgl:sz:upload')")
    public AjaxResult upload(@Parameter(description = "Excel文件") @RequestParam("file") MultipartFile file,
                            @Parameter(description = "业务类型(JS/SH/ZJ)") @RequestParam("bizType") String bizType) {
        try {
            InputStream inputStream = file.getInputStream();
            ExcelUtil<YjjtSzData> util = new ExcelUtil<>(YjjtSzData.class);
            List<YjjtSzData> dataList = util.importExcel(inputStream);

            String addvcd = bizType.toUpperCase();
            for (YjjtSzData data : dataList) {
                data.setAddvcd(addvcd);
                data.setCreateBy(getUsername());
            }

            yjjtSzDataService.importSzData(dataList, bizType);
            return AjaxResult.success("上传成功，共导入" + dataList.size() + "条数据");
        } catch (Exception e) {
            return AjaxResult.error("上传失败：" + e.getMessage());
        }
    }

    @PostMapping("/export")
    @Operation(summary = "导出水质数据", description = "导出水质数据Excel文件")
    @PreAuthorize("@ss.hasPermi('yjjtgl:sz:export')")
    public void export(HttpServletResponse response, YjjtSzData yjjtSzData) {
        try {
            List<YjjtSzData> list = yjjtSzDataService.selectYjjtSzDataList(yjjtSzData);
            ExcelUtil<YjjtSzData> util = new ExcelUtil<>(YjjtSzData.class);
            util.exportExcel(response, list, "水质数据");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}