package com.jeesharp.business.modules.mall.service.impl;

import com.jeesharp.business.modules.mall.entity.MallBrand;
import com.jeesharp.business.modules.mall.mapper.MallBrandMapper;
import com.jeesharp.business.modules.mall.service.MallBrandService;
import com.jeesharp.business.modules.common.constants.PlatformTenantConstants;
import com.jeesharp.business.modules.mall.vo.MallBrandQueryVO;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品品牌领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@Transactional(readOnly = true)
public class MallBrandServiceImpl extends CrudService<MallBrandMapper, MallBrand, MallBrandQueryVO> implements MallBrandService {

    private static final String IDS_SEPARATOR = ",";

    @Override
    @Transactional(readOnly = false)
    public void createBrand(MallBrand entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateBrand(MallBrand entity) {
        update(entity);
    }

    @Override
    public List<MallBrand> listByIds(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return List.of();
        }
        List<String> idList = ids.stream()
            .filter(StrUtil::isNotBlank)
            .map(String::trim)
            .distinct()
            .collect(Collectors.toList());
        if (idList.isEmpty()) {
            return List.of();
        }
        return getBaseMapper().selectByIds(PlatformTenantConstants.PLATFORM_TENANT_CODE, idList);
    }

    @Override
    public List<MallBrand> listPlatformEnabledForPublish() {
        return getBaseMapper().selectEnabledByTenantCode(PlatformTenantConstants.PLATFORM_TENANT_CODE);
    }
}
