package org.dromara.work.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.work.domain.vo.TpAppealsVo;
import org.dromara.work.domain.bo.TpAppealsBo;
import org.dromara.work.service.ITpAppealsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 订单投诉
 *
 * @author Maosw
 * @date 2025-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/appeals")
public class TpAppealsController extends BaseController {

    private final ITpAppealsService tpAppealsService;

    /**
     * 查询订单投诉列表
     */
    @SaCheckPermission("work:appeals:list")
    @GetMapping("/list")
    public TableDataInfo<TpAppealsVo> list(TpAppealsBo bo, PageQuery pageQuery) {
        return tpAppealsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单投诉列表
     */
    @SaCheckPermission("work:appeals:export")
    @Log(title = "订单投诉", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpAppealsBo bo, HttpServletResponse response) {
        List<TpAppealsVo> list = tpAppealsService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单投诉", TpAppealsVo.class, response);
    }

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

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

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

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