package com.trade.service.tradingPlatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trade.common.utils.ConstantUtil;
import com.trade.common.utils.LoggerUtil;
import com.trade.common.utils.bean.BeanCopyUtils;
import com.trade.common.utils.bean.BeanUtils;
import com.trade.service.DictConstants;
import com.trade.service.filestorage.service.BusinessRelateFileBaseService;
import com.trade.service.tradingPlatform.domain.TradingPlatformInsurance;
import com.trade.service.tradingPlatform.domain.TradingPlatformQualification;
import com.trade.service.tradingPlatform.dto.*;
import com.trade.service.tradingPlatform.mapper.TradingPlatformMapper;
import com.trade.service.tradingPlatform.domain.TradingPlatform;
import com.trade.service.tradingPlatform.service.TradingPlatformBaseService;
import com.trade.service.tradingPlatform.service.TradingPlatformInsuranceBaseService;
import com.trade.service.tradingPlatform.service.TradingPlatformQualificationBaseService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 贸易平台基础服务实现类
 *
 * @author hlb
 * @since 2024-06-05 17:00:03
 */
@Service("tradingPlatformBaseService")
public class TradingPlatformBaseServiceImpl extends ServiceImpl<TradingPlatformMapper, TradingPlatform> implements TradingPlatformBaseService {
    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);

    @Resource
    private TradingPlatformMapper tradingPlatformMapper;
    @Resource
    private TradingPlatformQualificationBaseService tradingPlatformQualificationBaseService;
    @Resource
    private TradingPlatformInsuranceBaseService tradingPlatformInsuranceBaseService;
    @Resource
    private BusinessRelateFileBaseService businessRelateFileBaseService;

    @Override
    public List<TradingPlatformBaseInfo> findBySearch(TradingPlatformSearchDto search) {
        return tradingPlatformMapper.findBySearch(search);
    }

    @Override
    public TradingPlatformDto findDetailInfoById(Long id) {
        TradingPlatform tradingPlatform = this.getById(id);
        if (tradingPlatform == null) {
            return null;
        }
        TradingPlatformDto tradingPlatformDto = BeanCopyUtils.copy(tradingPlatform, TradingPlatformDto.class);
        if (tradingPlatformDto == null) {
            return null;
        }
        if (DictConstants.SYS_YES.equalsIgnoreCase(tradingPlatform.getHasSinosureInsurance())) {
            TradingPlatformInsurance platformInsurance = tradingPlatformInsuranceBaseService.lambdaQuery()
                    .eq(TradingPlatformInsurance::getPlatformId, tradingPlatform.getId())
                    .orderByDesc(TradingPlatformInsurance::getGmtCreate)
                    .last("LIMIT 1").one();
            if (platformInsurance != null) {
                TradingPlatformInsuranceDto platformInsuranceDto = BeanCopyUtils.copy(platformInsurance, TradingPlatformInsuranceDto.class);
                tradingPlatformDto.setInsurance(platformInsuranceDto);
            }
        }
        TradingPlatformQualification platformQualification = tradingPlatformQualificationBaseService.lambdaQuery().eq(TradingPlatformQualification::getPlatformId, tradingPlatform.getId())
                .orderByDesc(TradingPlatformQualification::getGmtCreate)
                .last("LIMIT 1").one();
        if (platformQualification != null) {
            TradingPlatformQualificationDto platformQualificationDto = new TradingPlatformQualificationDto();
            BeanUtils.copyProperties(platformQualification, platformQualificationDto, "hisExportCommodityClass");
            platformQualificationDto.setHisExportCommodityClass(Arrays.stream(platformQualification.getHisExportCommodityClass().split(",")).collect(Collectors.toList()));
            businessRelateFileBaseService.fillBizObjFiles(platformQualificationDto, tradingPlatformDto.getId(), ConstantUtil.TRADING_PLATFORM, null);

            tradingPlatformDto.setQualification(platformQualificationDto);
        }

        return tradingPlatformDto;
    }
}

