package com.ssm.config;

import com.ssm.manage.entity.StoreMemberList;
import com.ssm.manage.service.SsmStoreMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    /**
     * Creates new {@link RedisMessageListenerContainer} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        try {
            String key = message.toString();
            if (key != null && key.startsWith("MPR_")) {
                String[] strs=key.split("_");
                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(Long.valueOf(strs[1]));
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());
                if(strs[3].equals(storeMemberList1.getPassword())){
                    storeMemberList1.setPassword(strs[2]);
                    ssmStoreMemberService.updateByPrimaryKey(storeMemberList1);
                }
            }else if(key != null && key.startsWith("MCPR_")){
                String[] strs=key.split("_");
                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(Long.valueOf(strs[1]));
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());
                if(strs[3].equals(storeMemberList1.getCashPassword())){
                    storeMemberList1.setCashPassword(strs[2]);
                    ssmStoreMemberService.updateByPrimaryKey(storeMemberList1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【修改支付订单过期状态异常】：" + e.getMessage());
        }
    }

}
