package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.RechargeOrder;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.admin.vo.RechargeOrderAdminVo;
import com.ruoyi.cai.service.RechargeOrderService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanConvertUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 充值订单
 *
 * @author 77
 * @date 2024-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/rechargeOrder")
public class RechargeOrderController extends BaseController {

    private final RechargeOrderService rechargeOrderService;
    private final UserService userService;

    /**
     * 查询充值订单列表
     */
    @SaCheckPermission("cai:rechargeOrder:list")
    @GetMapping("/list")
    public TableDataInfo<RechargeOrderAdminVo> list(RechargeOrderAdminVo bo, PageQuery pageQuery) {
        Page<RechargeOrderAdminVo> page = rechargeOrderService.pageAdmin(pageQuery,bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取充值订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:rechargeOrder:query")
    @GetMapping("/{id}")
    public R<RechargeOrderAdminVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        RechargeOrder rechargeOrder = rechargeOrderService.getById(id);
        RechargeOrderAdminVo res = BeanConvertUtil.convertTo(rechargeOrder, RechargeOrderAdminVo::new);
        User user = userService.getById(res.getUserId());
        BeanUtils.copyProperties(user,res);
        return R.ok(res);
    }

    @GetMapping("/trace")
    public R<RechargeOrderAdminVo> getInfoTraceId(String traceId) {
        RechargeOrder rechargeOrder = rechargeOrderService.getOne(Wrappers.lambdaQuery(RechargeOrder.class).eq(RechargeOrder::getTraceId,traceId).last("limit 1"));
        RechargeOrderAdminVo res = BeanConvertUtil.convertTo(rechargeOrder, RechargeOrderAdminVo::new);
        User user = userService.getById(res.getUserId());
        BeanUtils.copyProperties(user,res);
        return R.ok(res);
    }
}
