package org.wfc.user.controller;

import cn.hutool.core.collection.CollUtil;
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.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.domain.R;
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.security.annotation.InnerAuth;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.user.domain.UOrder;
import org.wfc.user.domain.UPackage;
import org.wfc.user.service.IUOrderService;
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("/order")
public class UOrderController extends BaseController {

    @Autowired
    private IUOrderService uOrderService;

    @Autowired
    private IUPackageService uPackageService;

    @Autowired
    private IURateLimitService uRateLimitService;

    @GetMapping("/page")
    public TableDataInfo page(UOrder uOrder) {
        startPage();
        List<UOrder> list = uOrderService.list();
        return getDataTable(list);
    }

    @GetMapping("/rechargePage")
    public TableDataInfo rechargePage() {
        startPage();
        List<UOrder> list = uOrderService.list(Wrappers.<UOrder>lambdaQuery()
                .eq(UOrder::getUserId, SecurityUtils.getUserId()).eq(UOrder::getType, 1)
                .orderByDesc(UOrder::getCreateTime));

        for (UOrder uOrder : list) {
            if (ObjectUtil.isNull(uOrder.getPackageId())) {
                continue;
            }
            UPackage uPackage = uPackageService.getById(uOrder.getPackageId());
            if (ObjectUtil.isNotNull(uPackage.getRateLimitId())) {
                uPackage.setRateLimits(uRateLimitService.getById(uPackage.getRateLimitId()));
            }
            uOrder.setUPackage(uPackage);
        }
        return getDataTable(list);
    }

    @GetMapping("/packagePage")
    public TableDataInfo packagePage() {
        startPage();
        List<UOrder> list = uOrderService.list(Wrappers.<UOrder>lambdaQuery()
                .eq(UOrder::getUserId, SecurityUtils.getUserId()).eq(UOrder::getType, 0)
                .orderByDesc(UOrder::getCreateTime));
        for (UOrder uOrder : list) {
            if (ObjectUtil.isNull(uOrder.getPackageId())) {
                continue;
            }
            UPackage uPackage = uPackageService.getById(uOrder.getPackageId());
            uOrder.setUPackage(uPackage);
        }
        return getDataTable(list);
    }

    @GetMapping("/list")
    public AjaxResult list(UOrder uOrder) {
        List<UOrder> list = uOrderService.list();
        return success(list);
    }

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

    @PostMapping
    public R<Long> add(@RequestBody UOrder uOrder) {
        return R.ok(uOrderService.saveOrder(uOrder));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody UOrder uOrder) {
        return toAjax(uOrderService.updateById(uOrder));
    }

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(uOrderService.removeByIds(CollUtil.newArrayList(ids)));
    }

    @InnerAuth
    @PostMapping("paySuccess/{id}")
    public AjaxResult paySuccess(@PathVariable("id") Long id) {
        return toAjax(uOrderService.paySuccess(id));
    }

    @PostMapping("payBalance/{id}")
    public AjaxResult payBalance(@PathVariable("id") Long id) {
        return toAjax(uOrderService.payByBalance(id));
    }
}
