package org.dromara.controller.checkPoint;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.dromara.domain.checkPoint.bo.CheckPointRecordBo;
import org.dromara.domain.checkPoint.vo.CheckPointRecordVo;
import org.dromara.service.ICheckPointRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 巡更点记录
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/checkPointRecord")
public class CheckPointRecordController extends BaseController {

    private final ICheckPointRecordService checkPointRecordService;

    /**
     * 查询巡更点记录列表
     */
    @SaCheckPermission("wuYe:checkPointRecord:list")
    @GetMapping("/list")
    public TableDataInfo<CheckPointRecordVo> list(CheckPointRecordBo bo, PageQuery pageQuery) {
        return checkPointRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡更点记录列表
     */
    @SaCheckPermission("wuYe:checkPointRecord:export")
    @Log(title = "巡更点记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CheckPointRecordBo bo, HttpServletResponse response) {
        List<CheckPointRecordVo> list = checkPointRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡更点记录", CheckPointRecordVo.class, response);
    }

    /**
     * 获取巡更点记录详细信息
     *
     * @param checkPointRecordId 主键
     */
    @SaCheckPermission("wuYe:checkPointRecord:query")
    @GetMapping("/{checkPointRecordId}")
    public R<CheckPointRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long checkPointRecordId) {
        return R.ok(checkPointRecordService.queryById(checkPointRecordId));
    }

    /**
     * 新增巡更点记录
     */
    @SaCheckPermission("wuYe:checkPointRecord:add")
    @Log(title = "巡更点记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CheckPointRecordBo bo) {
        return toAjax(checkPointRecordService.insertByBo(bo));
    }

    /**
     * 修改巡更点记录
     */
    @SaCheckPermission("wuYe:checkPointRecord:edit")
    @Log(title = "巡更点记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CheckPointRecordBo bo) {
        return toAjax(checkPointRecordService.updateByBo(bo));
    }

    /**
     * 删除巡更点记录
     *
     * @param checkPointRecordIds 主键串
     */
    @SaCheckPermission("wuYe:checkPointRecord:remove")
    @Log(title = "巡更点记录", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{checkPointRecordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] checkPointRecordIds) {
        return toAjax(checkPointRecordService.deleteWithValidByIds(List.of(checkPointRecordIds), true));
    }

    /**
     * 巡更点记录首页统计
     */
    @SaCheckPermission("wuYe:checkPointRecord:count")
    @PostMapping("/count")
    public R<Map<String,Object>> count(@RequestBody CheckPointRecordBo bo) {
        return checkPointRecordService.getCheckPointRecordCount(bo);
    }
}
