package com.crm.globex.configuration;

import com.alibaba.fastjson.JSON;
import com.crm.globex.service.SysConfigService;
import com.crm.rely.backend.core.entity.manager.ErrorManager;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @Author: houn
 * @Date: 2020/8/19 17:57
 * @Description:
 */
@Component
public class MangerErrorSendEmailComponent {
    private static Object obLock = new Object();
    @Autowired
    private EmailService emailService;

    @Autowired
    private SysConfigService sysConfigService;

    public static Queue<ErrorManager> errorManagerQueue = new ConcurrentLinkedQueue<>();

    public void addErrorInfo(Integer errorCode, String type, String serial, Object contentObject) {
        synchronized (obLock) {
            ErrorManager errorManager = new ErrorManager();

            errorManager.setSerial(serial);
            errorManager.setContent(JSON.toJSONString(contentObject));
            errorManager.setErrorCode(errorCode);
            errorManager.setTime(new Date());
            errorManager.setType(type);
            errorManager.setErrorNumber(1);

            errorManagerQueue.offer(errorManager);
        }
    }

    @Scheduled(fixedDelay = 60 * 1000)
    private void sendErrorEmail() throws ServiceException {
//        if (errorManagerQueue == null || errorManagerQueue.isEmpty()) {
//            return;
//        }
//        Map<String, ErrorManager> errorManagerMap = new HashMap<>();
//        ErrorManager errorManager = errorManagerQueue.poll();
//
//        while (errorManager != null) {
//            String key = errorManager.getSerial() + errorManager.getErrorCode();
//            ErrorManager error = errorManagerMap.getOrDefault(key, null);
//            if (error != null) {
//                error.setErrorNumber(error.getErrorNumber() + 1);
//            } else {
//                error = errorManager;
//            }
//            errorManagerMap.put(key, error);
//            errorManager = errorManagerQueue.poll();
//        }
//
//        Collection<ErrorManager> errorManagers = errorManagerMap.values();
//
//        if (errorManagers == null || errorManagers.size() <= 0) {
//            return;
//        }
//
//        SysConfigTable sysConfigTable = sysConfigService.getByCode(ConfigConstants.MANAGER_ERROR_EMAIL);
//        if (sysConfigTable == null || Strings.isNullOrEmpty(sysConfigTable.getValue())) {
//            return;
//        }
//        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
//        sysEmailSendEntity.setAddIp("127.0.0.1");
//        sysEmailSendEntity.setAddTime(new Date());
//        sysEmailSendEntity.setAddUser(-1L);
//
//        StringBuffer contentBuffer = new StringBuffer();
//
//        for (ErrorManager errorInfo : errorManagers) {
//            contentBuffer.append(JSON.toJSONString(errorInfo) + "\n\n");
//        }
//
//        sysEmailSendEntity.setSubject("Manager Error");
//        sysEmailSendEntity.setContent(contentBuffer.toString());
//        sysEmailSendEntity.setUsers(sysConfigTable.getValue());
//        sysEmailSendEntity.setEmailConfigId(4L);
//
//        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.TO_MANAGER_ERROR_INFO);
//        emailService.sendEmail(sysEmailSendEntity);
    }
}
