package ink.wgink.module.wechat.officialaccount.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.exceptions.rpc.RemoteRequestException;
import ink.wgink.module.wechat.officialaccount.manager.OfficialAccountAccessTokenManager;
import ink.wgink.module.wechat.officialaccount.pojo.vos.OfficialAccountTemplateMessageSendVO;
import ink.wgink.module.wechat.officialaccount.remote.IOfficialAccountRemoteService;
import ink.wgink.module.wechat.officialaccount.result.TemplateMessageWechatResult;
import ink.wgink.module.wechat.officialaccount.service.IOfficialAccountTemplateMessageService;
import ink.wgink.properties.wechat.official.account.OfficialAccountProperties;
import ink.wgink.util.BeanPropertyCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName: OfficialAccountTemplateMessageServiceImpl
 * @Description: 模板消息
 * @Author: wanggeng
 * @Date: 2021/9/30 10:09 上午
 * @Version: 1.0
 */

@Service
public class OfficialAccountTemplateMessageServiceImpl extends DefaultBaseService implements IOfficialAccountTemplateMessageService {

    @Autowired
    private IOfficialAccountRemoteService officialAccountTemplateMessageRemoteService;
    @Autowired
    private OfficialAccountProperties officialAccountProperties;

    @Override
    public void send(OfficialAccountTemplateMessageSendVO officialAccountTemplateMessageSendVO) {
        BeanPropertyCheckUtil.checkField(officialAccountTemplateMessageSendVO);
        if (officialAccountTemplateMessageSendVO.getData() == null) {
            throw new ParamsException("模板数据为空");
        }
        String accessToken = OfficialAccountAccessTokenManager.getInstance().getAccessToken().getAccessToken();
        TemplateMessageWechatResult templateMessageWechatResult = officialAccountTemplateMessageRemoteService.send(officialAccountProperties.getApiUrl(), accessToken, officialAccountTemplateMessageSendVO);
        if (templateMessageWechatResult.getErrcode() != 0) {
            throw new RemoteRequestException(templateMessageWechatResult.getErrmsg());
        }
    }
}
