package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.CustodyInterfaceStatisticsDao;
import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.CustodyInterfaceStatisticsEntity;
import com.friday.custody.service.basicService.CustodyInterfaceStatisticsService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;


@Service("custodyInterfaceStatisticsService")
public class CustodyInterfaceStatisticsServiceImpl extends ServiceImpl<CustodyInterfaceStatisticsDao, CustodyInterfaceStatisticsEntity> implements CustodyInterfaceStatisticsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CustodyInterfaceStatisticsEntity> page = this.page(
                new Query<CustodyInterfaceStatisticsEntity>().getPage(params),
                new QueryWrapper<CustodyInterfaceStatisticsEntity>()
        );

        return new PageUtils(page);
    }
    public List<CustodyInterfaceStatisticsEntity> queryInterfaceInfo(String admin,String trustNo){
        QueryWrapper<CustodyInterfaceStatisticsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin",admin);
        queryWrapper.eq("trust_no",trustNo);
        queryWrapper.eq("type","1");
        queryWrapper.ne("interface_path","邮件解析");
        List<CustodyInterfaceStatisticsEntity> custodyInterfaceStatisticsEntities = baseMapper.selectList(queryWrapper);
        return custodyInterfaceStatisticsEntities;
    }
    public List<CustodyInterfaceStatisticsEntity> queryIsNotApi(String admin,String trustNo){
        List<CustodyInterfaceStatisticsEntity> entities = baseMapper.queryIsNotApi(admin, trustNo);
        return entities;
    }
    @Autowired
    TenantEscrowAuthorizationInfoServiceImpl tenantEscrowAuthorizationInfoService;
    public List<CustodyInterfaceStatisticsEntity> queryHasEmail(String admin,String trustNo){
        QueryWrapper<CustodyInterfaceStatisticsEntity> queryWrapper = new QueryWrapper<>();
        List<CustodyInterfaceStatisticsEntity> custodyInterfaceStatisticsEntities = null;
        List<TenantEscrowAuthorizationInfoDto> tenantEscrowAuthorizationInfoDto = tenantEscrowAuthorizationInfoService.queryAuthorizationByTrustNoAndTenantName(admin, trustNo);
        //去重
        ArrayList<TenantEscrowAuthorizationInfoDto> tenantEscrowAuthorizationInfoDtos = new ArrayList<>(new HashSet<>(tenantEscrowAuthorizationInfoDto));
        for (TenantEscrowAuthorizationInfoDto escrowAuthorizationInfoDto : tenantEscrowAuthorizationInfoDtos) {
            String tenantName = escrowAuthorizationInfoDto.getTenantName();
            String trustNo1 = escrowAuthorizationInfoDto.getTrustNo();
            queryWrapper.eq("trust_no",trustNo1);
            queryWrapper.eq("interface_path","邮件解析");
            custodyInterfaceStatisticsEntities = baseMapper.selectList(queryWrapper);
        }

        return custodyInterfaceStatisticsEntities;
    }

}