package com.goldenwater.slgc.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.goldenwater.common.annotation.Log;
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.enums.BusinessType;
import com.goldenwater.common.utils.poi.ExcelUtil;
import com.goldenwater.slgc.domain.SlgcWaterUnit;
import com.goldenwater.slgc.domain.SlgcWaterUnitRecord;
import com.goldenwater.slgc.service.WaterUnitService;
import com.goldenwater.slgc.service.WaterUnitRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

/**
 * 水利考核管理
 *
 * @author goldenwater
 */
@Tag(name = "11. 水利考核管理")
@RestController
@RequestMapping("/slgc/water-unit")
public class WaterUnitController extends BaseController {
    @Autowired
    private WaterUnitService waterUnitService;
    @Autowired
    private WaterUnitRecordService waterUnitRecordService;

    /**
     * 查询水管单位列表
     *
     * @param slgcWaterUnit 水管单位查询条件
     * @return 分页列表数据
     */
    @Operation(summary = "11.01 查询水管单位列表")
    @GetMapping("/list")
    public TableDataInfo list(SlgcWaterUnit slgcWaterUnit) {
        startPage();
        List<SlgcWaterUnit> list = waterUnitService.selectWaterUnitList(slgcWaterUnit);
        return getDataTable(list);
    }

    /**
     * 获取水管单位详细信息
     *
     * @param id 水管单位ID
     * @return 水管单位信息
     */
    @Operation(summary = "11.02 获取水管单位详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return success(waterUnitService.selectWaterUnitById(id));
    }

    /**
     * 新增水管单位
     *
     * @param slgcWaterUnit 水管单位信息
     * @return 操作结果
     */
    @Operation(summary = "11.03 新增水管单位")
    @Log(title = "水利考核管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlgcWaterUnit slgcWaterUnit) {
        return toAjax(waterUnitService.insertWaterUnit(slgcWaterUnit));
    }

    /**
     * 修改水管单位
     *
     * @param slgcWaterUnit 水管单位信息
     * @return 操作结果
     */
    @Operation(summary = "11.04 修改水管单位")
    @Log(title = "水利考核管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlgcWaterUnit slgcWaterUnit) {
        return toAjax(waterUnitService.updateWaterUnit(slgcWaterUnit));
    }

    /**
     * 删除水管单位
     *
     * @param ids 待删除的水管单位ID数组
     * @return 操作结果
     */
    @Operation(summary = "11.05 删除水管单位")
    @Log(title = "水利考核管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(waterUnitService.deleteWaterUnitByIds(ids));
    }

    /**
     * 导出水管单位列表
     *
     * @param slgcWaterUnit 查询条件
     */
    @Operation(summary = "11.11 导出水管单位")
    @Log(title = "水利考核管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(SlgcWaterUnit slgcWaterUnit, HttpServletResponse response) {
        List<SlgcWaterUnit> list = waterUnitService.selectWaterUnitList(slgcWaterUnit);
        ExcelUtil<SlgcWaterUnit> util = new ExcelUtil<SlgcWaterUnit>(SlgcWaterUnit.class);
        util.exportExcel(response, list, "用水单位数据");
    }

    /**
     * 导入水管单位列表
     *
     * @param file 上传的Excel文件
     * @return 导入结果
     */
    @Operation(summary = "11.12 导入水管单位")
    @Log(title = "水利考核管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SlgcWaterUnit> util = new ExcelUtil<SlgcWaterUnit>(SlgcWaterUnit.class);
        List<SlgcWaterUnit> unitList = util.importExcel(file.getInputStream());
        String message = waterUnitService.importWaterUnit(unitList);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @Operation(summary = "11.13 下载水管单位导入模板")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SlgcWaterUnit> util = new ExcelUtil<SlgcWaterUnit>(SlgcWaterUnit.class);
        util.importTemplateExcel(response, "用水单位数据");
    }

    /**
     * 查询审核记录列表
     *
     * @param unitId 水管单位ID
     * @return 分页列表数据
     */
    @Operation(summary = "11.06 查询审核记录列表")
    @GetMapping("/record/list")
    public TableDataInfo listRecord(String unitId) {
        startPage();
        List<SlgcWaterUnitRecord> list = waterUnitRecordService.selectWaterUnitRecordList(unitId);
        return getDataTable(list);
    }

    /**
     * 获取审核记录详细信息
     *
     * @param id 记录ID
     * @return 审核记录信息
     */
    @Operation(summary = "11.07 获取审核记录详细信息")
    @GetMapping("/record/{id}")
    public AjaxResult getInfoRecord(@PathVariable String id) {
        return success(waterUnitRecordService.selectWaterUnitRecordById(id));
    }

    /**
     * 新增审核记录
     *
     * @param record 审核记录信息
     * @return 操作结果
     */
    @Operation(summary = "11.08 新增审核记录")
    @Log(title = "水利考核管理", businessType = BusinessType.INSERT)
    @PostMapping("/record")
    public AjaxResult addRecord(@RequestBody SlgcWaterUnitRecord record) {
        return toAjax(waterUnitRecordService.insertWaterUnitRecord(record));
    }

    /**
     * 修改审核记录
     *
     * @param record 审核记录信息
     * @return 操作结果
     */
    @Operation(summary = "11.09 修改审核记录")
    @Log(title = "水利考核管理", businessType = BusinessType.UPDATE)
    @PutMapping("/record")
    public AjaxResult editRecord(@RequestBody SlgcWaterUnitRecord record) {
        return toAjax(waterUnitRecordService.updateWaterUnitRecord(record));
    }

    /**
     * 删除审核记录
     *
     * @param ids 待删除的记录ID数组
     * @return 操作结果
     */
    @Operation(summary = "11.10 删除审核记录")
    @Log(title = "水利考核管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/record/{ids}")
    public AjaxResult removeRecord(@PathVariable String[] ids) {
        return toAjax(waterUnitRecordService.deleteWaterUnitRecordByIds(ids));
    }
}
