package com.trade.service.tradingplatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trade.common.utils.LoggerUtil;
import com.trade.common.utils.bean.BeanCopyUtils;
import com.trade.service.DictConstants;
import com.trade.service.TransStruct;
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.List;

/**
 * 贸易平台基础服务实现类
 *
 * @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 TransStruct transStruct;

    @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 = transStruct
                    .transTradingPlatformQualification2Dto(platformQualification);
            tradingPlatformDto.setQualification(platformQualificationDto);
        }
        return tradingPlatformDto;
    }

    @Override
    public TradingPlatform findByCertNo(String certNo) {
        return this.lambdaQuery().eq(TradingPlatform::getCertNo, certNo).one();
    }
}
