package com.goldenwater.slaj.hidd.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.slaj.hidd.domain.ObjHidd;
import com.goldenwater.slaj.hidd.service.IObjHiddService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import com.goldenwater.common.utils.poi.ExcelUtil;

/**
 * 隐患对象
 */
@RestController
@RequestMapping("/hidd/objhidd")
public class ObjHiddController extends BaseController {
    @Autowired
    private IObjHiddService objHiddService;

    @GetMapping("/list")
    public TableDataInfo list(ObjHidd objHidd) {
        startPage();
        List<ObjHidd> list = objHiddService.selectObjHiddList(objHidd);
        return getDataTable(list);
    }

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

    @PostMapping
    public AjaxResult add(@RequestBody ObjHidd objHidd) {
        return toAjax(objHiddService.insertObjHidd(objHidd));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody ObjHidd objHidd) {
        return toAjax(objHiddService.updateObjHidd(objHidd));
    }

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

    /**
     * 隐患名称匹配查询
     */
    @GetMapping("/listByName")
    public TableDataInfo listByName(ObjHidd objHidd) {
        startPage();
        List<ObjHidd> list = objHiddService.selectObjHiddListByName(objHidd);
        return getDataTable(list);
    }

    /**
     * 下级单位隐患列表
     */
    @GetMapping("/nextDepList")
    public TableDataInfo nextDepList(ObjHidd objHidd) {
        startPage();
        List<ObjHidd> list = objHiddService.selectNextDepHiddList(objHidd);
        return getDataTable(list);
    }

    /**
     * 下级隐患保存
     */
    @PostMapping("/saveNextHidd")
    public AjaxResult saveNextHidd(@RequestBody ObjHidd objHidd) {
        return toAjax(objHiddService.insertNextHidd(objHidd));
    }

    @Log(title = "隐患对象管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('hidd:objhidd:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ObjHidd objHidd) {
        List<ObjHidd> list = objHiddService.selectObjHiddList(objHidd);
        ExcelUtil<ObjHidd> util = new ExcelUtil<>(ObjHidd.class);
        util.exportExcel(response, list, "隐患对象数据");
    }

    /**
     * 隐患导入（Excel批量导入）
     */
    @Log(title = "隐患对象管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('hidd:objhidd:add')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ObjHidd> util = new ExcelUtil<>(ObjHidd.class);
        List<ObjHidd> list = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failMsg = new StringBuilder();
        int successCount = 0;
        int failCount = 0;
        for (ObjHidd hidd : list) {
            try {
                objHiddService.insertObjHidd(hidd);
                successCount++;
            } catch (Exception e) {
                failCount++;
                failMsg.append("行").append(successCount + failCount).append(": ").append(e.getMessage()).append("<br/>");
            }
        }
        successMsg.append("导入成功").append(successCount).append("条");
        if (failCount > 0) {
            failMsg.insert(0, "导入失败").append(failCount).append("条<br/>");
        }
        return AjaxResult.success(successMsg.toString() + failMsg.toString());
    }
}
