package com.ruoyi.xq.controller.app;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.xq.domain.UserVip;
import com.ruoyi.xq.domain.VipOrder;
import com.ruoyi.xq.domain.VipPrice;
import com.ruoyi.xq.dto.app.pay.OrderCreateVo;
import com.ruoyi.xq.dto.app.vip.GenVipOrderReq;
import com.ruoyi.xq.dto.app.vip.VipHomeVo;
import com.ruoyi.xq.enums.vip.UserVipOpenStatusEnum;
import com.ruoyi.xq.enums.vip.VipStatusEnum;
import com.ruoyi.xq.service.UserVipService;
import com.ruoyi.xq.service.VipOrderService;
import com.ruoyi.xq.service.VipPriceService;
import com.ruoyi.xq.util.CaiNumUtil;
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;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/vip")
@Tag(name = "VIP相关接口")
public class VipAppController {

    @Autowired
    private VipPriceService vipPriceService;
    @Autowired
    private UserVipService userVipService;
    @Autowired
    private VipOrderService vipOrderService;

    @GetMapping("/home")
    @Operation(summary = "获取VIP信息")
    @Log(title = "获取VIP信息", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<VipHomeVo> vipHome(Integer vipType){
        Long userId = LoginHelper.getUserId();
        List<VipPrice> vipPrices = vipPriceService.listByVipType(vipType);
        UserVip userVip = userVipService.getUserVip(userId, vipType);
        VipHomeVo vo = new VipHomeVo();
        for (VipPrice vipPrice : vipPrices) {
            if(vipPrice.getVipTime().equals(1)){
                vipPrice.setDayVipPrice(CaiNumUtil.vipDays(vipPrice.getVipPrice(),30L));
            }else if(vipPrice.getVipTime().equals(2)){
                vipPrice.setDayVipPrice(CaiNumUtil.vipDays(vipPrice.getVipPrice(),90L));
            }else if(vipPrice.getVipTime().equals(3)){
                vipPrice.setDayVipPrice(CaiNumUtil.vipDays(vipPrice.getVipPrice(),365L));
            }else{
                vipPrice.setDayVipPrice(new BigDecimal("0.01"));
            }
        }
        vo.setVipPriceList(vipPrices);
        vo.setUserId(userId);
        if(userVip == null){
            vo.setUserVipOpenStatus(UserVipOpenStatusEnum.NO.getCode());
        }else{
            LocalDateTime now = LocalDateTime.now();
            if(userVip.getVipStatus().equals(VipStatusEnum.CLOSE.getCode())){
                vo.setUserVipOpenStatus(UserVipOpenStatusEnum.NO.getCode());
            }else {
                if(userVip.getVipTimeout().isBefore(now)){
                    vo.setUserVipOpenStatus(UserVipOpenStatusEnum.VIP_TIMEOUT.getCode());
                    vo.setVipTimeOut(userVip.getVipTimeout());
                }else{
                    vo.setUserVipOpenStatus(UserVipOpenStatusEnum.OPEN_VIP.getCode());
                    vo.setVipTimeOut(userVip.getVipTimeout());
                }
            }
        }
        return R.ok(vo);
    }
    @PostMapping("/order/create")
    @Operation(summary = "生成VIP订单")
    @Log(title = "生成VIP订单", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<OrderCreateVo> createVipOrder(@RequestBody GenVipOrderReq req){
        Long userId = LoginHelper.getUserId();
        VipOrder vipOrder = vipOrderService.createVipOrder(userId, req.getVipPriceSettingId());
        OrderCreateVo result = new OrderCreateVo();
        result.setPrice(vipOrder.getVipPrice());
        result.setOrderNo(vipOrder.getOrderNo());
        result.setOrderName(vipOrder.getOrderName());
        return R.ok(result);
    }
}
