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.machinery.MachineryCreateReq;
import org.dromara.machinery.domain.req.machinery.MachineryQueryReq;
import org.dromara.machinery.domain.req.machinery.MachineryUpdateReq;
import org.dromara.machinery.domain.vo.BusMachineryVo;
import org.dromara.machinery.service.IBusMachineryService;
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/machinery")
public class BusMachineryController extends BaseController {

    private final IBusMachineryService busMachineryService;

    /**
     * 查询机械列表
     */
    @SaCheckPermission("machinery:machinery:list")
    @GetMapping("/list")
    public TableDataInfo<BusMachineryVo> list(MachineryQueryReq req, PageQuery pageQuery) {
        return busMachineryService.queryPageList(req, pageQuery);
    }

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

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

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

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

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