package com.jeesharp.business.modules.mall.controller;

import com.jeesharp.business.modules.mall.application.MallOrderApplicationService;
import com.jeesharp.business.modules.mall.dto.MallOrderPickupVerifyDTO;
import com.jeesharp.business.modules.mall.dto.MallOrderShipDTO;
import com.jeesharp.business.modules.mall.entity.MallOrder;
import com.jeesharp.business.modules.mall.vo.MallOrderExportVO;
import com.jeesharp.business.modules.mall.vo.MallOrderListVO;
import com.jeesharp.business.modules.mall.vo.MallOrderQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端商城订单接口：查询、发货、到店自提核销与导出。
 *
 * <p>C 端下单支付见 {@link com.jeesharp.business.mall.controller.MallOrderPayMobileController}；
 * 编排见 {@link com.jeesharp.business.modules.mall.application.MallOrderApplicationService}（含冻券/核销/双写统一单）。</p>
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/order/v1")
@Tag(name = "订单管理")
@AllArgsConstructor
public class MallOrderController extends BaseController {

    private final MallOrderApplicationService mallOrderApplicationService;

    @Operation(summary = "获取详情")
    @GetMapping(value = "info")
    public AjaxResult<MallOrder> info(@RequestParam("id") String id) {
        MallOrder order = mallOrderApplicationService.getAdminDetail(id);
        return AjaxResult.okData(order);
    }

    @Operation(summary = "获取列表（分页）")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallOrderListVO>> list(@RequestBody MallOrderQueryVO query) {
        PageRespVO<MallOrderListVO> page = mallOrderApplicationService.findPageForAdmin(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "导出Excel")
    @PostMapping(value = "export")
    public void export(@RequestBody MallOrderQueryVO query, HttpServletResponse response) {
        List<MallOrderExportVO> list = mallOrderApplicationService.findExportList(query);
        //ExcelExportUtil.export(response, MallOrderExportVO.class, list, "商城订单");
    }

    @Operation(summary = "到店自提核销")
    @PostMapping(value = "pickup/verify")
    public AjaxResult<Void> verifyPickup(@RequestBody @Valid MallOrderPickupVerifyDTO dto) {
        mallOrderApplicationService.verifyPickupOrder(dto);
        return AjaxResult.ok("核销成功");
    }

    @Operation(summary = "快递发货")
    @PostMapping(value = "ship")
    public AjaxResult<Void> ship(@RequestBody @Valid MallOrderShipDTO dto) {
        mallOrderApplicationService.shipOrder(dto);
        return AjaxResult.ok("发货成功");
    }

    @Operation(summary = "补偿写入统一订单（缺失 unified_order_id 的历史单）")
    @PostMapping(value = "backfillUnified")
    public AjaxResult<Integer> backfillUnified() {
        return AjaxResult.okData(mallOrderApplicationService.backfillUnifiedTradeMissing());
    }

    @Operation(summary = "保存")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody MallOrder order) {
        mallOrderApplicationService.save(order);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        mallOrderApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}