package com.ruoyi.jarvis.service.impl;

import com.ruoyi.jarvis.domain.TaobaoComment;
import com.ruoyi.jarvis.mapper.TaobaoCommentMapper;
import com.ruoyi.jarvis.service.ITaobaoCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

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

/**
 * 淘宝评论管理 Service 实现
 */
@Service
public class TaobaoCommentServiceImpl implements ITaobaoCommentService {

    private static final String PRODUCT_TYPE_MAP_PREFIX_TB = "product_type_map_tb";

    @Autowired
    private TaobaoCommentMapper taobaoCommentMapper;

    @Autowired(required = false)
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<TaobaoComment> selectTaobaoCommentList(TaobaoComment taobaoComment) {
        List<TaobaoComment> list = taobaoCommentMapper.selectTaobaoCommentList(taobaoComment);
        // 填充Redis映射的产品类型信息
        if (stringRedisTemplate != null) {
            Map<String, String> tbMap = getTbProductTypeMap();
            for (TaobaoComment c : list) {
                // 查找对应的产品类型
                String productId = c.getProductId();
                if (tbMap != null) {
                    for (Map.Entry<String, String> entry : tbMap.entrySet()) {
                        if (entry.getValue().equals(productId)) {
                            c.setProductType(entry.getKey());
                            c.setMappedProductId(productId);
                            break;
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public TaobaoComment selectTaobaoCommentById(Integer id) {
        TaobaoComment comment = taobaoCommentMapper.selectTaobaoCommentById(id);
        if (comment != null && stringRedisTemplate != null) {
            Map<String, String> tbMap = getTbProductTypeMap();
            if (tbMap != null) {
                String productId = comment.getProductId();
                for (Map.Entry<String, String> entry : tbMap.entrySet()) {
                    if (entry.getValue().equals(productId)) {
                        comment.setProductType(entry.getKey());
                        comment.setMappedProductId(productId);
                        break;
                    }
                }
            }
        }
        return comment;
    }

    @Override
    public int updateTaobaoCommentIsUse(TaobaoComment taobaoComment) {
        return taobaoCommentMapper.updateTaobaoCommentIsUse(taobaoComment);
    }

    @Override
    public int deleteTaobaoCommentByIds(Integer[] ids) {
        return taobaoCommentMapper.deleteTaobaoCommentByIds(ids);
    }

    @Override
    public int resetTaobaoCommentIsUseByProductId(String productId) {
        return taobaoCommentMapper.resetTaobaoCommentIsUseByProductId(productId);
    }

    private Map<String, String> getTbProductTypeMap() {
        if (stringRedisTemplate == null) {
            return null;
        }
        try {
            Map<Object, Object> rawMap = stringRedisTemplate.opsForHash().entries(PRODUCT_TYPE_MAP_PREFIX_TB);
            Map<String, String> result = new java.util.LinkedHashMap<>();
            for (Map.Entry<Object, Object> entry : rawMap.entrySet()) {
                result.put(entry.getKey().toString(), entry.getValue().toString());
            }
            return result;
        } catch (Exception e) {
            return null;
        }
    }
}

