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.dto.machinery.EqpMachineryCreateReq;
import org.dromara.machinery.domain.dto.machinery.EqpMachineryGisReq;
import org.dromara.machinery.domain.dto.machinery.EqpMachineryQueryReq;
import org.dromara.machinery.domain.dto.machinery.EqpMachineryUpdateReq;
import org.dromara.machinery.domain.vo.machinery.EqpMachineryVo;
import org.dromara.machinery.domain.vo.machinery.EqpMachineryGisVo;
import org.dromara.machinery.service.IEqpMachineryService;
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 EqpMachineryController extends BaseController {

    private final IEqpMachineryService machineryService;

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

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

    /**
     * 查询大屏机械列表
     */
    @SaCheckPermission("machinery:machinery:list")
    @GetMapping("/list/gis")
    public R<List<EqpMachineryGisVo>> queryGisList(EqpMachineryGisReq req) {
        return R.ok(machineryService.queryGisList(req));
    }

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

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

    /**
     * 修改机械
     */
    @SaCheckPermission("machinery:machinery:edit")
    @Log(title = "机械", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EqpMachineryUpdateReq req) {
        return toAjax(machineryService.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(machineryService.deleteWithValidByIds(List.of(ids), true));
    }
}
