package com.xingxi.api.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
import cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage;
import com.xingxi.api.service.CacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


@Slf4j
@Component("WXA_MEDIA_CHECK")
@RequiredArgsConstructor
public class WxaMediaCheckWxMaMessageHandlerImpl implements WxMaMessageHandler {
    private final CacheService cacheService;

    @Override
    public WxMaXmlOutMessage handle(WxMaMessage message, Map<String, Object> context, WxMaService service, WxSessionManager sessionManager) throws WxErrorException {
        String traceId = message.getTraceId();
        // 如果检测内容违规，此订单看情况处理是否要退款
        Object orderNo = cacheService.getCache(traceId);
        if(orderNo != null){
            var traceIds = (List<String>) cacheService.getCache((String) orderNo);
            if (traceIds != null && !traceIds.isEmpty()) {
                for (String id : traceIds) {
                    cacheService.invalidate(id);
                }
                cacheService.invalidate((String) orderNo);
            }

        }

        return null;
    }


}
