package com.czg.controller.user;

import com.alibaba.fastjson2.JSONObject;
import com.czg.PageQueryParam;
import com.czg.market.dto.PpHelpRecordDTO;
import com.czg.market.dto.PpPackageOrderDTO;
import com.czg.market.service.PpHelpRecordService;
import com.czg.market.service.PpPackageOrderService;
import com.czg.market.service.PpPackageService;
import com.czg.market.vo.PpOrderListVO;
import com.czg.market.vo.PpPackagePageReqVo;
import com.czg.market.vo.PpPackageVO;
import com.czg.order.dto.GbOrderQueryParam;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 用户端/套餐推广
 *
 * @author yjjie
 * @date 2025/12/18 18:03
 */
@RestController
@RequestMapping("/user/package")
public class UPpPackageController {

    @Resource
    private PpPackageService ppPackageService;

    @Resource
    private PpPackageOrderService ppPackageOrderService;

    @Resource
    private PpHelpRecordService ppHelpRecordService;

    /**
     * 创建套餐推广订单
     * 参数: {"packageId": 123, "shopId": 123}
     */
    @PostMapping("/order")
    public CzgResult<Long> createOrder(@RequestBody JSONObject params) {
        AssertUtil.isNull(params, "参数不能为空");
        Long packageId = params.getLong("packageId");
        AssertUtil.isNull(packageId, "参数错误");
        Long shopId = params.getLong("shopId");
        AssertUtil.isNull(shopId, "参数错误");
        return CzgResult.success(ppPackageOrderService.createPackageOrder(packageId, shopId));
    }

    /**
     * 获取套餐列表
     */
    @GetMapping
    public CzgResult<Page<PpPackageVO>> getPackageList(PpPackagePageReqVo reqVo) {
        return CzgResult.success(ppPackageService.getPackagePage(reqVo, false));
    }

    /**
     * 获取套餐详情
     */
    @GetMapping("/detail/{id}")
    public CzgResult<PpPackageVO> getPackageDetail(@PathVariable Long id, Long shopId) {
        return CzgResult.success(ppPackageService.getPackageDetail(id, shopId));
    }

    /**
     * 获取我的订单列表
     */
    @GetMapping("/order")
    public CzgResult<Page<PpOrderListVO>> getMyOrderList(GbOrderQueryParam reqVo) {
        return CzgResult.success(ppPackageOrderService.queryOrderList(reqVo.getShopId(), StpKit.USER.getLoginIdAsLong(), reqVo));
    }

    /**
     * 取消订单
     */
    @GetMapping("/cancel")
    public CzgResult<Boolean> cancelOrder(@RequestParam Long orderId) {
        return CzgResult.success(ppPackageOrderService.cancelOrder(orderId));
    }

    /**
     * 助力订单
     */
    @GetMapping("/help")
    public CzgResult<Boolean> helpOrder(Long orderId) {
        return CzgResult.success(ppPackageOrderService.helpOrder(orderId));
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/order/detail")
    public CzgResult<PpPackageOrderDTO> getOrderDetail(@RequestParam Long orderId) {
        return CzgResult.success(ppPackageOrderService.getOrderDetailById(orderId, StpKit.USER.getLoginIdAsLong()));
    }

    /**
     * 获取助力列表
     */
    @GetMapping("/help/page")
    public CzgResult<Page<PpHelpRecordDTO>> getHelpList(PageQueryParam reqVo) {
        return CzgResult.success(ppHelpRecordService.helpPage(reqVo));
    }
}
