package com.jianshui.system.event.listener;

import com.alibaba.fastjson.JSONObject;
import com.jianshui.system.event.evemts.JianshuiBillInfoCalucateExceptionEvent;
import com.jianshui.common.utils.DateUtils;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.http.HttpUtils;
import com.jianshui.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

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

/**
 * @Description 简税不含税金额、含税金额、税额计算异常监听器
 * @Author 巩权林
 * @Date 2022/3/26 21:46
 **/
@Component
public class JianshuiBillInfoCalucateExceptionListener implements ApplicationListener<JianshuiBillInfoCalucateExceptionEvent> {

    @Autowired
    private ISysConfigService configService;

    @Override
    @Async
    public void onApplicationEvent(JianshuiBillInfoCalucateExceptionEvent event) {
        // 计算异常，发布异常消息
        JSONObject params = new JSONObject();
        params.put("title", StringUtils.format("开票出现税额计算异常！销方名称为{}", event.getCompanyservice().getSellername()));
        params.put("content", StringUtils.format("## 错误信息\n" +
                "\n" +
                "{}\n" +
                "\n" +
                "## 订单详情\n" +
                "```json\n" +
                "{}\n" +
                "```\n" +
                "异常出现时间为:{}", event.getMsg(), event.getBillInfoJson(), DateUtils.getTime()));
        String url = configService.selectConfigByKey("push_showdoc_url");
        if (StringUtils.isNotEmpty(url)) {
            HttpUtils.sendJsonPost(url, params);
        }
    }
}
