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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallSpuTagConvert;
import com.jeesharp.business.modules.mall.dto.MallSpuTagSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSpuTag;
import com.jeesharp.business.modules.mall.service.MallSpuTagService;
import com.jeesharp.business.modules.mall.vo.MallSpuTagInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSpuTagListVO;
import com.jeesharp.business.modules.mall.vo.MallSpuTagQueryVO;
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;

/**
 * SPU 标签关联应用服务。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallSpuTagApplicationService {

    private final MallSpuTagService mallSpuTagService;

    public MallSpuTagInfoVO get(String id) {
        return MallSpuTagConvert.INSTANCE.toInfoVO(mallSpuTagService.get(id));
    }

    public PageRespVO<MallSpuTagListVO> findPage(Page<MallSpuTag> page, MallSpuTagQueryVO query) {
        Page<MallSpuTag> result = mallSpuTagService.findPage(page, query);
        return MallSpuTagConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallSpuTagSaveDTO dto) {
        MallSpuTag entity = MallSpuTagConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallSpuTagService.createSpuTag(entity);
            return;
        }
        mallSpuTagService.updateSpuTag(entity);
    }

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