package com.fs.app.redis;

import com.fs.common.constant.FsConstants;
import com.fs.common.utils.spring.SpringUtils;
import com.fs.event.TemplateBean;
import com.fs.event.TemplateEvent;
import com.fs.event.TemplateListenEnum;
import com.fs.his.domain.FsInquiryOrder;
import com.fs.his.domain.FsPackageOrder;
import com.fs.his.param.FsInquiryOrderCancelParam;
import com.fs.his.service.IFsInquiryOrderLogsService;
import com.fs.his.service.IFsInquiryOrderService;
import com.fs.his.service.IFsPackageOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    private static final Logger log = LoggerFactory.getLogger(RedisKeyExpirationListener.class);

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Autowired
    IFsInquiryOrderLogsService fsInquiryOrderLogsService;
    @Autowired
    private ApplicationEventPublisher publisher;
    /**
     * 针对redis数据失效事件，进行数据处理
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        //过期的key
        String key = new String(message.getBody(),StandardCharsets.UTF_8);
        log.info("rediskey过期：pattern={},channel={},key={}",new String(pattern),channel,key);
        //订单自动取消
        if(key.contains(FsConstants.REDIS_INQUIRY_ORDER_OUTTIME_UNPAY)) {
            key = key.replace(FsConstants.REDIS_INQUIRY_ORDER_OUTTIME_UNPAY, "");
            log.info("body:{}",key);
            String orderId = key;
            IFsInquiryOrderService orderService= SpringUtils.getBean(IFsInquiryOrderService.class);
            FsInquiryOrder order=orderService.selectFsInquiryOrderByOrderId(Long.parseLong(orderId));
            if(!order.getStatus().equals(1)){
                return;
            }
            FsInquiryOrderCancelParam param=new FsInquiryOrderCancelParam();
            param.setOrderId(Long.parseLong(orderId));
            param.setType(2);
            orderService.cancelOrder(param);
            log.info("订单id:{},未在规定时间支付取消成功",orderId);

        }
        else if(key.contains(FsConstants.REDIS_INQUIRY_ORDER_OUTTIME_UNRECEIVE)) {
            key = key.replace(FsConstants.REDIS_INQUIRY_ORDER_OUTTIME_UNRECEIVE, "");
            log.info("body:{}",key);
            String orderId = key;
            IFsInquiryOrderService orderService= SpringUtils.getBean(IFsInquiryOrderService.class);
            FsInquiryOrder order=orderService.selectFsInquiryOrderByOrderId(Long.parseLong(orderId));
            if(!order.getStatus().equals(2)){
                return;
            }
            FsInquiryOrderCancelParam param=new FsInquiryOrderCancelParam();
            param.setOrderId(Long.parseLong(orderId));
            param.setType(3);
            orderService.cancelOrder(param);
            log.info("订单id:{},未在规定时间接单取消成功",orderId);
        }
        else if(key.contains(FsConstants.REDIS_PACKAGE_ORDER_UNPAY)) {
            key = key.replace(FsConstants.REDIS_PACKAGE_ORDER_UNPAY, "");
            log.info("body:{}",key);
            String orderId = key;
            IFsPackageOrderService orderService= SpringUtils.getBean(IFsPackageOrderService.class);
            FsPackageOrder order=orderService.selectFsPackageOrderByOrderId(Long.parseLong(orderId));
            if(order.getStatus().equals(1)){
                TemplateBean templateBean = TemplateBean.builder()
                        .orderId(order.getOrderId().toString())
                        .orderSn(order.getOrderSn().toString())
                        .time(order.getCreateTime())
                        .remark("您有一个待支付订单,>>进入支付")
                        .uid(order.getUserId())
                        .templateType(TemplateListenEnum.TYPE_3.getValue())
                        .build();
                publisher.publishEvent(new TemplateEvent(this, templateBean));
            }

        }
    }
}
