package cn.stylefeng.workflow.instance.modular.message.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.sms.api.SmsSenderApi;
import cn.stylefeng.roses.kernel.sys.api.SysUserServiceApi;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.UserInfoDetailDTO;
import cn.stylefeng.workflow.api.entity.task.RuTask;
import cn.stylefeng.workflow.api.pojo.InstanceContext;
import cn.stylefeng.workflow.api.pojo.config.node.notice.NodeNotice;
import cn.stylefeng.workflow.instance.modular.message.TaskMessageApi;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ShortMessageImpl implements TaskMessageApi {

    @Resource
    private SmsSenderApi smsSenderApi;

    @Resource
    private SysUserServiceApi sysUserServiceApi;

    @Override
    public boolean validateSendFlag(NodeNotice nodeNotice) {
        if (nodeNotice == null) {
            return false;
        }
        return nodeNotice.getShortMessage();
    }

    @Override
    public void sendMessage(Long userId, RuTask ruTask, NodeNotice nodeNotice, InstanceContext instanceContext) {

        UserInfoDetailDTO userDetail = sysUserServiceApi.getUserDetail(userId);
        String phone = userDetail.getPhone();

        if (ObjectUtil.isEmpty(phone)) {
            return;
        }

        String shortMessageTemplateCode = "xxx";

        Map<String, Object> shortMessageParams = new HashMap<>();

        try {
            smsSenderApi.sendSms(phone, shortMessageTemplateCode, shortMessageParams);
        } catch (Exception e) {
            log.error("发送短信失败！", e);
        }

    }

}
