package org.dromara.machinery.controller;

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.machinery.domain.req.machinerydetail.MachineryDetailCreateReq;
import org.dromara.machinery.domain.req.machinerydetail.MachineryDetailQueryReq;
import org.dromara.machinery.domain.req.machinerydetail.MachineryDetailUpdateReq;
import org.dromara.machinery.domain.vo.BusMachineryDetailVo;
import org.dromara.machinery.service.IBusMachineryDetailService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机械详情
 *
 * @author lcj
 * @date 2025-03-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/machinery/machineryDetail")
public class BusMachineryDetailController extends BaseController {

    private final IBusMachineryDetailService busMachineryDetailService;

    /**
     * 查询机械详情列表
     */
    @SaCheckPermission("machinery:machineryDetail:list")
    @GetMapping("/list")
    public TableDataInfo<BusMachineryDetailVo> list(MachineryDetailQueryReq req, PageQuery pageQuery) {
        return busMachineryDetailService.queryPageList(req, pageQuery);
    }

    /**
     * 导出机械详情列表
     */
    @SaCheckPermission("machinery:machineryDetail:export")
    @Log(title = "机械详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MachineryDetailQueryReq req, HttpServletResponse response) {
        List<BusMachineryDetailVo> list = busMachineryDetailService.queryList(req);
        ExcelUtil.exportExcel(list, "机械详情", BusMachineryDetailVo.class, response);
    }

    /**
     * 获取机械详情详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("machinery:machineryDetail:query")
    @GetMapping("/{id}")
    public R<BusMachineryDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(busMachineryDetailService.queryById(id));
    }

    /**
     * 新增机械详情
     */
    @SaCheckPermission("machinery:machineryDetail:add")
    @Log(title = "机械详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody MachineryDetailCreateReq req) {
        return R.ok(busMachineryDetailService.insertByBo(req));
    }

    /**
     * 修改机械详情
     */
    @SaCheckPermission("machinery:machineryDetail:edit")
    @Log(title = "机械详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MachineryDetailUpdateReq req) {
        return toAjax(busMachineryDetailService.updateByBo(req));
    }

    /**
     * 删除机械详情
     *
     * @param ids 主键串
     */
    @SaCheckPermission("machinery:machineryDetail:remove")
    @Log(title = "机械详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busMachineryDetailService.deleteWithValidByIds(List.of(ids), true));
    }
}
