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.DataDictionaryTrustDao;
import com.friday.custody.dto.ApiDto;
import com.friday.custody.dto.DataTrustDto;
import com.friday.custody.dto.EmailDto;
import com.friday.custody.dto.TenantInfoMonitorDto;
import com.friday.custody.entity.CustodyInterfaceStatisticsEntity;
import com.friday.custody.entity.DataDictionaryTrustEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.DataDictionaryTrustService;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import com.friday.custody.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;


@Service("dataDictionaryTrustService")
public class DataDictionaryTrustServiceImpl extends ServiceImpl<DataDictionaryTrustDao, DataDictionaryTrustEntity>
        implements DataDictionaryTrustService, DataDictionaryBasicService {
    @Autowired
    TenantInfoServiceImpl tenantInfoService;

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

        return new PageUtils(page);
    }

    @Override
    public List<DataDictionaryTrustEntity> queryByLikeValue(String value) {
        QueryWrapper<DataDictionaryTrustEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(value)) {
            queryWrapper.like("value", value);
        }
        List<DataDictionaryTrustEntity> trustEntities = baseMapper.selectList(queryWrapper);
        if (!ObjectUtils.isEmpty(trustEntities)) {
            return trustEntities;
        }
        return null;
    }

    public String selectKey(String value) {
        QueryWrapper<DataDictionaryTrustEntity> queryWrapper = new QueryWrapper<DataDictionaryTrustEntity>().eq("value", value);
        DataDictionaryTrustEntity trustEntity = baseMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isEmpty(trustEntity)) {
            return trustEntity.getTrustNo();
        }
        return null;
    }

    @Override
    public Map<String, String> queryWithDetail(DataDictionaryTypeEnum dicType, String trustNo) {
        return null;
    }

    @Override
    public Map<String, String> queryWithDetail(String trustNo) {
        return null;
    }

    @Override
    public R queryData(String type) {
        List<DataDictionaryTrustEntity> entities = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(entities)) {
            return R.ok("查询字典信息成功！")
                    .put("data", entities.stream().collect(Collectors.toMap(DataDictionaryTrustEntity::getTrustNo
                            , DataDictionaryTrustEntity::getValue)));
        }
        return R.ok("字典信息为空，请联系管理员配置！");
    }
    @Autowired
    InvokeRecordServiceImpl invokeRecordService;
    @Autowired
    DataDictionaryTrustServiceImpl dataDictionaryTrustService;
    public List<DataTrustDto> queryAllData(String trustNo) {
        //查询托管
        List<DataTrustDto> dataTrustDtos = baseMapper.queryAllData(trustNo);
        //去重
        dataTrustDtos = new ArrayList<>(new HashSet<>(dataTrustDtos));
        ArrayList<TenantInfoMonitorDto> dataTrustDtos3;
        for (int i = 0; i < dataTrustDtos.size(); i++) {
            //根据trustNo查询旗下所有管理人
            String trustNoNow = dataTrustDtos.get(i).getTrustNo();
            List<TenantInfoMonitorDto> dataTrustDtos2 = tenantInfoService.queryAdmin(trustNoNow);
            //去重
            dataTrustDtos3 = new ArrayList<>(new HashSet<>(dataTrustDtos2));
            for (int i1 = 0; i1 < dataTrustDtos3.size(); i1++) {
                String admin = dataTrustDtos3.get(i1).getAdminNo();
                List<ApiDto> tablePermissionDtos = queryApi(admin,trustNoNow);
                List<EmailDto> emailDtos = queryHasEmail(admin,trustNoNow);
                List<ApiDto> isNoApiDtos = queryIsNotApi(admin, trustNoNow);
                dataTrustDtos3.get(i1).setApiList(tablePermissionDtos);
                dataTrustDtos3.get(i1).setIsNotapiList(isNoApiDtos);
                dataTrustDtos3.get(i1).setEmailList(emailDtos);
                dataTrustDtos.get(i).setAdminInfoList(dataTrustDtos3);
            }
        }
        return dataTrustDtos;
    }
    @Autowired
    CustodyInterfaceStatisticsServiceImpl custodyInterfaceStatisticsService;
    public List<ApiDto> queryApi(String admin,String trustNo){
        ArrayList<ApiDto> arrayList = new ArrayList<>();
        List<CustodyInterfaceStatisticsEntity> entityList = custodyInterfaceStatisticsService.queryInterfaceInfo(admin,trustNo);
        if (entityList.size()>0){
            for (CustodyInterfaceStatisticsEntity statisticsEntity : entityList) {
                String interfaceName = statisticsEntity.getInterfaceName();
                String interfacePath = statisticsEntity.getInterfacePath();
                arrayList.add(new ApiDto(interfacePath,interfaceName));
            }
            return arrayList;
        }
        return null;
    }
    public List<ApiDto> queryIsNotApi(String admin,String trustNo){
        ArrayList<ApiDto> arrayList = new ArrayList<>();
        List<CustodyInterfaceStatisticsEntity> entityList = custodyInterfaceStatisticsService.queryIsNotApi(admin,trustNo);
        if (entityList.size()>0){
            for (CustodyInterfaceStatisticsEntity statisticsEntity : entityList) {
                String interfaceName = statisticsEntity.getInterfaceName();
                String interfacePath = statisticsEntity.getInterfacePath();
                arrayList.add(new ApiDto(interfacePath,interfaceName));
            }
            return arrayList;
        }
        return null;
    }
    public List<EmailDto> queryHasEmail(String admin,String trustNo){
        ArrayList<EmailDto> arrayList = new ArrayList<>();
        List<CustodyInterfaceStatisticsEntity> entityList = custodyInterfaceStatisticsService.queryHasEmail(admin,trustNo);
        if (entityList.size()>0){
            for (CustodyInterfaceStatisticsEntity entity : entityList) {
                String interfaceName = entity.getInterfaceName();
                String interfacePath = entity.getInterfacePath();
                arrayList.add(new EmailDto(interfacePath,interfaceName));
            }
            return arrayList;
        }
        return null;
    }
    public List<DataTrustDto> queryAllData(){
        List<DataTrustDto> dataTrustDtos = baseMapper.queryAllData(null);
        return dataTrustDtos;
    }

}