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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallTagConvert;
import com.jeesharp.business.modules.mall.dto.MallTagSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallTag;
import com.jeesharp.business.modules.mall.service.MallTagService;
import com.jeesharp.business.modules.mall.vo.MallTagInfoVO;
import com.jeesharp.business.modules.mall.vo.MallTagListVO;
import com.jeesharp.business.modules.mall.vo.MallTagQueryVO;
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;

@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallTagApplicationService {

    private final MallTagService mallTagService;

    public MallTagInfoVO get(String id) {
        return MallTagConvert.INSTANCE.toInfoVO(mallTagService.get(id));
    }

    public PageRespVO<MallTagListVO> findPage(Page<MallTag> page, MallTagQueryVO query) {
        Page<MallTag> result = mallTagService.findPage(page, query);
        return MallTagConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallTagSaveDTO dto) {
        MallTag entity = MallTagConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallTagService.createTag(entity);
            return;
        }
        mallTagService.updateTag(entity);
    }

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

    public List<MallTag> listEnabledTags() {
        return mallTagService.listEnabledTags();
    }

    /**
     * 建品可选标签：固定返回平台已启用的商品标签（商户与平台共用）。
     */
    public List<MallTagListVO> listForPublish() {
        return MallTagConvert.INSTANCE.toListVO(mallTagService.listPlatformEnabledForPublish());
    }
}
