package com.ningdatech.pmapi.staging.service.impl;

import com.ningdatech.pmapi.staging.contants.StagingContant;
import com.ningdatech.pmapi.staging.enums.MsgTypeEnum;
import com.ningdatech.pmapi.staging.model.entity.ProjectStaging;
import com.ningdatech.pmapi.staging.model.entity.WorkNoticeStaging;
import com.ningdatech.pmapi.staging.mapper.NdWorkNoticeStagingMapper;
import com.ningdatech.pmapi.staging.service.INdWorkNoticeStagingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.pmapi.staging.utils.WorkNoticeFlowMapUtil;
import com.ningdatech.pmapi.todocenter.bean.entity.WorkNoticeInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CMM
 * @since 2023-02-28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class NdWorkNoticeStagingServiceImpl extends ServiceImpl<NdWorkNoticeStagingMapper, WorkNoticeStaging> implements INdWorkNoticeStagingService {

    private final NdWorkNoticeStagingMapper mapper;

    private final WorkNoticeFlowMapUtil workNoticeFlowMapUtil;

    /**
     * 增加 重试次数 和下次扫描时间
     * @param workNoticeStaging
     * @return java.lang.Boolean
     * @author CMM
     * @since 2023/02/28 18:02
     */
    @Override
    public Boolean addRetryTimes(WorkNoticeStaging workNoticeStaging) {
        Integer retryTimes = workNoticeStaging.getRetryTimes() + 1;
        if(!workNoticeFlowMapUtil.intervalTimeMap.containsKey(retryTimes)){
            log.info("没有对应重试间隔时间 添加重试信息失败");
            return Boolean.FALSE;
        }
        Integer addSeconds = workNoticeFlowMapUtil.intervalTimeMap.get(retryTimes);
        Boolean dead = Boolean.FALSE;
        //超过重试最大次数 dead置为 true
        if(retryTimes.compareTo(StagingContant.Retry.MAX_RETRY_TIMES) > 0){
            dead = Boolean.TRUE;
        }
        LocalDateTime nextRetryTime = LocalDateTime.now().plusSeconds(addSeconds);
        return mapper.addRetryTimes(workNoticeStaging.getId(),retryTimes,nextRetryTime,dead);
    }

    /**
     * 在对应的流程处理后，增加一个工作通知到暂存表中
     * @param workNoticeInfo
     * @param msgType
     * @return java.lang.Boolean
     * @author CMM
     * @since 2023/02/28 20:02
     */
    @Override
    public Boolean addByWorkNotice(WorkNoticeInfo workNoticeInfo, MsgTypeEnum msgType) {
        WorkNoticeStaging workNoticeStaging = WorkNoticeStaging.builder()
                .accountId(workNoticeInfo.getAccountId())
                .msg(workNoticeInfo.getMsg())
                .bizMsgId(workNoticeInfo.getBizMsgId())
                .organizationCode(workNoticeInfo.getOrganizationCode())
                .organizationName(workNoticeInfo.getOrganizationName())
                .receiverUserId(workNoticeInfo.getReceiverUserId())
                .msgType(msgType)
                .createOn(LocalDateTime.now())
                .updateOn(LocalDateTime.now())
                .nextTime(LocalDateTime.now())
                .retryTimes(0)
                .build();
        return this.save(workNoticeStaging);
    }
}
