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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallSpuSpecConvert;
import com.jeesharp.business.modules.mall.dto.MallSpuSpecSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSpuSpec;
import com.jeesharp.business.modules.mall.service.MallSpuSpecService;
import com.jeesharp.business.modules.mall.vo.MallSpuSpecInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSpuSpecListVO;
import com.jeesharp.business.modules.mall.vo.MallSpuSpecQueryVO;
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;

/**
 * SPU规格关联 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallSpuSpecApplicationService {

    private final MallSpuSpecService mallSpuSpecService;

    public MallSpuSpecInfoVO get(String id) {
        return MallSpuSpecConvert.INSTANCE.toInfoVO(mallSpuSpecService.get(id));
    }

    public PageRespVO<MallSpuSpecListVO> findPage(Page<MallSpuSpec> page, MallSpuSpecQueryVO query) {
        Page<MallSpuSpec> result = mallSpuSpecService.findPage(page, query);
        return MallSpuSpecConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallSpuSpecSaveDTO dto) {
        MallSpuSpec entity = MallSpuSpecConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallSpuSpecService.createSpuSpec(entity);
            return;
        }
        mallSpuSpecService.updateSpuSpec(entity);
    }

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

    /**
     * 获取SPU的规格列表
     */
    public List<MallSpuSpec> getSpuSpecs(String spuId) {
        return mallSpuSpecService.getSpuSpecs(spuId);
    }

    /**
     * 批量保存SPU规格关联
     */
    @Transactional(readOnly = false)
    public void batchSaveSpuSpecs(String spuId, List<String> specIds) {
        mallSpuSpecService.batchSaveSpuSpecs(spuId, specIds);
    }

    /**
     * 删除SPU的所有规格关联
     */
    @Transactional(readOnly = false)
    public void deleteSpuSpecs(String spuId) {
        mallSpuSpecService.deleteSpuSpecs(spuId);
    }
}
