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.MallAfterSaleBo;
import org.dromara.mall.trade.domain.vo.MallAfterSaleVo;
import org.dromara.mall.trade.service.MallAfterSaleService;
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/after-sale")
public class AdminMallAfterSaleController extends BaseController {

    private final MallAfterSaleService mallAfterSaleService;

    /**
     * 查询售后申请列表
     */
    @Operation(summary = "查询售后申请列表", description = "分页查询售后申请列表")
    @SaCheckPermission("mall:order:after-sale:list")
    @GetMapping("/list")
    public TableDataInfo<MallAfterSaleVo> list(@Parameter(description = "售后申请查询条件") MallAfterSaleBo bo,
                                               @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallAfterSaleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出售后申请列表
     */
    @Operation(summary = "导出售后申请列表", description = "导出售后申请到Excel")
    @SaCheckPermission("mall:order:after-sale:export")
    @Log(title = "售后申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "售后申请查询条件") MallAfterSaleBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<MallAfterSaleVo> list = mallAfterSaleService.queryList(bo);
        ExcelUtil.exportExcel(list, "售后申请", MallAfterSaleVo.class, response);
    }

    /**
     * 获取售后申请详细信息
     */
    @Operation(summary = "获取售后申请详情", description = "根据售后申请ID获取详细信息")
    @SaCheckPermission("mall:order:after-sale:query")
    @GetMapping("/{id}")
    public R<MallAfterSaleVo> getInfo(@Parameter(description = "售后申请ID", required = true)
                                      @NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(mallAfterSaleService.queryById(id));
    }

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

    /**
     * 审核售后申请（同意/拒绝）
     */
    @Operation(summary = "审核售后申请", description = "审核售后申请，1-同意，2-拒绝")
    @SaCheckPermission("mall:order:after-sale: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,
                         @Parameter(description = "拒绝原因")
                         @RequestParam(required = false) String refuseReason) {
        return toAjax(mallAfterSaleService.audit(id, status, refuseReason));
    }

    /**
     * 确认收货（商家确认收到退货）
     */
    @Operation(summary = "确认收货", description = "商家确认收到退货商品")
    @SaCheckPermission("mall:order:after-sale:receive")
    @Log(title = "售后申请", businessType = BusinessType.UPDATE)
    @PutMapping("/receive/{id}")
    public R<Void> confirmReceive(@Parameter(description = "售后申请ID", required = true)
                                  @NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return toAjax(mallAfterSaleService.confirmReceive(id));
    }

    /**
     * 完成售后（退款完成或换货完成）
     */
    @Operation(summary = "完成售后", description = "完成售后申请（退款完成或换货完成）")
    @SaCheckPermission("mall:order:after-sale:complete")
    @Log(title = "售后申请", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{id}")
    public R<Void> complete(@Parameter(description = "售后申请ID", required = true)
                            @NotNull(message = "主键不能为空")
                            @PathVariable Long id) {
        return toAjax(mallAfterSaleService.complete(id));
    }
}
