package com.hotwj.platform.securityManagement.hiddenDangerPlan.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.hotwj.platform.securityManagement.hiddenDangerPlan.domain.bo.HotHiddenDangerPlanBo;
import com.hotwj.platform.securityManagement.hiddenDangerPlan.domain.vo.HotHiddenDangerPlanVo;
import com.hotwj.platform.securityManagement.hiddenDangerPlan.service.IHotHiddenDangerPlanService;
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 jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 隐患排查
 *
 * @author shihongwei
 * @date 2025-12-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/securityManagement/hiddenDangerPlan")
@Tag(name = "隐患排查", description = "隐患排查管理")
public class HotHiddenDangerPlanController extends BaseController {

    private final IHotHiddenDangerPlanService hotHiddenDangerPlanService;

    /**
     * 查询隐患排查列表
     */
    //@SaCheckPermission("securityManagement:hiddenDangerPlan:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询隐患排查列表")
    public TableDataInfo<HotHiddenDangerPlanVo> list(HotHiddenDangerPlanBo bo, PageQuery pageQuery) {
        return hotHiddenDangerPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出隐患排查列表
     */
    //@SaCheckPermission("securityManagement:hiddenDangerPlan:export")
    @Log(title = "隐患排查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出隐患排查列表")
    public void export(HotHiddenDangerPlanBo bo, HttpServletResponse response) {
        List<HotHiddenDangerPlanVo> list = hotHiddenDangerPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "隐患排查", HotHiddenDangerPlanVo.class, response);
    }

    /**
     * 获取隐患排查详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("securityManagement:hiddenDangerPlan:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取隐患排查详细信息")
    public R<HotHiddenDangerPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                            @Parameter(name = "id", description = "主键", required = true, example = "1")
                                            @PathVariable Long id) {
        return R.ok(hotHiddenDangerPlanService.queryById(id));
    }

    /**
     * 手动生成隐患排查计划
     */
    @SaIgnore
    @Log(title = "隐患排查", businessType = BusinessType.INSERT)
    @PostMapping("/generatePlans")
    @Operation(summary = "手动生成隐患排查计划")
    public R<Void> generatePlans() {
        hotHiddenDangerPlanService.generatePlans();
        return R.ok();
    }

    /**
     * 生成指定企业的隐患排查计划
     *
     * @param companyId 企业ID
     */
    //@SaCheckPermission("securityManagement:hiddenDangerPlan:add")
    @Log(title = "隐患排查", businessType = BusinessType.INSERT)
    @PostMapping("/generatePlan/{companyId}")
    @Operation(summary = "生成指定企业的隐患排查计划")
    public R<Void> generatePlan(@NotNull(message = "企业ID不能为空")
                                @Parameter(name = "companyId", description = "企业ID", required = true, example = "1")
                                @PathVariable Long companyId) {
        hotHiddenDangerPlanService.generatePlan(companyId);
        return R.ok();
    }

    /**
     * 新增隐患排查
     */
    //@SaCheckPermission("securityManagement:hiddenDangerPlan:add")
    @Log(title = "隐患排查", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增隐患排查")
    public R<Long> add(@Validated(AddGroup.class) @RequestBody HotHiddenDangerPlanBo bo) {
        Long id = hotHiddenDangerPlanService.insertByBo(bo);
        return R.ok(id);
    }

    /**
     * 修改隐患排查
     */
    //@SaCheckPermission("securityManagement:hiddenDangerPlan:edit")
    @Log(title = "隐患排查", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改隐患排查")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotHiddenDangerPlanBo bo) {
        return toAjax(hotHiddenDangerPlanService.updateByBo(bo));
    }

    /**
     * 删除隐患排查
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("securityManagement:hiddenDangerPlan:remove")
    @Log(title = "隐患排查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除隐患排查")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @Parameter(name = "ids", description = "主键串", required = true, example = "1,2,3")
                          @PathVariable Long[] ids) {
        return toAjax(hotHiddenDangerPlanService.deleteWithValidByIds(List.of(ids), true));
    }
}
