package com.ruoyi.xq.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.PageModel;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.xq.domain.UserExtend;
import com.ruoyi.xq.domain.WxTransOrder;
import com.ruoyi.xq.dto.app.account.*;
import com.ruoyi.xq.dto.app.pay.OrderCreateVo;
import com.ruoyi.xq.dto.app.wxtrans.GenWxTransOrderReq;
import com.ruoyi.xq.dto.app.wxtrans.WxTransPriceAppVo;
import com.ruoyi.xq.enums.common.SystemConfigEnum;
import com.ruoyi.xq.manager.SystemConfigManager;
import com.ruoyi.xq.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/api/account")
@Tag(name = "账户管理")
public class AccountAppController {
    @Autowired
    private UserExtendService userExtendService;
    @Autowired
    private AccountChangeLogService accountChangeLogService;
    @Autowired
    private WxTransOrderService wxTransOrderService;
    @Autowired
    private SystemConfigManager systemConfigManager;
    @Autowired
    private WxTransDataService wxTransDataService;
    @Autowired
    private WxTransLogService wxTransLogService;

    @GetMapping("/info")
    @Operation(summary = "当前用户信息的账户余额情况")
    @Log(title = "当前用户信息的账户余额情况", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<AccountInfoVO> accountInfo(){
        UserExtend userExtend = userExtendService.getByUserId(LoginHelper.getUserId());
        AccountInfoVO accountInfoVO = new AccountInfoVO();
        accountInfoVO.setUserId(userExtend.getUserId());
        accountInfoVO.setIncomeCoin(userExtend.getIncomeCoin());
        accountInfoVO.setWxTransNum(userExtend.getWxTransNum());
        return R.ok(accountInfoVO);
    }

    @GetMapping("/detail/page")
    @Operation(summary = "查询账户明细")
    @Log(title = "查询账户明细", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<PageModel<AccountDetailVo>> detail(PageQuery pageQuery){
        Page<AccountDetailVo> page = accountChangeLogService.pageApp(pageQuery,LoginHelper.getUserId());
        return R.ok(PageModel.build(page));
    }

    @GetMapping("/wxTrans/price")
    @Operation(summary = "获取微信交换单价")
    @Log(title = "获取微信交换单价", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<WxTransPriceAppVo> getPrice(){
        WxTransPriceAppVo vo = new WxTransPriceAppVo();
        BigDecimal price = systemConfigManager.getSystemConfigOfBigDecimal(SystemConfigEnum.WX_TRANS_PRICE);
        vo.setPrice(price);
        return R.ok(vo);
    }

    @PostMapping("/wxTrans/order/create")
    @Operation(summary = "生成微信交换订单")
    @Log(title = "生成微信交换订单", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<OrderCreateVo> createWxTransOrder(@RequestBody GenWxTransOrderReq req){
        Long userId = LoginHelper.getUserId();
        WxTransOrder vipOrder = wxTransOrderService.createWxTransOrder(userId, req.getWxTransNum());
        OrderCreateVo result = new OrderCreateVo();
        result.setPrice(vipOrder.getWxPrice());
        result.setOrderNo(vipOrder.getOrderNo());
        result.setOrderName(vipOrder.getOrderName());
        return R.ok(result);
    }

    @PostMapping("/wxTrans/apply")
    @Operation(summary = "处理交换微信")
    @Log(title = "处理交换微信", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> applyWxTrans(@RequestBody WxTransDataApplyReq req){
        req.setUserId(LoginHelper.getUserId());
        wxTransDataService.apply(req);
        return R.ok();
    }

    @PostMapping("/wxTrans/star")
    @Operation(summary = "发起交换微信")
    @Log(title = "发起交换微信", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> starWxTrans(@RequestBody StarWxTransReq starWxTransReq){
        starWxTransReq.setSponsorUserId(LoginHelper.getUserId());
        wxTransDataService.start(starWxTransReq);
        return R.ok();
    }


    @GetMapping("/wxTrans/data/page")
    @Operation(summary = "查询交换微信数据")
    @Log(title = "发起交换微信", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<PageModel<WxTransDataListAppVo>> wxTransDataPage(PageQuery pageQuery, WxTransDataQuery wxTransDataQuery){
        wxTransDataQuery.setUserId(LoginHelper.getUserId());
        Page<WxTransDataListAppVo> page = wxTransDataService.pageApp(pageQuery, wxTransDataQuery);
        return R.ok(PageModel.build(page));
    }

    @GetMapping("/wxTrans/log/page")
    @Operation(summary = "查询交换微信账户明细")
    @Log(title = "查询交换微信账户明细", businessType = BusinessType.OTHER,isPrintResponseData = false, isSaveDb = false)
    public R<PageModel<WxTransLogListVo>> wxTransLogPage(PageQuery pageQuery, WxTransLogQuery query){
        query.setUserId(LoginHelper.getUserId());
        Page<WxTransLogListVo> page = wxTransLogService.pageApp(pageQuery, query);
        return R.ok(PageModel.build(page));
    }




}
