package com.fzm.mall.server.front.redis;

import com.alibaba.fastjson.JSON;
import com.fzm.mall.server.front.asset.model.vo.good.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fzm.mall.server.front.goods.model.GoodSale;
import com.fzm.mall.server.front.goods.model.GoodSpu;
import com.fzm.mall.server.front.repository.RedisRepository;
import com.fzm.mall.server.front.constant.cache.GoodCache;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author tdz
 * @date 2021/02/22 11:26
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodRedis {
    private final RedisRepository redisRepository;

    public void setSku(String coin, GoodSkuVO sku) {
        redisRepository.setObj(GoodCache.GOOD_SKU_UNIT + coin, sku, 7, TimeUnit.HOURS);
    }

    public GoodSkuVO getSku(String coin) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOOD_SKU_UNIT + coin);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), GoodSkuVO.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delSku(coin);
        }
        return null;
    }

    public void delSku(String coin) {
        redisRepository.delObj(GoodCache.GOOD_SKU_UNIT + coin);
    }


    public void setSpu(String gid, GoodSpu goodSpu) {
        redisRepository.setObj(GoodCache.GOODS_SPU + gid, goodSpu, 7, TimeUnit.HOURS);
    }

    public GoodSpu getSpu(String gid) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOODS_SPU + gid);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), GoodSpu.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delSpu(gid);
        }
        return null;
    }
    public void delSpu(String gid) {
        redisRepository.delObj(GoodCache.GOODS_SPU + gid);
    }

    public void delSkuProp(String skuId) {
        redisRepository.delObj(GoodCache.GOOD_SKU_PROP + skuId);
    }

    public void setSkuProp(String skuId, List<Map<String, Object>> list) {
        redisRepository.setObj(GoodCache.GOOD_SKU_PROP + skuId, list, 7, TimeUnit.HOURS);
    }

    public List<Map<String, Object>> getSkuProp(String skuId) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOOD_SKU_PROP + skuId);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), List.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delSkuProp(skuId);
        }
        return null;
    }


    public void setSkus(String gid, List<GoodSkuVO> goodSkus) {
        redisRepository.setObj(GoodCache.GOODS_SKU + gid, goodSkus, 7, TimeUnit.HOURS);
    }

    public List<GoodSkuVO> getSkus(String gid) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOODS_SKU + gid);
            if (obj != null) {
                return JSON.parseArray(JSON.toJSONString(obj), GoodSkuVO.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delSkus(gid);
        }
        return null;
    }

    public void delSkus(String gid) {
        redisRepository.delObj(GoodCache.GOODS_SKU + gid);
    }


    public void setSale(String gid, GoodSale goodSale) {
        redisRepository.setObj(GoodCache.GOODS_SALE + gid, goodSale, 7, TimeUnit.HOURS);
    }

    public GoodSale getSale(String gid) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOODS_SALE + gid);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), GoodSale.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delSale(gid);
        }
        return null;
    }

    public void delSale(String gid) {
        redisRepository.delObj(GoodCache.GOODS_SALE + gid);
    }


    public void setCount(String gid, GoodCountVO goodCount) {
        redisRepository.setObj(GoodCache.GOOD_COUNT + gid, goodCount, 7, TimeUnit.HOURS);
    }

    public GoodCountVO getCount(String gid) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOOD_COUNT + gid);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), GoodCountVO.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delCount(gid);
        }
        return null;
    }

    public void delCount(String gid) {
        redisRepository.delObj(GoodCache.GOOD_COUNT + gid);
    }


    public void setBp(String gid, GoodBpVO goodBpVO) {
        redisRepository.setObj(GoodCache.GOOD_BP + gid, goodBpVO, 7, TimeUnit.HOURS);
    }

    public GoodBpVO getBp(String gid) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOOD_BP + gid);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), GoodBpVO.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delBp(gid);
        }
        return null;
    }

    public void delBp(String gid) {
        redisRepository.delObj(GoodCache.GOOD_BP + gid);
    }

    public void setTrace(String gid, GoodTraceVO goodTraceVO) {
        redisRepository.setObj(GoodCache.GOOD_TRACE + gid, goodTraceVO, 7, TimeUnit.HOURS);
    }

    public GoodTraceVO getTrace(String gid) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOOD_TRACE + gid);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), GoodTraceVO.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delTrace(gid);
        }
        return null;
    }

    public void delTrace(String gid) {
        redisRepository.delObj(GoodCache.GOOD_TRACE + gid);
    }

    public void setState(String gid, GoodStateVO goodStateVO) {
        redisRepository.setObj(GoodCache.GOOD_STATE + gid, goodStateVO, 7, TimeUnit.HOURS);
    }

    public GoodStateVO getState(String gid) {
        try {
            Object obj = redisRepository.getObj(GoodCache.GOOD_STATE + gid);
            if (obj != null) {
                return JSON.parseObject(JSON.toJSONString(obj), GoodStateVO.class);
            }
        } catch (Exception e) {
            // 缓存中获取的数据转换发生错误，重置缓存
            delState(gid);
        }
        return null;
    }

    public void delState(String gid) {
        redisRepository.delObj(GoodCache.GOOD_STATE + gid);
    }
}
