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.YjjtSlData;
import com.goldenwater.yjjt.service.IYjjtSlDataService;
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.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

    @Resource
    private IYjjtSlDataService yjjtSlDataService;

    @GetMapping("/list")
    @Operation(summary = "查询水量数据列表", description = "查询水量数据列表")
    @PreAuthorize("@ss.hasPermi('yjjtgl:sl:list')")
    public TableDataInfo list(YjjtSlData yjjtSlData) {
        startPage();
        List<YjjtSlData> list = yjjtSlDataService.selectYjjtSlDataList(yjjtSlData);
        return getDataTable(list);
    }

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

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

    @PostMapping("/upload")
    @Operation(summary = "上传水量数据", description = "上传水量数据Excel文件")
    @PreAuthorize("@ss.hasPermi('yjjtgl:sl: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<YjjtSlData> util = new ExcelUtil<>(YjjtSlData.class);
            List<YjjtSlData> dataList = util.importExcel(inputStream);

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

            yjjtSlDataService.importSlData(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:sl:export')")
    public void export(HttpServletResponse response, YjjtSlData yjjtSlData) {
        try {
            List<YjjtSlData> list = yjjtSlDataService.selectYjjtSlDataList(yjjtSlData);
            ExcelUtil<YjjtSlData> util = new ExcelUtil<>(YjjtSlData.class);
            util.exportExcel(response, list, "水量数据");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}