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.ArPointBo;
import org.dromara.inspection.domain.vo.ArPointVo;
import org.dromara.inspection.service.IArPointService;
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/point")
public class ArPointController extends BaseController {

    private final IArPointService arPointService;

    /**
     * 查询巡检点位列表
     */
    @SaCheckPermission("inspection:point:list")
    @GetMapping("/list")
    public TableDataInfo<ArPointVo> list(@Validated(QueryGroup.class) ArPointBo bo, PageQuery pageQuery) {
        return arPointService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡检点位列表
     */
    @SaCheckPermission("inspection:point:export")
    @Log(title = "巡检点位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ArPointBo bo, HttpServletResponse response) {
        List<ArPointVo> list = arPointService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡检点位", ArPointVo.class, response);
    }

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

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

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

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