package com.trade.client.tradingplatform.service;

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

import javax.annotation.Resource;

import com.trade.client.security.utils.UserUtils;
import com.trade.client.tradingplatform.TradingPlatformService;
import com.trade.common.exception.ServiceException;
import com.trade.common.utils.bean.BeanCopyUtils;
import com.trade.service.enterprise.customer.domain.Enterprise;
import com.trade.service.enterprise.customer.service.EnterpriseBaseService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import com.trade.common.constant.HttpStatus;
import com.trade.common.utils.LoggerUtil;
import com.trade.service.tradingplatform.domain.TradingPlatform;
import com.trade.service.tradingplatform.dto.*;
import com.trade.service.tradingplatform.service.TradingPlatformBaseService;

import cn.hutool.core.util.StrUtil;

/**
 * 贸易平台服务实现类
 *
 * @author hlb
 * @since 2024-06-05 17:00:44
 */
@Service("tradingPlatformService")
public class TradingPlatformServiceImpl implements TradingPlatformService {

    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);
    @Resource
    private TradingPlatformBaseService tradingPlatformBaseService;
    @Resource
    private EnterpriseBaseService enterpriseBaseService;

    @Override
    public List<TradingPlatformDto> queryCanMatchTradingPlatforms() {
        Long customerUserId = UserUtils.getLoginUser().getCustomerUserId();
        Enterprise enterprise = enterpriseBaseService.getEnterpriseByCustomerId(customerUserId);
        if (enterprise == null) {
            throw new ServiceException("您没有关联的企业信息，请联系管理员", HttpStatus.BAD_REQUEST);
        }
        if (StrUtil.isEmpty(enterprise.getProxyDistrictCode())) {
            return new ArrayList<>();
        }
        return tradingPlatformBaseService.lambdaQuery()
                .eq(TradingPlatform::getDistrictCode, enterprise.getProxyDistrictCode())
                .eq(TradingPlatform::getDraft, false).list().stream()
                .map(o -> BeanCopyUtils.copy(o, TradingPlatformDto.class)).collect(Collectors.toList());
    }
}
