package org.dromara.materials.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.materials.domain.req.materials.MaterialsCreateReq;
import org.dromara.materials.domain.req.materials.MaterialsQueryReq;
import org.dromara.materials.domain.req.materials.MaterialsUpdateReq;
import org.dromara.materials.domain.vo.BusMaterialsVo;
import org.dromara.materials.service.IBusMaterialsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 材料
 *
 * @author lcj
 * @date 2025-03-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/materials/materials")
public class BusMaterialsController extends BaseController {

    private final IBusMaterialsService busMaterialsService;

    /**
     * 查询材料列表
     */
    @SaCheckPermission("materials:materials:list")
    @GetMapping("/list")
    public TableDataInfo<BusMaterialsVo> list(MaterialsQueryReq req, PageQuery pageQuery) {
        return busMaterialsService.queryPageList(req, pageQuery);
    }

    /**
     * 导出材料列表
     */
    @SaCheckPermission("materials:materials:export")
    @Log(title = "材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MaterialsQueryReq req, HttpServletResponse response) {
        List<BusMaterialsVo> list = busMaterialsService.queryList(req);
        ExcelUtil.exportExcel(list, "材料名称", BusMaterialsVo.class, response);
    }

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

    /**
     * 新增材料
     */
    @SaCheckPermission("materials:materials:add")
    @Log(title = "材料", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody MaterialsCreateReq req) {
        return R.ok(busMaterialsService.insertByBo(req));
    }

    /**
     * 修改材料
     */
    @SaCheckPermission("materials:materials:edit")
    @Log(title = "材料", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MaterialsUpdateReq req) {
        return toAjax(busMaterialsService.updateByBo(req));
    }

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