package com.ruoyi.xq.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.VipOrder;
import com.ruoyi.xq.domain.VipPrice;
import com.ruoyi.xq.dto.admin.vipOrder.VipOrderAdminVo;
import com.ruoyi.xq.enums.common.OrderTypeEnum;
import com.ruoyi.xq.enums.pay.PayStatusEnum;
import com.ruoyi.xq.manager.OrderNoUtil;
import com.ruoyi.xq.mapper.VipOrderMapper;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.service.VipOrderService;
import com.ruoyi.xq.service.VipPriceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * VIP订单Service业务层处理
 *
 * @author 77
 * @date 2024-03-04
 */
@RequiredArgsConstructor
@Service
public class VipOrderServiceImpl extends ServiceImpl<VipOrderMapper,VipOrder> implements VipOrderService {

    @Autowired
    private VipPriceService vipPriceService;
    @Autowired
    private UserService userService;
    @Override
    public VipOrder createVipOrder(Long userId, Long vipPriceSettingId) {
        VipPrice vipPrice = vipPriceService.getById(vipPriceSettingId);
        if(vipPrice == null){
            throw new ServiceException("vip价格错误");
        }
        String orderNo = OrderNoUtil.gen(OrderTypeEnum.VIP);
        User user = userService.getById(userId);
        VipOrder vipOrder = new VipOrder();
        vipOrder.setUserId(user.getId());
        vipOrder.setUsercode(user.getUsercode());
        vipOrder.setVipId(vipPrice.getId());
        vipOrder.setVipType(vipPrice.getVipType());
        vipOrder.setVipTime(vipPrice.getVipTime());
        vipOrder.setVipPrice(vipPrice.getVipPrice());
        vipOrder.setOrderNo(orderNo);
        vipOrder.setOrderName(vipPrice.getVipName());
        vipOrder.setPayStatus(PayStatusEnum.READY_PAY.getCode());
        this.save(vipOrder);
        return vipOrder;
    }

    @Override
    public Page<VipOrderAdminVo> pageApp(PageQuery pageQuery, VipOrderAdminVo bo) {
        return baseMapper.pageApp(pageQuery.build(), bo);
    }
}
