package org.dromara.main.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.main.domain.bo.MainGoodsBo;
import org.dromara.main.domain.vo.MainGoodsVo;
import org.dromara.main.service.IMainGoodsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/main/goods")
public class MainGoodsController extends BaseController {

    private final IMainGoodsService mainGoodsService;

    /**
     * 查询商品列表
     */
    @SaCheckPermission("main:goods:list")
    @GetMapping("/list")
    public TableDataInfo<MainGoodsVo> list(MainGoodsBo bo, PageQuery pageQuery) {
        return mainGoodsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品列表
     */
    @SaCheckPermission("main:goods:export")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MainGoodsBo bo, HttpServletResponse response) {
        List<MainGoodsVo> list = mainGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品信息", MainGoodsVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("main:goods:query")
    @GetMapping("/{id}")
    public R<MainGoodsVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(mainGoodsService.queryById(id));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("main:goods:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MainGoodsBo bo) {
        return toAjax(mainGoodsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("main:goods:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MainGoodsBo bo) {
        return toAjax(mainGoodsService.updateByBo(bo));
    }

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