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.TpOrderCommentBo;
import org.dromara.work.domain.vo.TpOrderCommentVo;
import org.dromara.work.service.ITpOrderCommentService;
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/orderComment")
public class TpOrderCommentController extends BaseController {

    private final ITpOrderCommentService tpOrderCommentService;

    /**
     * 查询订单评论列表
     */
    @SaCheckPermission("work:orderComment:list")
    @GetMapping("/list")
    public TableDataInfo<TpOrderCommentVo> list(TpOrderCommentBo bo, PageQuery pageQuery) {
        return tpOrderCommentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单评论列表
     */
    @SaCheckPermission("work:orderComment:export")
    @Log(title = "订单评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpOrderCommentBo bo, HttpServletResponse response) {
        List<TpOrderCommentVo> list = tpOrderCommentService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单评论", TpOrderCommentVo.class, response);
    }

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

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

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

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