package com.czg.controller.admin;

import com.alibaba.fastjson2.JSONObject;
import com.czg.annotation.SaAdminCheckPermission;
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.order.vo.GbOrderCountVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端/套餐推广
 *
 * @author yjjie
 * @date 2025/12/18 11:18
 */
@Slf4j
@RestController
@RequestMapping("/admin/package")
public class PpPackageController {

    @Resource
    private PpPackageService ppPackageService;

    @Resource
    private PpPackageOrderService ppPackageOrderService;

    /**
     * 添加套餐
     */
    @PostMapping
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:add", name = "添加套餐")
    public CzgResult<Void> addPackage(@RequestBody @Validated({InsertGroup.class}) PpPackageVO packageVO) {
        ppPackageService.insertPackage(packageVO);
        return CzgResult.success();
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:update", name = "修改套餐")
    public CzgResult<Void> updatePackage(@RequestBody @Validated({UpdateGroup.class}) PpPackageVO packageVO) {
        ppPackageService.updatePackage(packageVO);
        return CzgResult.success();
    }

    /**
     * 删除套餐
     */
    @DeleteMapping("/{id}")
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:delete", name = "删除套餐")
    public CzgResult<Boolean> deletePackage(@PathVariable Long id) {
        return CzgResult.success(ppPackageService.deletePackage(id));
    }

    /**
     * 上下架套餐
     * 参数: {"status": 1, "packageId": 123}
     */
    @PutMapping("/online")
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:online", name = "上下架套餐")
    public CzgResult<Boolean> switchPackage(@RequestBody JSONObject param) {
        AssertUtil.isNull(param, "参数错误");
        Integer status = param.getInteger("status");
        Long packageId = param.getLong("packageId");
        AssertUtil.isNull(status, "参数错误");
        AssertUtil.isNull(packageId, "参数错误");
        return CzgResult.success(ppPackageService.updateOnlineStatus(packageId, status));
    }

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

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

    /**
     * 获取套餐推广开关
     * 0: 关闭 1: 开启
     */
    @GetMapping("/switch")
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:getSwitch", name = "获取套餐推广开关")
    public CzgResult<Integer> getPackagePromotionSwitch() {
        return CzgResult.success(ppPackageService.getPackagePromotionSwitch(StpKit.USER.getShopId()));
    }

    /**
     * 修改套餐推广开关
     * 0: 关闭 1: 开启
     * {"status": 1}
     */
    @PutMapping("/switch")
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:updateSwitch", name = "修改套餐推广开关")
    public CzgResult<Boolean> updatePackagePromotionSwitch(@RequestBody JSONObject param) {
        log.info("修改套餐推广开关: {}", param);
        AssertUtil.isNull(param, "参数错误");
        Integer status = param.getInteger("status");
        AssertUtil.isNull(status, "参数错误");
        log.info("修改套餐推广开关 status == {}", status);
        return CzgResult.success(ppPackageService.updatePackagePromotionSwitch(status, StpKit.USER.getShopId()));
    }

    /**
     * 获取套餐推广订单列表
     */
    @GetMapping("/order")
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:order", name = "套餐推广订单列表")
    public CzgResult<Page<PpOrderListVO>> getPackageOrderList(GbOrderQueryParam reqVo) {
        return CzgResult.success(ppPackageOrderService.queryOrderList(StpKit.USER.getShopId(), null, reqVo));
    }

    /**
     * 订单统计
     */
    @GetMapping("/order/stat")
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:order", name = "套餐推广订单列表")
    public CzgResult<GbOrderCountVO> getPackageOrderStat(GbOrderQueryParam reqVo) {
        return CzgResult.success(ppPackageOrderService.orderCount(StpKit.USER.getShopId(), reqVo));
    }
}
