package com.jeesharp.business.modules.mall.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallSkuLevelPriceConvert;
import com.jeesharp.business.modules.mall.dto.MallSkuLevelPriceSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSkuLevelPrice;
import com.jeesharp.business.modules.mall.service.MallSkuLevelPriceService;
import com.jeesharp.business.modules.mall.vo.MallSkuLevelPriceInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSkuLevelPriceListVO;
import com.jeesharp.business.modules.mall.vo.MallSkuLevelPriceQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 会员价 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallSkuLevelPriceApplicationService {

    private final MallSkuLevelPriceService mallSkuLevelPriceService;

    public MallSkuLevelPriceInfoVO get(String id) {
        return MallSkuLevelPriceConvert.INSTANCE.toInfoVO(mallSkuLevelPriceService.get(id));
    }

    public PageRespVO<MallSkuLevelPriceListVO> findPage(Page<MallSkuLevelPrice> page, MallSkuLevelPriceQueryVO query) {
        Page<MallSkuLevelPrice> result = mallSkuLevelPriceService.findPage(page, query);
        return MallSkuLevelPriceConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallSkuLevelPriceSaveDTO dto) {
        MallSkuLevelPrice entity = MallSkuLevelPriceConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallSkuLevelPriceService.createLevelPrice(entity);
            return;
        }
        mallSkuLevelPriceService.updateLevelPrice(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        mallSkuLevelPriceService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 获取 SKU 的会员等级价格
     */
    public BigDecimal getMemberPrice(String skuId, String memberLevelCode) {
        return mallSkuLevelPriceService.getMemberPrice(skuId, memberLevelCode);
    }

    /**
     * 批量获取 SKU 的会员等级价格
     */
    public List<MallSkuLevelPrice> getMemberPricesBySku(String skuId) {
        return mallSkuLevelPriceService.getMemberPricesBySku(skuId);
    }
}
