package org.wfc.system.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType;
import org.wfc.system.domain.UPackage;
import org.wfc.system.domain.URateLimit;
import org.wfc.system.service.IUPackageService;
import org.wfc.system.service.IURateLimitService;

import java.util.List;

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

    @Autowired
    private 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);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return success(uPackageService.getById(id));
    }

    @Log(title = "Package Managementt", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UPackage uPackage) {
        return toAjax(uPackageService.save(uPackage));
    }

    @Log(title = "Package Managementt", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UPackage uPackage) {
        return toAjax(uPackageService.updateById(uPackage));
    }

    @Log(title = "Package Managementt", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(uPackageService.removeByIds(CollUtil.newArrayList(ids)));
    }

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