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.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
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.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
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.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 充值订单
 *
 * @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);
    }

    /**
     * 新增充值订单
     */
    @SaCheckPermission("cai:rechargeOrder:add")
    @Log(title = "充值订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RechargeOrder bo) {
        return toAjax(rechargeOrderService.save(bo));
    }

    /**
     * 修改充值订单
     */
    @SaCheckPermission("cai:rechargeOrder:edit")
    @Log(title = "充值订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RechargeOrder bo) {
        return toAjax(rechargeOrderService.updateById(bo));
    }

    /**
     * 删除充值订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:rechargeOrder:remove")
    @Log(title = "充值订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(rechargeOrderService.removeBatchByIds(Arrays.asList(ids)));
    }
}
