package com.sonic.lion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sonic.lion.dao.AppStoreProductDao;
import com.sonic.lion.dao.PayChargeDao;
import com.sonic.lion.domain.entity.AppStoreProduct;
import com.sonic.lion.domain.entity.PayCharge;
import com.sonic.lion.domain.enums.ProductType;
import com.sonic.lion.domain.input.SubChargeProductListInput;
import com.sonic.lion.domain.output.SubProductListOutput;
import com.sonic.lion.enums.MemberType;
import com.sonic.lion.service.PayChargeService;
import com.sonic.lion.utils.BeanConvert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: code
 * @date: 2025/06/24
 * @Description:
 * @version: 1.0.0
 */
@Service
public class PayChargeServiceImpl implements PayChargeService {

    @Autowired
    private PayChargeDao payChargeDao;

    @Autowired
    private AppStoreProductDao productDao;

    @Override
    public PayCharge getByBundleIdAndProductIdAndPlatform(String bundleId, String productId, String platform) {
        if (StringUtils.isBlank(bundleId) || StringUtils.isBlank(productId)) {
            return null;
        }
        LambdaQueryWrapper<PayCharge> queryWrapper = Wrappers.<PayCharge>lambdaQuery()
                .eq(PayCharge::getBundleId, bundleId)
                .eq(PayCharge::getProductId, productId)
                .eq(PayCharge::getPlatform, platform);
        return payChargeDao.selectOne(queryWrapper);
    }

    @Override
    public PayCharge getByBundleIdAndProductIdAndPlatform(String productId, String platform) {
        if (StringUtils.isBlank(productId)) {
            return null;
        }
        LambdaQueryWrapper<PayCharge> queryWrapper = Wrappers.<PayCharge>lambdaQuery()
                .eq(PayCharge::getProductId, productId)
                .eq(PayCharge::getPlatform, platform);
        return payChargeDao.selectOne(queryWrapper);
    }

    @Override
    public List<PayCharge> list(String platform) {
        LambdaQueryWrapper<PayCharge> queryWrapper = Wrappers.<PayCharge>lambdaQuery()
                .eq(PayCharge::getPlatform, platform);
        queryWrapper = queryWrapper.eq(PayCharge::getDeleted, false).orderByAsc(PayCharge::getChargeAmount);
        return payChargeDao.selectList(queryWrapper);
    }

    @Override
    public List<PayCharge> list(String platform, String version) {
        LambdaQueryWrapper<PayCharge> queryWrapper = Wrappers.<PayCharge>lambdaQuery()
                .eq(PayCharge::getPlatform, platform)
                .eq(PayCharge::getVersion, version)
                .eq(PayCharge::getDeleted, false).orderByAsc(PayCharge::getChargeAmount);
        return payChargeDao.selectList(queryWrapper);
    }

    @Override
    public List<SubProductListOutput> subProductList(SubChargeProductListInput input) {
        List<AppStoreProduct> vipList = listSubscriptionProducts(input.getPlatform(), input.getVersion(), MemberType.VIP);
        return BeanConvert.copeList(vipList, SubProductListOutput.class);
    }

    /**
     * 获取订阅产品
     * @param platform
     * @param version
     * @param memberType
     * @return
     */
    private List<AppStoreProduct> listSubscriptionProducts(String platform, String version , MemberType memberType) {
        LambdaQueryWrapper<AppStoreProduct> queryWrapper = Wrappers.<AppStoreProduct>lambdaQuery()
                .eq(AppStoreProduct::getPlatform, platform)
                .eq(AppStoreProduct::getVersion, version)
                .eq(AppStoreProduct::getProductType, ProductType.SUBSCRIPTION)
                .eq(AppStoreProduct::getMemberType, memberType)
                .eq(AppStoreProduct::getDeleted, false).orderByAsc(AppStoreProduct::getChargeAmount);
        return productDao.selectList(queryWrapper);
    }

    @Override
    public AppStoreProduct getAppStoreProduct(String productId, String platform) {
        LambdaQueryWrapper<AppStoreProduct> queryWrapper = Wrappers.<AppStoreProduct>lambdaQuery()
                .eq(AppStoreProduct::getDeleted, false)
                .eq(AppStoreProduct::getProductId, productId)
                .eq(AppStoreProduct::getPlatform, platform);
        return productDao.selectOne(queryWrapper);
    }


    @Override
    public PayCharge getByProductId(String productId) {
        return getByProductId(productId, null);
    }

    @Override
    public PayCharge getByProductId(String productId, String platform) {
        if (StringUtils.isBlank(productId)) {
            return null;
        }
        LambdaQueryWrapper<PayCharge> queryWrapper = Wrappers.<PayCharge>lambdaQuery()
                .eq(PayCharge::getProductId, productId)
                .eq(StringUtils.isNotEmpty(platform), PayCharge::getPlatform, platform);
        return payChargeDao.selectOne(queryWrapper);
    }
}
