package com.mortals.xhx.daemon;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.framework.config.InterceptorConfig;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.common.code.ApiRespCodeEnum;
import com.mortals.xhx.common.code.OneThingRespCodeEnum;
import com.mortals.xhx.common.model.DefaultTbQueueMsgHeaders;
import com.mortals.xhx.common.model.MessageHeader;
import com.mortals.xhx.queue.*;
import com.mortals.xhx.queue.provider.TbCoreQueueProducerProvider;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 一件事微信对外服务接口
 *
 * @author: zxfei
 * @date: 2021/8/24 20:28
 */
@CrossOrigin(origins = "*")
@RestController
@CommonsLog
@RequestMapping("/test/")
public class DemoApiController {

    @Autowired
    private TbCoreQueueProducerProvider producerProvider;


    @GetMapping("/sentry")
    public String sentry() {
        log.error("[main][我就是展示下异常！]");

        return "success";
    }


    @GetMapping("/exception")
    public String exception() {
        throw new RuntimeException("直接抛出异常");
    }

    @GetMapping("/appexception")
    public String appexception() {
        throw new AppException("自定义异常");
    }

    /**
     * 发送消息
     *
     * @return
     */
    @PostMapping("produceMsg")
    public String produceMsg() {
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(OneThingRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(OneThingRespCodeEnum.SUCCESS.getValue());
        try {
            TbQueueProducer<TbQueueMsg> producer = producerProvider.getTbCoreMsgProducer();

            TopicPartitionInfo info = TopicPartitionInfo.builder().topic("demoTopic").build();
            TbQueueMsgHeaders header = new DefaultTbQueueMsgHeaders();
            header.put(MessageHeader.CLIENTID, "aaa");


            Map<String, Object> headers = new HashMap<>();
            headers.put(MessageHeader.TIMESTAMP, DateUtils.getCurrStrDateTime().getBytes());
            String payLoad = "12313";

//            TbQueueMsg queueMsg = new DefaultTbQueueMsg(UUID.randomUUID().toString(), payLoad.getBytes(), header);
//
//
//            for (int i = 0; i < 10; i++) {
//                producer.send(info, queueMsg, new TbQueueCallback() {
//                    @Override
//                    public void onSuccess(TbQueueMsgMetadata metadata) {
//                        // logger.info("消息发送成功");
//                    }
//
//                    @Override
//                    public void onFailure(Throwable t) {
//
//                    }
//                });
//            }

        } catch (Exception e) {
            log.error("消息提交失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }

        log.info("响应【消息发送】【响应体】-->  " + JSONObject.toJSONString(rsp));
        return JSON.toJSONString(rsp);
    }

}
