package com.fs.app.taskService.impl;


import com.alibaba.fastjson.JSON;
import com.fs.common.core.redis.RedisCache;
import com.fs.course.domain.FsCourseWatchLog;
import com.fs.qw.domain.QwCompany;
import com.fs.qw.domain.QwUser;
import com.fs.qw.service.IQwCompanyService;
import com.fs.qw.service.impl.QwExternalContactServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Service
@AllArgsConstructor
public class AsyncCourseWatchFinishService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private IQwCompanyService iQwCompanyService;

    @Autowired
    private QwExternalContactServiceImpl qwExternalContactService;

    @Autowired
    RedisCache redisCache;

    /**
    * 异步处理完课打备注的
    */
    @Async("scheduledExecutorService")
    public void executeCourseWatchFinish(FsCourseWatchLog finishLog) {

        FsCourseWatchLog watchLog = new FsCourseWatchLog();
        watchLog.setQwExternalContactId(finishLog.getQwExternalContactId());
        watchLog.setFinishTime(finishLog.getFinishTime());
        watchLog.setQwUserId(finishLog.getQwUserId());


        QwUser qwUserByRedis = qwExternalContactService.getQwUserByRedisForId(String.valueOf(finishLog.getQwUserId()));
        if (qwUserByRedis == null) {
            log.error("无企微员工信息 {} 跳过处理。", finishLog.getQwUserId());
            return;
        }

        QwCompany qwCompany = iQwCompanyService.getQwCompanyByRedis(qwUserByRedis.getCorpId());

        if (qwCompany == null) {
            log.error("企业微信主体为空 {} 跳过处理。", qwUserByRedis.getCorpId());
            return;
        }

        rocketMQTemplate.asyncSend("course-finish-notes", JSON.toJSONString(finishLog),     new SendCallback() {
            @Override public void onSuccess(SendResult sendResult) {}  // 空实现
            @Override public void onException(Throwable e) {log.error("推送完课打备注失败1:{},{}",JSON.toJSONString(finishLog),e.getMessage());}          // 空实现
        });


        // 定义默认值
         final Integer DEFAULT_SERVER_NUM = 99;

        // 使用
        Integer companyServerNum = Optional.ofNullable(qwCompany.getCompanyServerNum())
                .orElse(DEFAULT_SERVER_NUM);
        switch (companyServerNum){
            case 1:
                rocketMQTemplate.asyncSend("course-finish-notes", JSON.toJSONString(finishLog),     new SendCallback() {
                    @Override public void onSuccess(SendResult sendResult) {}  // 空实现
                    @Override public void onException(Throwable e) {log.error("推送完课打备注失败1:{},{}",JSON.toJSONString(finishLog),e.getMessage());}          // 空实现
                });
                break;
            case 2:

                rocketMQTemplate.asyncSend("course-finish-notesTwo", JSON.toJSONString(finishLog),     new SendCallback() {
                    @Override public void onSuccess(SendResult sendResult) {}  // 空实现
                    @Override public void onException(Throwable e) {log.error("推送完课打备注失败2:{},{}",JSON.toJSONString(finishLog),e.getMessage());}          // 空实现
                });
                break;
            case 3:
                rocketMQTemplate.asyncSend("course-finish-notesThree", JSON.toJSONString(finishLog),     new SendCallback() {
                    @Override public void onSuccess(SendResult sendResult) {}  // 空实现
                    @Override public void onException(Throwable e) {log.error("推送完课打备注失败3:{},{}",JSON.toJSONString(finishLog),e.getMessage());}          // 空实现
                });
                break;
            default:
                break;
        }


    }

}
