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.TpOrderSmallBo;
import org.dromara.work.domain.vo.TpOrderSmallVo;
import org.dromara.work.service.ITpOrderSmallService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单小图
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/orderSmall")
public class TpOrderSmallController extends BaseController {

    private final ITpOrderSmallService tpOrderSmallService;

    /**
     * 查询订单小图列表
     */
    @SaCheckPermission("work:orderSmall:list")
    @GetMapping("/list")
    public TableDataInfo<TpOrderSmallVo> list(TpOrderSmallBo bo, PageQuery pageQuery) {
        return tpOrderSmallService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单小图列表
     */
    @SaCheckPermission("work:orderSmall:export")
    @Log(title = "订单小图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpOrderSmallBo bo, HttpServletResponse response) {
        List<TpOrderSmallVo> list = tpOrderSmallService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单小图", TpOrderSmallVo.class, response);
    }

    /**
     * 获取订单小图详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:orderSmall:query")
    @GetMapping("/{id}")
    public R<TpOrderSmallVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpOrderSmallService.queryById(id));
    }

    /**
     * 新增订单小图
     */
    @SaCheckPermission("work:orderSmall:add")
    @Log(title = "订单小图", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpOrderSmallBo bo) {
        return toAjax(tpOrderSmallService.insertByBo(bo));
    }

    /**
     * 修改订单小图
     */
    @SaCheckPermission("work:orderSmall:edit")
    @Log(title = "订单小图", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpOrderSmallBo bo) {
        return toAjax(tpOrderSmallService.updateByBo(bo));
    }

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