package com.ruoyi.bst.vipOrder.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.app.service.AppService;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinType;
import com.ruoyi.bst.areaJoin.service.AreaJoinService;
import com.ruoyi.bst.channel.domain.enums.ChannelBstType;
import com.ruoyi.bst.channel.service.ChannelService;
import com.ruoyi.bst.userApp.service.UserAppService;
import com.ruoyi.bst.vip.domain.VipVO;
import com.ruoyi.bst.vip.service.VipService;
import com.ruoyi.bst.vipOrder.domain.VipOrder;
import com.ruoyi.bst.vipOrder.domain.bo.VipOrderCreateBO;
import com.ruoyi.bst.vipOrder.domain.dto.VipOrderCreateDTO;
import com.ruoyi.bst.vipOrder.domain.enums.VipOrderStatus;
import com.ruoyi.bst.vipOrder.service.VipOrderConverter;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.system.user.service.UserService;

@Service
public class VipOrderConverterImpl implements VipOrderConverter {

    @Autowired
    private VipService vipService;

    @Autowired
    private UserService userService;

    @Autowired
    private AppService appService;

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private AreaJoinService areaJoinService;

    @Override
    public VipOrderCreateBO toCreateBO(VipOrderCreateDTO dto) {
        if (dto == null) {
            return null;
        }

        VipOrderCreateBO bo = new VipOrderCreateBO();
        bo.setDto(dto);

        // 获取卡券信息
        bo.setVip(vipService.selectVipById(dto.getVipId()));

        // 获取用户信息
        bo.setUser(userService.selectUserById(dto.getUserId()));

        // 获取APP信息
        bo.setApp(appService.selectAppById(dto.getAppId()));

        // 获取支付渠道信息
        bo.setChannel(channelService.selectChannelByChannelId(dto.getChannelId()));

        // 获取用户APP信息
        if (bo.getUser() != null && bo.getApp() != null) {
            bo.setUserApp(userAppService.selectByUserApp(dto.getUserId(), dto.getAppId()));
        }

        // 获取运营区信息
        if (bo.getVip() != null) {
            bo.setArea(areaService.selectAreaById(bo.getVip().getAreaId()));
            // 获取运营区合伙人信息
            if (bo.getArea() != null) {
                bo.setPartners(areaJoinService.selectListByAreaId(bo.getArea().getId(), AreaJoinType.PARTNER.getCode()));
            }
        }

        // 生成订单
        VipOrder order = this.generateOrder(bo);
        bo.setOrder(order);

        // 查询渠道支付列表
        bo.setChannelPayList(channelService.selectPayChannelList(order.getUserId(), order.getVipAreaId(), dto.getAppId(), ChannelBstType.VIP_ORDER.getCode()));

        return bo;
    }

    /**
     * 生成订单
     * @param bo
     * @return
     */
    private VipOrder generateOrder(VipOrderCreateBO bo) {
        VipOrder order = new VipOrder();

        // 设置基本信息
        order.setStatus(VipOrderStatus.WAIT_PAY.getCode());

        // 用户信息
        UserVO user = bo.getUser();
        if (user != null) {
            order.setUserId(user.getUserId());
        }

        // VIP信息
        VipVO vip = bo.getVip();
        if (vip != null) {
            order.setVipId(vip.getId());
            order.setVipName(vip.getName());
            order.setVipAreaId(vip.getAreaId());
            order.setVipType(vip.getType());
            order.setVipPrice(vip.getPrice());
            order.setVipDiscountValue(vip.getDiscountValue());
            order.setVipValidDays(vip.getValidDays().longValue());
            order.setVipEnableLimit(vip.getEnableLimit());
            order.setVipLimitTotal(vip.getLimitTotal());
            order.setVipLimitCount(vip.getLimitCount());
            order.setVipLimitRound(vip.getLimitRound());
            order.setVipLimitUnit(vip.getLimitUnit());
            order.setVipMinAmount(vip.getMinAmount());
        }

        return order;
    }

}
