package com.gic.haoban.manage.service.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.CollectionUtil;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.GICMQClientUtil;
import com.gic.dubbo.entity.ProviderLocalTag;
import com.gic.haoban.manage.api.constants.Manage3Constants;
import com.gic.haoban.manage.api.dto.QwFrientNoticeDTO;
import com.gic.haoban.manage.api.dto.QywxErrorLogDTO;
import com.gic.haoban.manage.service.pojo.DealQywxExternalUserPojo;
import com.gic.mq.sdk.GicMQClient;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.api.dto.qdto.QywxErrorLogListQDTO;
import com.gic.haoban.manage.service.dao.mapper.TabQywxErrorLogMapper;
import com.gic.haoban.manage.service.entity.TabQywxErrorLog;
import com.gic.haoban.manage.service.pojo.bo.TabQywxErrorLogBO;
import com.gic.haoban.manage.service.service.QywxErrorLogService;

/**
 * 企微报错日志(TabQywxErrorLog)表服务实现类
 *
 * @author mozhu
 * @date 2022-04-18 15:28:02
 */
@Service
public class TabQywxErrorLogServiceImpl implements QywxErrorLogService {
    private static final Logger log = LogManager.getLogger(QywxErrorLogService.class);

    @Autowired
    private TabQywxErrorLogMapper tabQywxErrorLogMapper;

    private static GicMQClient clientInstance = GICMQClientUtil.getClientInstance();

    @Override
    public TabQywxErrorLogBO insert(TabQywxErrorLogBO tabQywxErrorLogBO) {
        String wxEnterpriseId = tabQywxErrorLogBO.getWxEnterpriseId();
        if (StringUtils.isBlank(wxEnterpriseId)) {
            return null;
        }
        tabQywxErrorLogBO.setId(UniqueIdUtils.uniqueLong());
        tabQywxErrorLogBO.setCreateTime(new Date());
        this.tabQywxErrorLogMapper.insert(EntityUtil.changeEntityByOrika(TabQywxErrorLog.class, tabQywxErrorLogBO));
        return tabQywxErrorLogBO;
    }

    @Override
    public List<TabQywxErrorLog> queryListByPage(QywxErrorLogListQDTO qywxErrorLogListQDTO) {
        return this.tabQywxErrorLogMapper.queryListByPage(qywxErrorLogListQDTO);
    }

    @Override
    public void addFriendErrLog(String wxEnterpriseId, String msg, int type , String externalUserId) {
        QywxErrorLogDTO errLog = new QywxErrorLogDTO() ;
        errLog.setWxEnterpriseId(wxEnterpriseId);
        errLog.setExternalUserId(externalUserId);
        errLog.setTraceId(ProviderLocalTag.tag.get().traceId);
        errLog.setQywxErrorType(type);
        errLog.setScenario("加好友");
        errLog.setErrorContent(msg);
        this.putToMq(errLog);
    }

    @Override
    public void putToMq(QywxErrorLogDTO errLog) {
        try {
            clientInstance.sendMessage(Manage3Constants.QYWX_ERROR_MSG, JSON.toJSONString(errLog));
        } catch (Exception e) {
            log.error("发送消息失败：{}", e.getMessage(), e);
        }
    }

    @Override
    public void doFriendTimer(String params) {
        log.info("加好友异常重试");
        Date yesterday = DateUtil.addDay(new Date() , -1) ;
        Date start = DateUtil.getStartTimeOfDay(yesterday) ;
        Date end = DateUtil.getEndTimeOfDay(yesterday) ;
        List<TabQywxErrorLog> list = this.tabQywxErrorLogMapper.listErrorFriend(null,4, start, end) ;
        if(CollectionUtils.isNotEmpty(list)) {
            String mqName =  "dealQywxExternalUserMq2";
            for(TabQywxErrorLog tab :  list) {
                try {
                    DealQywxExternalUserPojo dealQywxExternalUserPojo = new DealQywxExternalUserPojo();
                    dealQywxExternalUserPojo.setType(DealQywxExternalUserPojo.DealType.add.getType());
                    if(StringUtils.isNotBlank(tab.getErrorContent())) {
                        dealQywxExternalUserPojo.setData(JSONObject.parseObject(tab.getErrorContent(), QwFrientNoticeDTO.class));
                        clientInstance.sendMessage(mqName, JSONObject.toJSONString(dealQywxExternalUserPojo));
                    }
                } catch (Exception e) {
                    log.info("异步处理异常：{}", e);
                }
            }
        }
    }
}
