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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallAddPurchaseConvert;
import com.jeesharp.business.modules.mall.dto.MallAddPurchaseSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallAddPurchase;
import com.jeesharp.business.modules.mall.service.MallAddPurchaseService;
import com.jeesharp.business.modules.mall.vo.MallAddPurchaseInfoVO;
import com.jeesharp.business.modules.mall.vo.MallAddPurchaseListVO;
import com.jeesharp.business.modules.mall.vo.MallAddPurchaseQueryVO;
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.util.List;

/**
 * 加价购配置 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallAddPurchaseApplicationService {

    private final MallAddPurchaseService mallAddPurchaseService;

    public MallAddPurchaseInfoVO get(String id) {
        return MallAddPurchaseConvert.INSTANCE.toInfoVO(mallAddPurchaseService.get(id));
    }

    public PageRespVO<MallAddPurchaseListVO> findPage(Page<MallAddPurchase> page, MallAddPurchaseQueryVO query) {
        Page<MallAddPurchase> result = mallAddPurchaseService.findPage(page, query);
        return MallAddPurchaseConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallAddPurchaseSaveDTO dto) {
        MallAddPurchase entity = MallAddPurchaseConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallAddPurchaseService.createAddPurchase(entity);
            return;
        }
        mallAddPurchaseService.updateAddPurchase(entity);
    }

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

    /**
     * 获取SKU的可加价购选项
     */
    public List<MallAddPurchase> getAddPurchaseOptions(String mainSkuId) {
        return mallAddPurchaseService.getAddPurchaseOptions(mainSkuId);
    }

    /**
     * 获取所有可用的加价购配置
     */
    public List<MallAddPurchase> getAvailableAddPurchases() {
        return mallAddPurchaseService.getAvailableAddPurchases();
    }
}
