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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallPresaleConvert;
import com.jeesharp.business.modules.mall.dto.MallPresaleSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallPresale;
import com.jeesharp.business.modules.mall.service.MallPresaleService;
import com.jeesharp.business.modules.mall.vo.MallPresaleInfoVO;
import com.jeesharp.business.modules.mall.vo.MallPresaleListVO;
import com.jeesharp.business.modules.mall.vo.MallPresaleQueryVO;
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;

/**
 * 预售配置 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallPresaleApplicationService {

    private final MallPresaleService mallPresaleService;

    public MallPresaleInfoVO get(String id) {
        return MallPresaleConvert.INSTANCE.toInfoVO(mallPresaleService.get(id));
    }

    public PageRespVO<MallPresaleListVO> findPage(Page<MallPresale> page, MallPresaleQueryVO query) {
        Page<MallPresale> result = mallPresaleService.findPage(page, query);
        return MallPresaleConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallPresaleSaveDTO dto) {
        MallPresale entity = MallPresaleConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallPresaleService.createPresale(entity);
            return;
        }
        mallPresaleService.updatePresale(entity);
    }

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

    /**
     * 根据SPU ID获取预售配置
     */
    public MallPresale getBySpuId(String spuId) {
        return mallPresaleService.getBySpuId(spuId);
    }

    /**
     * 检查商品是否在预售中
     */
    public boolean isInPresalePeriod(String spuId) {
        return mallPresaleService.isInPresalePeriod(spuId);
    }

    /**
     * 检查是否可以支付尾款
     */
    public boolean canPayTail(String spuId) {
        return mallPresaleService.canPayTail(spuId);
    }
}
