package com.ningdatech.pmapi.sys.manage;

import com.ningdatech.pmapi.common.enumeration.CommonEnum;
import com.ningdatech.pmapi.common.helper.UserInfoHelper;
import com.ningdatech.pmapi.meeting.helper.YxtCallOrSmsHelper;
import com.ningdatech.pmapi.projectlib.enumeration.InstTypeEnum;
import com.ningdatech.pmapi.projectlib.enumeration.WarningFlowTypeEnum;
import com.ningdatech.pmapi.projectlib.enumeration.WarningOperationTypeEnum;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.staging.enums.MsgTypeEnum;
import com.ningdatech.pmapi.staging.service.INdWorkNoticeStagingService;
import com.ningdatech.pmapi.sys.model.entity.Notify;
import com.ningdatech.pmapi.sys.model.entity.WflowEarlyWarningRecords;
import com.ningdatech.pmapi.sys.service.IEarlyWarningRecordsService;
import com.ningdatech.pmapi.sys.service.INotifyService;
import com.ningdatech.pmapi.todocenter.bean.entity.WorkNoticeInfo;
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO;
import com.ningdatech.yxt.model.cmd.SendSmsCmd;
import com.wflow.enums.WarningRuleTypeEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Classname EarlyWarningManage
 * @Description
 * @Date 2023/8/7 9:25
 * @Author PoffyZhang
 */
@Component
@AllArgsConstructor
@Slf4j
public class EarlyWarningManage {

    private final UserInfoHelper userInfoHelper;

    private final NoticeManage noticeManage;

    private final IEarlyWarningRecordsService earlyWarningRecordsService;

    private final YxtCallOrSmsHelper yxtCallOrSmsHelper;

    private final INdWorkNoticeStagingService workNoticeStagingService;

    private final INotifyService notifyService;

    /**
     * 预警通知
     * @param noticeMethod
     * @param noticeContent
     * @param timeout
     * @param employeeCode
     * @param project
     */
    public void doEarlyWarning(String noticeMethod,String noticeContent, Integer timeout,Integer biz,
                               LocalDateTime startTime,String employeeCode, Project project,Integer ruleType) {
        //1.存入 预警记录
        UserFullInfoDTO user = userInfoHelper.getUserFullInfoByEmployeeCode(employeeCode);
        WflowEarlyWarningRecords records = new WflowEarlyWarningRecords();
        String content = StringUtils.EMPTY;

        switch (WarningRuleTypeEnum.checkByCode(ruleType)){
            case PROCESS_WARNING:
                content = convertContent(noticeContent,project.getProjectName(),
                        InstTypeEnum.getByCode(biz),timeout);
                records.setRuleType(WarningRuleTypeEnum.PROCESS_WARNING.getCode());
                break;
            case DECLARED_WARNING:
                content = convertContent(noticeContent,project.getProjectName(),
                        WarningFlowTypeEnum.getByCode(biz),timeout);
                records.setRuleType(WarningRuleTypeEnum.DECLARED_WARNING.getCode());
                break;
            case OPERATION_WARNING:
                content = convertContent(noticeContent,project.getProjectName(),
                        WarningOperationTypeEnum.getByCode(biz),timeout);
                records.setRuleType(WarningRuleTypeEnum.OPERATION_WARNING.getCode());
                break;
            default:
                log.info("匹配不到 规则类型");
                return;
        }

        records.setAreaCode(project.getAreaCode());
        records.setBuildOrgCode(project.getBuildOrgCode());
        records.setBuildOrgName(project.getBuildOrgName());
        records.setCreateOn(LocalDateTime.now());
        records.setWarningTime(LocalDateTime.now());
        records.setInstStart(startTime);
        records.setInstType(biz);
        records.setNoticeMethod(noticeMethod);
        records.setNoticeContent(content);
        records.setProjectName(project.getProjectName());
        records.setWarningUsername(Objects.nonNull(user) ? user.getUsername() : StringUtils.EMPTY);
        records.setWarningEmployeecode(employeeCode);
        earlyWarningRecordsService.save(records);

        //2.消息提醒
        Notify notify = noticeManage.assemblyAuditNotify(user.getUserId(), project, content);
        notify.setType(MsgTypeEnum.PROJECT_REVIEW.name());
        notifyService.save(notify);

        //3.发短信
        if(noticeMethod.contains(String.valueOf(CommonEnum.MOBILE.getCode()))){
            SendSmsCmd.SendSmsContext context = new SendSmsCmd.SendSmsContext();
            context.setReceiveNumber(user.getMobile());
            context.setContent(content);
            yxtCallOrSmsHelper.sendSms(context);
        }

        //4.浙政钉
        if(noticeMethod.contains(String.valueOf(CommonEnum.ZWDD.getCode()))){
            // 获取发送浙政钉工作通知必要信息
            WorkNoticeInfo passWorkNoticeInfo = noticeManage.getSendWorkNoticeInfo(employeeCode);
            passWorkNoticeInfo.setMsg(content);
            // 放入工作通知暂存表中，通过扫表异步发送
            workNoticeStagingService.addByWorkNotice(passWorkNoticeInfo, MsgTypeEnum.PROJECT_REVIEW);
        }
    }

    /**
     * 转换出 通知的内容
     * @param noticeContent
     * @param projectName
     * @param instTypeEnum
     * @param timeout
     * @return
     */
    private String convertContent(String noticeContent, String projectName, InstTypeEnum instTypeEnum, Integer timeout) {
        noticeContent = noticeContent.replace("{projectName}",projectName)
                .replace("{flowType}",Objects.nonNull(instTypeEnum) ? instTypeEnum.getDesc() : "{flowType}")
                .replace("{stepName}",Objects.nonNull(instTypeEnum) ? instTypeEnum.getDesc() : "{stepName}")
                .replace("{time}",String.valueOf(timeout));
        log.info("通知内容 :{}",noticeContent);
        return noticeContent;
    }

    private String convertContent(String noticeContent, String projectName, WarningFlowTypeEnum warningFlowTypeEnum, Integer timeout) {
        noticeContent = noticeContent.replace("{projectName}",projectName)
                .replace("{flowType}",Objects.nonNull(warningFlowTypeEnum) ? warningFlowTypeEnum.getDesc() : "{flowType}")
                .replace("{stepName}",Objects.nonNull(warningFlowTypeEnum) ? warningFlowTypeEnum.getDesc() : "{stepName}")
                .replace("{time}",String.valueOf(timeout));
        log.info("通知内容 :{}",noticeContent);
        return noticeContent;
    }

    private String convertContent(String noticeContent, String projectName, WarningOperationTypeEnum operationTypeEnum, Integer timeout) {
        noticeContent = noticeContent.replace("{projectName}",projectName)
                .replace("{flowType}",Objects.nonNull(operationTypeEnum) ? operationTypeEnum.getDesc() : "{flowType}")
                .replace("{stepName}",Objects.nonNull(operationTypeEnum) ? operationTypeEnum.getDesc() : "{stepName}")
                .replace("{time}",String.valueOf(timeout));
        log.info("通知内容 :{}",noticeContent);
        return noticeContent;
    }
}
