package org.dromara.inspection.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.core.validate.QueryGroup;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.inspection.domain.bo.ArRegionBo;
import org.dromara.inspection.domain.vo.ArRegionVo;
import org.dromara.inspection.service.IArRegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.util.Arrays;
import java.util.List;

/**
 * 巡检区域Controller
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/inspection/region")
public class ArRegionController extends BaseController {

    private final IArRegionService arRegionService;

    /**
     * 查询巡检区域列表
     */
    @SaCheckPermission("inspection:region:list")
    @GetMapping("/list")
    public TableDataInfo<ArRegionVo> list(@Validated(QueryGroup.class) ArRegionBo bo, PageQuery pageQuery) {
        return arRegionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡检区域列表
     */
    @SaCheckPermission("inspection:region:export")
    @Log(title = "巡检区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ArRegionBo bo, HttpServletResponse response) {
        List<ArRegionVo> list = arRegionService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡检区域", ArRegionVo.class, response);
    }

    /**
     * 获取巡检区域详细信息
     *
     * @param id 区域ID
     */
    @SaCheckPermission("inspection:region:query")
    @GetMapping("/{id}")
    public R<ArRegionVo> getInfo(@NotNull(message = "区域ID不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(arRegionService.queryById(id));
    }

    /**
     * 新增巡检区域
     */
    @SaCheckPermission("inspection:region:add")
    @Log(title = "巡检区域", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArRegionBo bo) {
        return toAjax(arRegionService.insertByBo(bo));
    }

    /**
     * 修改巡检区域
     */
    @SaCheckPermission("inspection:region:edit")
    @Log(title = "巡检区域", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArRegionBo bo) {
        return toAjax(arRegionService.updateByBo(bo));
    }

    /**
     * 删除巡检区域
     *
     * @param ids 区域ID串
     */
    @SaCheckPermission("inspection:region:remove")
    @Log(title = "巡检区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "区域ID不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(arRegionService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
