package org.dromara.elevator.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.elevator.domain.vo.EMaintenanceRecordVo;
import org.dromara.elevator.domain.bo.EMaintenanceRecordBo;
import org.dromara.elevator.service.IEMaintenanceRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 维保记录;
 *
 * @author Lion Li
 * @date 2024-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mrecord/maintenanceRecord")
public class EMaintenanceRecordController extends BaseController {

    private final IEMaintenanceRecordService eMaintenanceRecordService;

    /**
     * 查询维保记录;列表
     */
    @SaCheckPermission("elevator:maintenanceRecord:list")
    @GetMapping("/list")
    public TableDataInfo<EMaintenanceRecordVo> list(EMaintenanceRecordBo bo, PageQuery pageQuery) {
        return eMaintenanceRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出维保记录;列表
     */
    @SaCheckPermission("elevator:maintenanceRecord:export")
    @Log(title = "维保记录;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EMaintenanceRecordBo bo, HttpServletResponse response) {
        List<EMaintenanceRecordVo> list = eMaintenanceRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "维保记录;", EMaintenanceRecordVo.class, response);
    }

    /**
     * 获取维保记录;详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:maintenanceRecord:query")
    @GetMapping("/{id}")
    public R<EMaintenanceRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eMaintenanceRecordService.queryById(id));
    }

    /**
     * 新增维保记录;
     */
    @SaCheckPermission("elevator:maintenanceRecord:add")
    @Log(title = "维保记录;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EMaintenanceRecordBo bo) {
        return toAjax(eMaintenanceRecordService.insertByBo(bo));
    }

    /**
     * 修改维保记录;
     */
    @SaCheckPermission("elevator:maintenanceRecord:edit")
    @Log(title = "维保记录;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EMaintenanceRecordBo bo) {
        return toAjax(eMaintenanceRecordService.updateByBo(bo));
    }

    /**
     * 删除维保记录;
     *
     * @param ids 主键串
     */
    @SaCheckPermission("elevator:maintenanceRecord:remove")
    @Log(title = "维保记录;", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eMaintenanceRecordService.deleteWithValidByIds(List.of(ids), true));
    }
}
