package org.dromara.work.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.work.domain.bo.TpProdBo;
import org.dromara.work.domain.vo.TpProdVo;
import org.dromara.work.service.ITpProdService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品
 *
 * @author Maosw
 * @date 2025-03-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/prod")
public class TpProdController extends BaseController {

    private final ITpProdService tpProdService;

    /**
     * 查询商品列表
     */
    @SaCheckPermission("work:prod:list")
    @GetMapping("/list")
    public TableDataInfo<TpProdVo> list(TpProdBo bo, PageQuery pageQuery) {
        return tpProdService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品列表
     */
    @SaCheckPermission("work:prod:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpProdBo bo, HttpServletResponse response) {
        List<TpProdVo> list = tpProdService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品", TpProdVo.class, response);
    }

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

    /**
     * 新增商品
     */
    @SaCheckPermission("work:prod:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpProdBo bo) {
        return toAjax(tpProdService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("work:prod:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpProdBo bo) {
        return toAjax(tpProdService.updateByBo(bo));
    }

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