package com.codvision.workflow.listener;


import com.codvision.auth.support.SendSms;
import com.codvision.base.service.ISysConfigService;
import com.codvision.workflow.entity.dto.WorkFlowSmsDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.variable.api.event.FlowableVariableEvent;
import org.springframework.stereotype.Component;

import static com.codvision.workflow.constant.WorkFlowConstant.CLAIM_SIGN;
import static com.codvision.workflow.constant.WorkFlowConstant.CLAIM_SMS_PARAMS;

/**
 * 全局变量监听器
 * @author hxl
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GlobalWorkFlowVariableListener extends AbstractFlowableEngineEventListener {

    private final TaskService taskService;
    private final ISysConfigService configService;

    @Override
    protected void variableCreated(FlowableVariableEvent event) {
        try {
            // 签收短信消息通知
            if (CLAIM_SIGN.equals(event.getVariableName()) && (Boolean) event.getVariableValue()) {
                WorkFlowSmsDTO sms = (WorkFlowSmsDTO) taskService.getVariableLocal(event.getTaskId(), CLAIM_SMS_PARAMS);
                if (sms == null) {
                    return;
                }

                SendSms sendSms = new SendSms(configService);
                sendSms.sendWithParam(sms.getPhoneNumbers(), sms.getSignName(), sms.getTemplateCode(), sms.getTemplateParam());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
