package com.goldenwater.slaj.hazard.controller;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
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.slaj.hazard.domain.ObjHaz;
import com.goldenwater.slaj.hazard.domain.ObjHazVo;
import com.goldenwater.slaj.hazard.service.IObjHazService;

/**
 * 危险源对象
 */
@RestController
@RequestMapping("/hazard/objhaz")
public class ObjHazController extends BaseController {
    @Autowired
    private IObjHazService objHazService;

    @GetMapping("/list")
    public TableDataInfo list(ObjHazVo objHazVo) {
        startPage();
        List<ObjHazVo> list = objHazService.selectObjHazList(objHazVo);
        return getDataTable(list);
    }

    @GetMapping("/{guid}")
    public AjaxResult getInfo(@PathVariable String guid) {
        return AjaxResult.success(objHazService.selectObjHazByGuid(guid));
    }

    @PostMapping
    public AjaxResult add(@RequestBody ObjHaz objHaz) {
        return toAjax(objHazService.insertObjHaz(objHaz));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody ObjHaz objHaz) {
        return toAjax(objHazService.updateObjHaz(objHaz));
    }

    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(objHazService.deleteObjHazByGuids(guids));
    }

    @Log(title = "危险源管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('hazard:objhaz:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ObjHazVo objHazVo) {
        List<ObjHazVo> list = objHazService.selectObjHazList(objHazVo);
        ExcelUtil<ObjHazVo> util = new ExcelUtil<ObjHazVo>(ObjHazVo.class);
        util.exportExcel(response, list, "危险源数据");
    }

    @GetMapping("/groupBy")
    public TableDataInfo groupBy(@RequestParam(required = false) String hazSort,
                                  @RequestParam(required = false) String engName,
                                  @RequestParam(required = false) String orgName) {
        Map<String, Object> params = new java.util.HashMap<>();
        params.put("hazSort", hazSort);
        params.put("engName", engName);
        params.put("orgName", orgName);
        startPage();
        List<Map<String, Object>> list = objHazService.selectHazGroupBy(params);
        return getDataTable(list);
    }
}
