package com.xhpc.wxma.controller;

import cn.hutool.core.date.DateUtil;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.wxma.socket.OrderNotificationWebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author yuyang
 * @date 2021/8/11 17:47
 */
@RestController
public class OrderNotificationWebSocketController extends BaseController {

    @Autowired
    private RedisService redisService;

    private static final Logger logger = LoggerFactory.getLogger(OrderNotificationWebSocketController.class);
    @GetMapping("/test")
    public void test(@RequestParam String userId){
        OrderNotificationWebSocket.sendMessage(userId,"有新订单啦");
    }

    @GetMapping("/orderWebSocket/getMessage")
    public R getMessage(@RequestParam String userId, @RequestParam String message){
        OrderNotificationWebSocket.sendMessage(userId,message);
        logger.info("发送用户--->"+userId+"发送消息成功------->>>>>message："+message);
        return R.ok();
    }

    @GetMapping("/getDituNumber")
    public void getDituNumber(){
        String format = DateUtil.format(new Date(), "yyyyMMdd");

        if(redisService.getCacheObject("global:" + format) !=null){
            String number = redisService.getCacheObject("global:" + format).toString();
            int numbers =Integer.parseInt(number);
            numbers++;
            redisService.setCacheObject("global:"+format, numbers);
        }else{
            redisService.setCacheObject("global:"+format, 0);
        }

    }

}
