package com.friday.custody.service.trustService.citicZhongXin.utils;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.friday.custody.service.trustService.citicZhongXin.common.UrlFrequencyEnum;
import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.InvokeFrequencyEntity;
import com.friday.custody.service.basicService.impl.InvokeFrequencyServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

/**
 * @Description
 * @Author wulifeng
 * @Date: 2022/4/6
 */
@Slf4j
@Service("frequencyUtil")
public class FrequencyUtil {
    @Autowired
    InvokeFrequencyServiceImpl invokeFrequencyService;

    public static final String invoke_time_pattern = "yyyyMMdd";

    public synchronized boolean judgeFrequency(String invokeType, TenantEscrowAuthorizationInfoDto tenant) {
        log.info("judgeFrequency_invokeType={},tenant={}",invokeType,tenant);
        Map<String,Integer> urlFrequency = UrlFrequencyEnum.getUrlFrequencyMap();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(invoke_time_pattern);

        String invokeDate = dateTimeFormatter.format(localDate);
        String uniqueKey = invokeDate + invokeType + tenant.getTenantName() + tenant.getTrustNo() +tenant.getAppKey();
        Date now = new Date();
        InvokeFrequencyEntity invokeFrequencyEntity = invokeFrequencyService.queryByUniqueKey(uniqueKey);
        if(ObjectUtils.isNull(invokeFrequencyEntity)){
            log.info("今日第一次调用{},调用日期为{}",invokeType,invokeDate);
            InvokeFrequencyEntity build = InvokeFrequencyEntity.builder()
                    .invokeDate(invokeDate)
                    .invokeTimes(1)
                    .invokeType(invokeType)
                    .trustNo(tenant.getTrustNo())
                    .admin(tenant.getTenantName())
                    .uniqueKey(uniqueKey)
                    .createTime(now)
                    .updateTime(now)
                    .build();

            invokeFrequencyService.saveData(build);
            return true;
        }else{
            Integer invokeTimes = invokeFrequencyEntity.getInvokeTimes();
            if(invokeTimes==null){
                invokeTimes = 0;
            }
            int thisTime = invokeTimes + 1;
            log.info("今日第{}次调用{},调用日期为{}",thisTime,invokeType,invokeDate);

            Integer integer = urlFrequency.get(invokeType);
            if(thisTime > integer){ // 超过调用上限
                log.error("接口为{}，日期为{}，超过调用上限{}",invokeType,integer,integer);
                return false;
            }else{ // 未超过上限
                invokeFrequencyEntity.setInvokeTimes(thisTime);
                invokeFrequencyEntity.setUpdateTime(now);
                invokeFrequencyService.updateByUniqueKey(invokeFrequencyEntity);
                return true;
            }
        }
    }
}
