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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallBargainActivityConvert;
import com.jeesharp.business.modules.mall.dto.MallBargainActivitySaveDTO;
import com.jeesharp.business.modules.mall.entity.MallBargainActivity;
import com.jeesharp.business.modules.mall.service.MallBargainActivityService;
import com.jeesharp.business.modules.mall.vo.MallBargainActivityInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBargainActivityListVO;
import com.jeesharp.business.modules.mall.vo.MallBargainActivityQueryVO;
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 MallBargainActivityApplicationService {

    private final MallBargainActivityService mallBargainActivityService;

    public MallBargainActivityInfoVO get(String id) {
        return mallBargainActivityService.getActivity(id);
    }

    public PageRespVO<MallBargainActivityListVO> findPage(Page<MallBargainActivity> page, MallBargainActivityQueryVO query) {
        return mallBargainActivityService.findActivityPage(page, query);
    }

    @Transactional(readOnly = false)
    public void save(MallBargainActivitySaveDTO dto) {
        MallBargainActivity entity = MallBargainActivityConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallBargainActivityService.createActivity(entity);
            return;
        }
        mallBargainActivityService.updateActivity(entity);
    }

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

    /**
     * 获取进行中的砍价活动
     */
    public List<MallBargainActivity> getActiveActivities() {
        return mallBargainActivityService.getActiveActivities();
    }

    /**
     * 根据SKU获取砍价活动
     */
    public MallBargainActivity getBySkuId(String skuId) {
        return mallBargainActivityService.getBySkuId(skuId);
    }
}
