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

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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.entity.MallSpec;
import com.jeesharp.business.modules.mall.entity.MallSpecValue;

import com.jeesharp.business.modules.mall.mapper.MallSpecMapper;
import com.jeesharp.business.modules.mall.mapper.MallSpecValueMapper;
import com.jeesharp.business.modules.mall.service.MallSpecValueService;
import com.jeesharp.business.modules.common.constants.PlatformTenantConstants;
import com.jeesharp.business.modules.mall.vo.MallSpecValueQueryVO;
import com.jeesharp.framework.exception.BizErrorException;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.utils.WebFrameworkUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 规格值领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallSpecValueServiceImpl extends CrudService<MallSpecValueMapper, MallSpecValue, MallSpecValueQueryVO>
    implements MallSpecValueService {

    private final MallSpecMapper mallSpecMapper;

    @Override
    @Transactional(readOnly = false)
    public void createSpecValue(MallSpecValue entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateSpecValue(MallSpecValue entity) {
        update(entity);
    }

    @Override
    public List<MallSpecValue> listEnabledForPublish(String specId) {
        if (StrUtil.isBlank(specId)) {
            return List.of();
        }
        String tenantCode = requireLoginTenantCode();
        return getBaseMapper().selectEnabledBySpecIdAndTenantCode(tenantCode, specId.trim());
    }

    @Override
    @Transactional(readOnly = false)
    public MallSpecValue createOrGetForPublish(String specId, String specValue) {
        if (StrUtil.isBlank(specId)) {
            throw new BizErrorException("规格维度不能为空");
        }
        if (StrUtil.isBlank(specValue)) {
            throw new BizErrorException("规格值不能为空");
        }
        String sid = specId.trim();
        String text = specValue.trim();
        if (text.length() > 64) {
            throw new BizErrorException("规格值长度不能超过64个字符");
        }
        ensurePlatformSpecDimension(sid);
        String tenantCode = requireLoginTenantCode();
        MallSpecValue existing = getBaseMapper().selectBySpecIdValueAndTenantCode(tenantCode, sid, text);
        if (existing != null) {
            if (!DisabledEnabledEnum.ENABLED.getValue().equals(existing.getStatus())) {
                existing.setStatus(DisabledEnabledEnum.ENABLED.getValue());
                update(existing);
            }
            return existing;
        }
        MallSpecValue entity = new MallSpecValue();
        entity.setSpecId(sid);
        entity.setSpecValue(text);
        entity.setSortOrder(0);
        entity.setStatus(DisabledEnabledEnum.ENABLED.getValue());
        createSpecValue(entity);
        return entity;
    }

    @Override
    public List<MallSpecValue> listByIdsForCurrentTenant(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().selectByIdsAndTenantCode(requireLoginTenantCode(), idList);
    }

    private void ensurePlatformSpecDimension(String specId) {
        MallSpec spec = mallSpecMapper.get(specId);
        if (spec == null) {
            throw new BizErrorException("规格维度不存在");
        }
        if (!PlatformTenantConstants.isPlatformTenantCode(spec.getTenantCode())) {
            throw new BizErrorException("规格维度无效，请使用平台维护的销售规格");
        }
        if (!DisabledEnabledEnum.ENABLED.getValue().equals(spec.getStatus())) {
            throw new BizErrorException("规格维度已禁用");
        }
    }

    private static String requireLoginTenantCode() {
        String tenantCode = WebFrameworkUtils.getLoginUserTenantCode();
        if (StrUtil.isBlank(tenantCode)) {
            throw new BizErrorException("无法识别当前租户");
        }
        return tenantCode.trim();
    }
}
