package org.dromara.mall.trade.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
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.mall.trade.domain.bo.MallOrderEvaluateBo;
import org.dromara.mall.trade.domain.vo.MallOrderEvaluateVo;
import org.dromara.mall.trade.service.MallOrderEvaluateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 订单评价管理
 *
 * @author pengles
 */
@Tag(name = "管理端-订单评价管理", description = "订单评价管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/order/evaluate")
public class AdminMallOrderEvaluateController extends BaseController {

    private final MallOrderEvaluateService mallOrderEvaluateService;

    /**
     * 查询订单评价列表
     */
    @Operation(summary = "查询订单评价列表", description = "分页查询订单评价列表")
    @SaCheckPermission("mall:order:evaluate:list")
    @GetMapping("/list")
    public TableDataInfo<MallOrderEvaluateVo> list(@Parameter(description = "评价查询条件") MallOrderEvaluateBo bo,
                                                   @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallOrderEvaluateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单评价列表
     */
    @Operation(summary = "导出订单评价列表", description = "导出订单评价到Excel")
    @SaCheckPermission("mall:order:evaluate:export")
    @Log(title = "订单评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "评价查询条件") MallOrderEvaluateBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<MallOrderEvaluateVo> list = mallOrderEvaluateService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单评价", MallOrderEvaluateVo.class, response);
    }

    /**
     * 获取订单评价详细信息
     */
    @Operation(summary = "获取订单评价详情", description = "根据评价ID获取详细信息")
    @SaCheckPermission("mall:order:evaluate:query")
    @GetMapping("/{id}")
    public R<MallOrderEvaluateVo> getInfo(@Parameter(description = "评价ID", required = true)
                                          @NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(mallOrderEvaluateService.queryById(id));
    }

    /**
     * 删除订单评价
     */
    @Operation(summary = "删除订单评价", description = "删除订单评价")
    @SaCheckPermission("mall:order:evaluate:remove")
    @Log(title = "订单评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "评价ID数组", required = true)
                          @NotNull(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallOrderEvaluateService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 审核评价（通过/驳回）
     */
    @Operation(summary = "审核评价", description = "审核评价，1-通过，2-驳回")
    @SaCheckPermission("mall:order:evaluate:audit")
    @Log(title = "订单评价", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{id}")
    public R<Void> audit(@Parameter(description = "评价ID", required = true)
                         @NotNull(message = "主键不能为空")
                         @PathVariable Long id,
                         @Parameter(description = "审核状态", required = true)
                         @NotNull(message = "审核状态不能为空")
                         @RequestParam Integer status) {
        return toAjax(mallOrderEvaluateService.audit(id, status));
    }

    /**
     * 回复评价
     */
    @Operation(summary = "回复评价", description = "商家回复评价")
    @SaCheckPermission("mall:order:evaluate:reply")
    @Log(title = "订单评价", businessType = BusinessType.UPDATE)
    @PutMapping("/reply/{id}")
    public R<Void> reply(@Parameter(description = "评价ID", required = true)
                         @NotNull(message = "主键不能为空")
                         @PathVariable Long id,
                         @Parameter(description = "回复内容", required = true)
                         @NotNull(message = "回复内容不能为空")
                         @RequestParam String replyContent) {
        return toAjax(mallOrderEvaluateService.reply(id, replyContent));
    }
}
