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

import com.jeesharp.business.modules.common.enums.DisabledEnabledEnum;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallSpecConvert;
import com.jeesharp.business.modules.mall.dto.MallSpecSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSpec;

import com.jeesharp.business.modules.mall.service.MallSpecService;
import com.jeesharp.business.modules.mall.vo.MallSpecInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSpecListVO;
import com.jeesharp.business.modules.mall.vo.MallSpecQueryVO;
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;

/**
 * 规格名应用服务。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallSpecApplicationService {

    private final MallSpecService mallSpecService;

    public MallSpecInfoVO get(String id) {
        return MallSpecConvert.INSTANCE.toInfoVO(mallSpecService.get(id));
    }

    public PageRespVO<MallSpecListVO> findPage(Page<MallSpec> page, MallSpecQueryVO query) {
        Page<MallSpec> result = mallSpecService.findPage(page, query);
        return MallSpecConvert.INSTANCE.toPageRespVO(result);
    }

    /**
     * 建品可选规格维度：平台已启用规格（任意租户只读）。
     */
    public List<MallSpecListVO> listForPublish() {
        return MallSpecConvert.INSTANCE.toListVO(mallSpecService.listPlatformEnabledForPublish());
    }

    @Transactional(readOnly = false)
    public void save(MallSpecSaveDTO dto) {
        MallSpec entity = MallSpecConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId()) && entity.getStatus() == null) {
            entity.setStatus(DisabledEnabledEnum.ENABLED.getValue());
        }
        if (StrUtil.isBlank(dto.getId())) {
            mallSpecService.createSpec(entity);
            return;
        }
        mallSpecService.updateSpec(entity);
    }

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