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.ArTaskBo;
import org.dromara.inspection.domain.vo.ArTaskVo;
import org.dromara.inspection.service.IArTaskService;
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/task")
public class ArTaskController extends BaseController {

    private final IArTaskService arTaskService;

    /**
     * 查询巡检任务模板列表
     */
    @SaCheckPermission("inspection:task:list")
    @GetMapping("/list")
    public TableDataInfo<ArTaskVo> list(@Validated(QueryGroup.class) ArTaskBo bo, PageQuery pageQuery) {
        return arTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡检任务模板列表
     */
    @SaCheckPermission("inspection:task:export")
    @Log(title = "巡检任务模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ArTaskBo bo, HttpServletResponse response) {
        List<ArTaskVo> list = arTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡检任务模板", ArTaskVo.class, response);
    }

    /**
     * 获取巡检任务模板详细信息
     *
     * @param id 任务ID
     */
    @SaCheckPermission("inspection:task:query")
    @GetMapping("/{id}")
    public R<ArTaskVo> getInfo(@NotNull(message = "任务ID不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(arTaskService.queryById(id));
    }

    /**
     * 新增巡检任务模板
     */
    @SaCheckPermission("inspection:task:add")
    @Log(title = "巡检任务模板", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArTaskBo bo) {
        return toAjax(arTaskService.insertByBo(bo));
    }

    /**
     * 修改巡检任务模板
     */
    @SaCheckPermission("inspection:task:edit")
    @Log(title = "巡检任务模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArTaskBo bo) {
        return toAjax(arTaskService.updateByBo(bo));
    }

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