package com.bohuikeji.delivery.backstage.controller.test;

import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.constants.WxAppEm;
import com.bohuikeji.delivery.backstage.constants.WxTmplEm;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.socket.AdminNotice;
import com.bohuikeji.delivery.backstage.entity.socket.CourierNotice;
import com.bohuikeji.delivery.backstage.service.ISocketService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.delivery.backstage.service.IWxUserService;
import com.bohuikeji.delivery.backstage.task.CourierNoticeTask;
import com.bohuikeji.delivery.backstage.util.AppPush;
import com.bohuikeji.delivery.backstage.util.WxUtils;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.socket.Smsg;
import com.bohuikeji.frame.module.admin.service.IRanchSocketService;
import com.bohuikeji.frame.module.admin.utils.TaskUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试使用
 *
 * @author 孙劲辉
 */
@Slf4j
@Api(tags = {"Socket功能测试"})
@RestController
@RequestMapping("/api/test/socket")
@RequiredArgsConstructor
public class SocketTestApi extends ApiController {
    final IRanchSocketService ranchSocketService;
    final ISocketService socketService;
    final IWxUserService wxUserService;
    final CourierNoticeTask noticeTask;
    final ITOrderService orderService;

    @ApiOperation("发送消息")
    @GetMapping("/msg")
    APIEntity testSocket(Smsg smsg) {
        ranchSocketService.sendMsg(smsg);
        return APIEntity.success();
    }

    @ApiOperation("发送消息给后台")
    @GetMapping("/admin")
    APIEntity testSocketAdmin(String text, Integer franID) {
        socketService.sendAdminMsg(new AdminNotice(text).setFranId(franID));
        return APIEntity.success();
    }

    @ApiOperation("发送消息给骑手")
    @GetMapping("/courier")
    APIEntity testSocketAdmin(Integer orderId,String courierIds,boolean type) {
        TOrder order = orderService.getById(orderId);
        //推送语音消息
        CourierNotice notice = new CourierNotice("同城急送，订单来袭!", courierIds);
        notice.setMp3("https://oss.bhynm.com/delivery/upload/2022/3/23/164802791720412.mp3");

        return APIEntity.success(socketService.sendCourierMsg(notice,type));
    }

    @ApiOperation("发送微信公众号消息")
    @GetMapping("/wx/gzh/tlmp")
    JSONObject wxGzhTlmp(String openid) {
        Map<String, Object> params = new HashMap<>();
        params.put("first", "同城急送，订单来袭~");
        params.put("keyword1", "孙劲辉");
        params.put("keyword2", "15771335697".replaceAll("(\\d{3})\\d{5}(\\d{3})", "$1*****$2"));
        params.put("keyword3", "回民区");
        params.put("remark", "点击立即抢单！");
        WxTmplEm courierTlmp = WxTmplEm.COURIER_ORDER_NEW;
        courierTlmp.setUrl(WxUtils.getMpShareLink(WxAppEm.MP_COURIER, "pages/start/start", null).getString("url_link"));
        return WxUtils.sendTmplMsg(WxAppEm.GZH_TCJS, courierTlmp, openid, params);
    }

    @ApiOperation("任务测试")
    @GetMapping("/task")
    APIEntity task() {

        //延迟10秒放入队列
        TaskUtils.run(noticeTask).addParam(CourierNoticeTask.KEY_ORDER_ID,202);
        TaskUtils.add(noticeTask,5000).addParam(CourierNoticeTask.KEY_ORDER_ID,196);
        return APIEntity.success();
    }
}
