package com.ningdatech.pmapi.scheduler.task;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.pmapi.staging.model.entity.WorkNoticeStaging;
import com.ningdatech.pmapi.staging.service.INdWorkNoticeStagingService;
import com.ningdatech.pmapi.staging.utils.WorkNoticeFlowMapUtil;
import com.ningdatech.pmapi.todocenter.bean.entity.WorkNoticeInfo;
import com.ningdatech.zwdd.client.ZwddClient;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import com.ningdatech.pmapi.scheduler.contants.TaskContant;
import com.ningdatech.pmapi.staging.contants.StagingContant;
import com.ningdatech.pmapi.staging.model.entity.ProjectStaging;
import com.ningdatech.pmapi.staging.service.IProjectStagingService;
import com.ningdatech.pmapi.staging.utils.ProjectStatusFlowMapUtil;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 发送工作通知定时任务
 * @return
 * @author CMM
 * @since 2023/02/28 21:23
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WorkNoticeFlowTask {

    private final INdWorkNoticeStagingService workNoticeStagingService;
    private final ZwddClient zwddClient;

    // @Scheduled(cron = "0 */1 * * * ?")
    public void statusFlow() throws UnknownHostException {
        //测试暂时用自己电脑HOST
        if (TaskContant.Host.HOST_CMM.equals(InetAddress.getLocalHost().getHostName())) {
            //1. 定时取 工作通知暂存表的数据进行发送
            List<WorkNoticeStaging> stagingList = workNoticeStagingService.list(Wrappers.lambdaQuery(WorkNoticeStaging.class)
                    .eq(WorkNoticeStaging::getDead, Boolean.FALSE)
                    .le(WorkNoticeStaging::getNextTime, LocalDateTime.now())
                    .le(WorkNoticeStaging::getRetryTimes, StagingContant.Retry.MAX_RETRY_TIMES)
                    .orderByAsc(WorkNoticeStaging::getId));

            log.info("需要发送的工作通知 size:{} :{}",stagingList.size(), JSON.toJSONString(stagingList));
            if(CollUtil.isEmpty(stagingList)){
                log.info("没有需要发送的工作通知!");
                return;
            }
            //遍历
            for(WorkNoticeStaging workNoticeStaging : stagingList){
                try{
                    WorkNoticeInfo workNoticeInfo = new WorkNoticeInfo();
                    BeanUtils.copyProperties(workNoticeStaging,workNoticeInfo);
                    String receiverUserId = workNoticeInfo.getReceiverUserId();
                    String bizMsgId = workNoticeInfo.getBizMsgId();
                    String msg = workNoticeInfo.getMsg();
                    GenericResult<Void> result = zwddClient.sendWorkNotice(receiverUserId, bizMsgId, msg);
                    if (result.isSuccess()){
                        //执行成功了 删除暂存的数据
                        workNoticeStagingService.removeById(workNoticeStaging);
                    }
                }catch (Exception e){
                    log.error("发送工作通知 异常 bizMsgId:【" + workNoticeStaging.getBizMsgId() + "】 异常内容:" + e);
                }finally {
                    //增加重试的次数 和下次扫描时间
                    workNoticeStagingService.addRetryTimes(workNoticeStaging);
                }
            }
        }
    }
}
