package org.wfc.user.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.user.domain.UPackage;
import org.wfc.user.domain.URateLimit;
import org.wfc.user.service.IUPackageService;
import org.wfc.user.service.IURateLimitService;

import java.util.List;

/**
 * <p>
 * 用户平台-套餐表 前端控制器
 * </p>
 *
 * @author sys
 * @since 2024-12-20
 */
@RestController
@RequestMapping("/package")
public class UPackageController extends BaseController {

    @Autowired
    public IUPackageService uPackageService;

    @Autowired
    private IURateLimitService uRateLimitService;

    @GetMapping("/page")
    public TableDataInfo page(UPackage uPackage) {
        startPage();
        List<UPackage> list = getPackages();
        return getDataTable(list);
    }

    @GetMapping("/list")
    public AjaxResult list(UPackage uPackage) {
        List<UPackage> list = getPackages();
        return success(list);
    }

    private List<UPackage> getPackages() {
        List<UPackage> list = uPackageService.list(Wrappers.<UPackage>lambdaQuery()
                .eq(UPackage::getPackageEnable, true));
        for (UPackage pack : list) {
            if (ObjectUtil.isNull(pack.getRateLimitId())) {
                continue;
            }
            URateLimit uRateLimit = uRateLimitService.getById(pack.getRateLimitId());
            pack.setRateLimits(uRateLimit);
        }
        return list;
    }
}
