package com.liquidnet.service.goblin.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.commons.lang.util.RandomUtil;
import com.liquidnet.service.goblin.constant.GoblinRedisConst;
import com.liquidnet.service.goblin.dto.GoblinStoreMarketDto;
import com.liquidnet.service.goblin.dto.manage.vo.GoblinMgtCategorySpecVo;
import com.liquidnet.service.goblin.dto.vo.*;
import com.liquidnet.service.goblin.entity.GoblinFrontBanner;
import com.liquidnet.service.goblin.entity.GoblinFrontHotWord;
import com.liquidnet.service.goblin.entity.GoblinFrontNavigation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class GoblinRedisUtils {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    GoblinMongoUtils goblinMongoUtils;

    private int randomMax = 10;

    public void del(String... keys) {
        redisUtil.del(keys);
    }

    /* ---------------------------------------- sku库存相关 ---------------------------------------- */
    public void setSkuStock(String marketPre, String skuId, Integer stock) {
        String rk = GoblinRedisConst.REAL_STOCK_SKU;
        if (marketPre != null) {
            rk = rk.concat(marketPre + ":");
        }
        rk = rk.concat(skuId);
        redisUtil.set(rk, stock);
    }

    public int getSkuStock(String marketPre, String skuId) {
        String rk = GoblinRedisConst.REAL_STOCK_SKU;
        if (marketPre != null && !marketPre.equals("null")) {
            rk = rk.concat(marketPre + ":");
        }
        rk = rk.concat(skuId);
        Object obj = redisUtil.get(rk);
        if (obj == null) {
            return 0;
        } else {
            return (int) obj;
        }
    }

    public int incrSkuStock(String marketPre, String skuId, Integer stock) {
        String rk = GoblinRedisConst.REAL_STOCK_SKU;
        if (marketPre != null && !marketPre.equals("null")) {
            rk = rk.concat(marketPre + ":");
        }
        rk = rk.concat(skuId);
        return (int) redisUtil.incr(rk, stock);
    }

    public int decrSkuStock(String marketPre, String skuId, Integer stock) {
        String rk = GoblinRedisConst.REAL_STOCK_SKU;
        if (marketPre != null && !marketPre.equals("null")) {
            rk = rk.concat(marketPre + ":");
        }
        rk = rk.concat(skuId);
        return (int) redisUtil.decr(rk, stock);
    }

    //添加 skuId 相关的活动 skuId
    public void addSkuRe(String skuId, String marketSkuId) {
        String rk = GoblinRedisConst.SKU_RELATION.concat(skuId);
        List<String> list = getSkuRe(skuId);
        list.add(marketSkuId);
        redisUtil.set(rk, list);
    }

    //删除 skuId 相关的活动 skuId
    public void removeSkuRe(String skuId, String marketSkuId) {
        String rk = GoblinRedisConst.SKU_RELATION.concat(skuId);
        List<String> list = getSkuRe(skuId);
        list.remove(marketSkuId);
        redisUtil.set(rk, list);
    }

    //获取 skuId 相关的活动 skuId
    public List<String> getSkuRe(String skuId) {
        String rk = GoblinRedisConst.SKU_RELATION.concat(skuId);
        Object obj = redisUtil.get(rk);
        if (obj == null) {
            return new ArrayList();
        } else {
            return (List<String>) obj;
        }
    }

    /* ---------------------------------------- 平台标签数据源 ---------------------------------------- */

    public List<GoblinSelfTagVo> getSelfGoodsTagVos() {// 普通商品标签
        String valStr = (String) redisUtil.get(GoblinRedisConst.BASIC_SELF_GOODS_TAG);
        boolean valStrIsEmptyFlg = StringUtils.isEmpty(valStr);
        List<GoblinSelfTagVo> vos;
        if (valStrIsEmptyFlg) {
            if (!CollectionUtils.isEmpty(vos = goblinMongoUtils.getSelfTagVos("0"))) {
                redisUtil.set(GoblinRedisConst.BASIC_SELF_GOODS_TAG, JsonUtils.toJson(vos));
            }
        } else {
            vos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinSelfTagVo>>() {
            });
        }
        return vos;
    }

    public List<GoblinSelfTagVo> getSelfExtagVos() {// 专属标签（音乐人等）
        String valStr = (String) redisUtil.get(GoblinRedisConst.BASIC_SELF_EXTAG);
        List<GoblinSelfTagVo> vos;
        if (StringUtils.isEmpty(valStr)) {
            if (!CollectionUtils.isEmpty(vos = goblinMongoUtils.getSelfTagVos("1"))) {
                redisUtil.set(GoblinRedisConst.BASIC_SELF_GOODS_TAG, JsonUtils.toJson(vos));
            }
        } else {
            vos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinSelfTagVo>>() {
            });
        }
        return vos;
    }

    /* ---------------------------------------- 平台分类数据源 ---------------------------------------- */

    public List<GoblinSelfGoodsCategoryVo> getSelfGoodsCategoryVos() {
        String valStr = (String) redisUtil.get(GoblinRedisConst.BASIC_SELF_GOODS_CATEGORY);
        boolean valStrIsEmptyFlg = StringUtils.isEmpty(valStr);
        List<GoblinSelfGoodsCategoryVo> vos;
        if (valStrIsEmptyFlg) {
            if (!CollectionUtils.isEmpty(vos = goblinMongoUtils.getSelfGoodsCategoryVos())) {
                redisUtil.set(GoblinRedisConst.BASIC_SELF_GOODS_CATEGORY, JsonUtils.toJson(vos));
            }
        } else {
            vos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinSelfGoodsCategoryVo>>() {
            });
        }
        return vos;
    }

    /* ---------------------------------------- 店铺分类数据源 ---------------------------------------- */

    public boolean setStoreGoodsCategoryVos(String storeId, List<GoblinStoreGoodsCategoryVo> vos) {
        return redisUtil.set(GoblinRedisConst.BASIC_STORE_GOODS_CATEGORY.concat(storeId), JsonUtils.toJson(vos));
    }

    public List<GoblinStoreGoodsCategoryVo> getStoreGoodsCategoryVos(String storeId) {
        String rk = GoblinRedisConst.BASIC_STORE_GOODS_CATEGORY.concat(storeId);
        String valStr = (String) redisUtil.get(rk);
        boolean valStrIsEmptyFlg = StringUtils.isEmpty(valStr);
        List<GoblinStoreGoodsCategoryVo> vos = null;
        if (valStrIsEmptyFlg && !CollectionUtils.isEmpty(vos = goblinMongoUtils.getStoreGoodsCategoryVos(storeId))) {
            valStr = JsonUtils.toJson(vos);
            redisUtil.set(rk, valStr);
        } else if (!valStrIsEmptyFlg) {
            vos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinStoreGoodsCategoryVo>>() {
            });
        }
        return vos;
    }

    /* ---------------------------------------- 平台分类规格数据源 ---------------------------------------- */

    public boolean setCategorySpec(String cateId, GoblinMgtCategorySpecVo vo) {
        return redisUtil.set(GoblinRedisConst.BASIC_SELF_GOODS_CATEGORY_SPEC.concat(cateId), vo);
    }

    public GoblinMgtCategorySpecVo getCategorySpec(String cateId) {
        String rk = GoblinRedisConst.BASIC_SELF_GOODS_CATEGORY_SPEC.concat(cateId);
        GoblinMgtCategorySpecVo vo = (GoblinMgtCategorySpecVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getCategorySpecVo(cateId))) {
            redisUtil.set(rk, vo);
        }
        return vo;
    }

    /* ---------------------------------------- 服务支持数据源 ---------------------------------------- */

    public List<GoblinServiceSupportVo> getServiceSupportVos() {
        String valStr = (String) redisUtil.get(GoblinRedisConst.BASIC_SERVICE_SUPPORT);
        boolean valStrIsEmptyFlg = StringUtils.isEmpty(valStr);
        List<GoblinServiceSupportVo> vos;
        if (valStrIsEmptyFlg) {
            if (!CollectionUtils.isEmpty(vos = goblinMongoUtils.getMgtServiceSupportVos())) {
                redisUtil.set(GoblinRedisConst.BASIC_SERVICE_SUPPORT, JsonUtils.toJson(vos));
            }
        } else {
            vos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinServiceSupportVo>>() {
            });
        }
        return vos;
    }

    /* ---------------------------------------- 店铺公告数据源 ---------------------------------------- */

    public GoblinStoreNoticeVo getStoreNoticeVo(String storeId, LocalDateTime nowTime) {
        List<GoblinStoreNoticeVo> noticeVos = this.getStoreNoticeVos(storeId, nowTime);
        int size = noticeVos.size();
        for (int i = size - 1; i >= 0; i--) {
            GoblinStoreNoticeVo noticeVo = noticeVos.get(i);
            if (noticeVo.getReleaseTime().isBefore(nowTime)) {
                noticeVos.removeIf(r -> (
                        (r.getLongLasting().equals("0") && r.getCancellTime().isBefore(nowTime))
                                || r.getReleaseTime().isBefore(noticeVo.getReleaseTime())
                ));
                this.setStoreNoticeVos(storeId, noticeVos);
                return noticeVo;
            }
        }
        return null;
    }

    public List<GoblinStoreNoticeVo> getStoreNoticeVos(String storeId, LocalDateTime nowTime) {
        String rk = GoblinRedisConst.STORE_NOTICE.concat(storeId);
        List<GoblinStoreNoticeVo> vos = (List<GoblinStoreNoticeVo>) redisUtil.get(rk);
        if (CollectionUtils.isEmpty(vos) && !CollectionUtils.isEmpty(vos = goblinMongoUtils.getStoreNoticeVos(storeId, nowTime))) {
//            redisUtil.set(rk, vos);
        }
        return vos;
    }

    public boolean setStoreNoticeVos(String storeId, List<GoblinStoreNoticeVo> vos) {
        return redisUtil.set(GoblinRedisConst.STORE_NOTICE.concat(storeId), vos);
    }

    /* ---------------------------------------- 店铺配置数据源 ---------------------------------------- */

    public List<GoblinStoreConfigVo> getStoreConfigVos(String storeId) {
        String rk = GoblinRedisConst.BASIC_STORE_CONF.concat(storeId);
        List<GoblinStoreConfigVo> vos = (List<GoblinStoreConfigVo>) redisUtil.get(rk);
        if (CollectionUtils.isEmpty(vos) && !CollectionUtils.isEmpty(vos = goblinMongoUtils.getStoreConfigVos(storeId))) {
            redisUtil.set(rk, vos);
        }
        return vos;
    }

    public boolean setStoreConfigVos(String storeId, List<GoblinStoreConfigVo> vos) {
        return redisUtil.set(GoblinRedisConst.BASIC_STORE_CONF.concat(storeId), vos);
    }

    public void delStoreConfigVos(String storeId) {
        redisUtil.del(GoblinRedisConst.BASIC_STORE_CONF.concat(storeId));
    }

    /* ---------------------------------------- 店铺数据源 ---------------------------------------- */

    private boolean setStoreIds(String uid, List<String> storeIds) {
        return redisUtil.set(GoblinRedisConst.BASIC_USTORE.concat(uid), storeIds);
    }

    public boolean addStoreId(String uid, String storeId) {
        List<String> storeIds = this.getStoreIds(uid);
        if (CollectionUtils.isEmpty(storeIds)) {
            storeIds = CollectionUtil.arrayListString();
        }
        storeIds.add(storeId);
        return this.setStoreIds(uid, storeIds);
    }

    public void delStoreIds(String uid) {
        redisUtil.del(GoblinRedisConst.BASIC_USTORE.concat(uid));
    }

    public void delStoreId(String uid, String storeId) {
        List<String> storeIds = this.getStoreIds(uid);
        if (!CollectionUtils.isEmpty(storeIds)) {
            storeIds.removeIf(r -> r.equals(storeId));
            this.setStoreIds(uid, storeIds);
        }
    }

    public List<String> getStoreIds(String uid) {
//        List<String> list = (List<String>) redisUtil.get(GoblinRedisConst.BASIC_USTORE.concat(uid));
//        return CollectionUtils.isEmpty(list) ? Arrays.asList("1") : list;
        return (List<String>) redisUtil.get(GoblinRedisConst.BASIC_USTORE.concat(uid));
    }

    public boolean hasStoreId(String uid, String storeId) {
        List<String> list = this.getStoreIds(uid);
        return !CollectionUtils.isEmpty(list) && list.contains(storeId);
    }

    public boolean setStoreInfoVo(GoblinStoreInfoVo vo) {
        List<String> storeIds = this.getStoreIds(vo.getUid());
        if (CollectionUtils.isEmpty(storeIds)) {
            storeIds = CollectionUtil.arrayListString();
        }
        storeIds.add(vo.getStoreId());
        this.setStoreIds(vo.getUid(), storeIds);
        return redisUtil.set(GoblinRedisConst.BASIC_STORE.concat(vo.getStoreId()), vo);
    }

    public void delStoreInfoVo(String storeId) {
        redisUtil.del(GoblinRedisConst.BASIC_STORE.concat(storeId));
    }

    public GoblinStoreInfoVo getStoreInfoVo(String storeId) {
        String rk = GoblinRedisConst.BASIC_STORE.concat(storeId);
        GoblinStoreInfoVo vo = (GoblinStoreInfoVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getStoreInfoVo(storeId))) {
            redisUtil.set(rk, vo);
        }
        return vo;
    }

    public GoblinStoreInfoVo getStoreInfoVoByUid(String uid) {
        List<String> storeIds = this.getStoreIds(uid);
        GoblinStoreInfoVo storeInfoVo;
        if (CollectionUtils.isEmpty(storeIds)) {
            storeInfoVo = goblinMongoUtils.getStoreInfoVoByUid(uid);
            if (null != storeInfoVo) {
                storeIds = CollectionUtil.arrayListString();
                storeIds.add(storeInfoVo.getStoreId());
                this.setStoreIds(uid, storeIds);
            }
        } else {
            storeInfoVo = this.getStoreInfoVo(storeIds.get(0));
        }
        return storeInfoVo;
    }

    /* ---------------------------------------- 商品数据源 ---------------------------------------- */

    public boolean setGoodsInfoVo(GoblinGoodsInfoVo vo) {
        return redisUtil.set(GoblinRedisConst.BASIC_GOODS.concat(vo.getSpuId()), vo);
    }

    public void delGoodsInfoVo(String spuId) {
        redisUtil.del(GoblinRedisConst.BASIC_GOODS.concat(spuId));
    }

    public void delGoodsInfoVoByUnShelves(String spuId) {
        redisUtil.del(GoblinRedisConst.BASIC_GOODS_UNSHELVES.concat(spuId));
    }

    /**
     * 商品基础信息
     *
     * @param spuId 商品ID
     * @return GoblinGoodsInfoVo
     */
    public GoblinGoodsInfoVo getGoodsInfoVo(String spuId) {
        String rk = GoblinRedisConst.BASIC_GOODS.concat(spuId);
        GoblinGoodsInfoVo vo = (GoblinGoodsInfoVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getGoodsInfoVo(spuId))) {
            if (vo.getShelvesStatus().equals("3")) {
                redisUtil.set(rk, vo);
            } else {
                vo = null;
            }
        }
        return vo;
    }

    public GoblinGoodsInfoVo getGoodsInfoVoByUnShelves(String spuId) {
        String rk = GoblinRedisConst.BASIC_GOODS_UNSHELVES.concat(spuId);
        GoblinGoodsInfoVo vo = (GoblinGoodsInfoVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getGoodsInfoVo(spuId))) {
            if (vo.getShelvesStatus().equals("1")) {
                redisUtil.set(rk, vo);
            } else {
                vo = null;
            }
        }
        return vo;
    }

    public GoblinGoodsInfoVo getMgtGoodsInfoVo(String spuId) {
        String rk = GoblinRedisConst.BASIC_GOODS.concat(spuId);
        GoblinGoodsInfoVo vo = (GoblinGoodsInfoVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getMgtGoodsInfoVo(spuId))) {
            redisUtil.set(rk, vo);
        }
        return vo;
    }

    public boolean setGoodsSkuInfoVo(GoblinGoodsSkuInfoVo vo) {
        return redisUtil.set(GoblinRedisConst.BASIC_GOODS_SKU.concat(vo.getSkuId()), vo);
    }

    public void delGoodsSkuInfoVo(String skuId) {
        redisUtil.del(GoblinRedisConst.BASIC_GOODS_SKU.concat(skuId));
    }

    public void delGoodsSkuInfoVoByUnShelves(String skuId) {
        redisUtil.del(GoblinRedisConst.BASIC_GOODS_SKU_UNSHELVES.concat(skuId));
    }

    /**
     * 单品信息
     *
     * @param skuId 单品ID
     * @return GoblinGoodsSkuInfoVo
     */
    public GoblinGoodsSkuInfoVo getGoodsSkuInfoVo(String skuId) {
        String rk = GoblinRedisConst.BASIC_GOODS_SKU.concat(skuId);
        GoblinGoodsSkuInfoVo vo = (GoblinGoodsSkuInfoVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getGoodsSkuInfoVo(skuId))) {
            if (vo.getShelvesStatus().equals("3")) {
                redisUtil.set(rk, vo);
            } else {
                vo = null;
            }
        }
        return vo;
    }

    public GoblinGoodsSkuInfoVo getGoodsSkuInfoVoByUnShelves(String skuId) {
        String rk = GoblinRedisConst.BASIC_GOODS_SKU_UNSHELVES.concat(skuId);
        GoblinGoodsSkuInfoVo vo = (GoblinGoodsSkuInfoVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getGoodsSkuInfoVo(skuId))) {
            if (vo.getShelvesStatus().equals("1")) {
                redisUtil.set(rk, vo);
            } else {
                vo = null;
            }
        }
        return vo;
    }

    public GoblinGoodsSkuInfoVo getMgtGoodsSkuInfoVo(String skuId) {
        String rk = GoblinRedisConst.BASIC_GOODS_SKU.concat(skuId);
        GoblinGoodsSkuInfoVo vo = (GoblinGoodsSkuInfoVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getMgtGoodsSkuInfoVo(skuId))) {
            redisUtil.set(rk, vo);
        }
        return vo;
    }


    /* ----------------------------------------  ---------------------------------------- */

    /**
     * 覆盖 商铺活动vo
     *
     * @param storeMarketId
     * @param vo
     * @return
     */
    public boolean setGoblinStoreMarketVo(String storeMarketId, GoblinStoreMarketVo vo) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_STORE_MARKET.concat(storeMarketId);
        return redisUtil.set(redisKey, vo);
    }

    /**
     * 获取 商铺活动vo
     *
     * @param storeMarketId
     * @return
     */
    public GoblinStoreMarketVo getGoblinStoreMarketVo(String storeMarketId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_STORE_MARKET.concat(storeMarketId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            GoblinStoreMarketVo vo = goblinMongoUtils.getStoreMarket(storeMarketId);
            setGoblinStoreMarketVo(storeMarketId, vo);
            return vo;
        } else {
            return (GoblinStoreMarketVo) obj;
        }
    }

    /**
     * 删除 商铺活动vo
     *
     * @param storeMarketId
     * @return
     */
    public void delGoblinStoreMarketVo(String storeMarketId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_STORE_MARKET.concat(storeMarketId);
        redisUtil.del(redisKey);
    }


    /**
     * 获取 配置的商铺id
     *
     * @param marketId
     * @param storeId
     */
    public List<GoblinStoreMarketIsConfigVo> getStoreMarketIsConfig(String marketId, String storeId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_STORE_MARKET_ISCONFIG.concat(marketId).concat(":store_id:" + storeId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return new ArrayList();
        } else {
            return (List<GoblinStoreMarketIsConfigVo>) obj;
        }
    }

    public void addStoreMarketIsConfig(String marketId, String storeId, String spuId, String marketSpuId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_STORE_MARKET_ISCONFIG.concat(marketId).concat(":store_id:" + storeId);
        List<GoblinStoreMarketIsConfigVo> voList = getStoreMarketIsConfig(marketId, storeId);
        GoblinStoreMarketIsConfigVo vo = GoblinStoreMarketIsConfigVo.getNew();
        vo.setMarketSpuId(marketSpuId);
        vo.setSpuId(spuId);
        voList.add(vo);
        redisUtil.set(redisKey, voList);
    }

    public void delStoreMarketIsConfig(String marketId, String storeId, String spuId, String marketSpuId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_STORE_MARKET_ISCONFIG.concat(marketId).concat(":store_id:" + storeId);
        List<GoblinStoreMarketIsConfigVo> voList = getStoreMarketIsConfig(marketId, storeId);
        for (int i = 0; i < voList.size(); i++) {
            String itemSpuId = voList.get(i).getSpuId();
            if (spuId.equals(itemSpuId)) {
                voList.remove(i);
            }
        }
        redisUtil.set(redisKey, voList);
    }

    /**
     * 添加 活动内商品款式关联关系
     *
     * @param marketPre
     * @param marketId
     * @param vo
     */
    public void addMarketRelation(String marketPre, String marketId, GoblinMarketRelationVo vo) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_MARKET_RELATION.concat(marketPre).concat(marketId);
        List<GoblinMarketRelationVo> voList = getMarketRelation(marketPre, marketId);
        voList.add(vo);
        redisUtil.set(redisKey, voList);
    }

    /**
     * 移除 活动内商品款式关联关系
     *
     * @param marketPre
     * @param marketId
     */
    public void removeMarketRelation(String marketPre, String marketId, String spuId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_MARKET_RELATION.concat(marketPre).concat(marketId);
        List<GoblinMarketRelationVo> voList = getMarketRelation(marketPre, marketId);
        for (GoblinMarketRelationVo item : voList) {
            if (item.getSpuId().equals(spuId)) {
                voList.remove(item);
                break;
            }
        }
        redisUtil.set(redisKey, voList);
    }

    /**
     * 获取 活动内商品款式关联关系
     *
     * @param marketPre
     * @param marketId
     */
    public List<GoblinMarketRelationVo> getMarketRelation(String marketPre, String marketId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_MARKET_RELATION.concat(marketPre).concat(marketId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return new ArrayList();
        } else {
            return (List<GoblinMarketRelationVo>) obj;
        }
    }

    /**
     * 获取 当前商铺能参与的官方活动id列表
     *
     * @param storeId
     * @return
     */
    public List<String> getStoreZhengzaiRelation(String storeId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_STORE_SELF_RELATION.concat(storeId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return CollectionUtil.arrayListString();
        } else {
            return (List<String>) obj;
        }
    }

    /**
     * 正在下单 可参加商户关系 详情
     *
     * @param selfMarketId
     * @param storeId
     */
    public GoblinMarketingZhengzaiRelationVo getZhengzaiRelation(String selfMarketId, String storeId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ZHENGZAI_RELATION.concat(selfMarketId).concat(":store_id:" + storeId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (GoblinMarketingZhengzaiRelationVo) obj;
        }
    }

    /**
     * 正在下单 可参加商户 详情
     *
     * @param selfMarketId
     */
    public GoblinSelfMarketingVo getSelfMarket(String selfMarketId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_SELF_MARKET.concat(selfMarketId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (GoblinSelfMarketingVo) obj;
        }
    }

    //获取全部正在下单的活动
    public List<GoblinSelfMarketingVo> getGoblinSelfMarketingVoList() {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ZHENGZAI_LIST;
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return goblinMongoUtils.getGoblinSelfMarketingVoList();
        } else {
            return (List<GoblinSelfMarketingVo>) obj;
        }
    }

    //获取 用户sku购买个数
    public Integer getSkuCountByUid(String uid, String skuId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_BUY_COUNT.concat(uid + ":skuId:" + skuId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return 0;
        } else {
            return (Integer) obj;
        }
    }

    //获取 用户是否可购买
    public boolean getSkuCanBuyByUid(String mobile, String skuId) {
        String redisKey = GoblinRedisConst.REDIS_CAN_BUY.concat(skuId + ":").concat(mobile);
        return redisUtil.hasKey(redisKey);
    }

    // 增加 用户sku购买个数
    public int incrSkuCountByUid(String uid, String skuId, int number) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_BUY_COUNT.concat(uid + ":skuId:" + skuId);
        return (int) redisUtil.incr(redisKey, number);
    }

    // 减少 用户sku购买个数
    public int decrSkuCountByUid(String uid, String skuId, int number) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_BUY_COUNT.concat(uid + ":skuId:" + skuId);
        return (int) redisUtil.decr(redisKey, number);
    }

    //获取 sku销量
    public Integer getSkuSaleCount(String skuId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_SALE_COUNT.concat(skuId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (Integer) obj;
        }
    }

    // 增加 sku销量
    public int incrSkuSaleCount(String spuId, String skuId, int number) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_SALE_COUNT.concat(skuId);
        incrSpuSaleCount(spuId, number);
        return (int) redisUtil.incr(redisKey, number);
    }

    // 减少 sku销量
    public int decrSkuSaleCount(String spuId, String skuId, int number) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_SALE_COUNT.concat(skuId);
        decrSpuSaleCount(spuId, number);
        return (int) redisUtil.decr(redisKey, number);
    }

    //获取 spu销量
    public Integer getSpuSaleCount(String spuId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_SALE_SPU_COUNT.concat(spuId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (Integer) obj;
        }
    }

    // 增加 spu销量
    private int incrSpuSaleCount(String spuId, int number) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_SALE_SPU_COUNT.concat(spuId);
        return (int) redisUtil.incr(redisKey, number);
    }

    // 减少 spu销量
    private int decrSpuSaleCount(String spuId, int number) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_SALE_SPU_COUNT.concat(spuId);
        return (int) redisUtil.decr(redisKey, number);
    }

    // 赋值 订单相关vo
    public void setGoblinOrder(String orderId, GoblinStoreOrderVo vo) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER.concat(orderId);
        redisUtil.set(redisKey, vo);
    }


    // 赋值 订单相关Skuvo
    public void setGoblinOrderSku(String orderSkuId, GoblinOrderSkuVo vo) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_SKU.concat(orderSkuId);
        redisUtil.set(redisKey, vo);
    }

    // 赋值 快递vo
    public void addGoblinMail(String orderId, GoblinMailVo vo) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_MAIL.concat(orderId);
        List<GoblinMailVo> list = getGoblinMail(orderId);
        list.add(vo);
        redisUtil.set(redisKey, list);
    }

    // 修改 快递vo
    public void changeGoblinMail(String orderId, GoblinMailVo vo) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_MAIL.concat(orderId);
        List<GoblinMailVo> list = getGoblinMail(orderId);
        for (GoblinMailVo item : list) {
            if (item.getMailId().equals(vo.getMailId())) {
                item.setMailNo(vo.getMailNo());
                break;
            }
        }
        redisUtil.set(redisKey, list);
    }

    // 获取 快递vo
    public List<GoblinMailVo> getGoblinMail(String orderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_MAIL.concat(orderId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return ObjectUtil.goblinMailVo();
        } else {
            return (List<GoblinMailVo>) obj;
        }
    }


    // 获取 订单相关vo
    public GoblinStoreOrderVo getGoblinOrder(String orderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER.concat(orderId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (GoblinStoreOrderVo) obj;
        }
    }

    // 获取 订单相关Skuvo
    public GoblinOrderSkuVo getGoblinOrderSkuVo(String orderSkuId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_SKU.concat(orderSkuId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (GoblinOrderSkuVo) obj;
        }
    }

    // 获取 未支付订单id列表
    public List<String> getUnPayOrder(String randomKey) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_UN_PAY.concat(randomKey);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return CollectionUtil.arrayListString();
        } else {
            return (List<String>) obj;
        }
    }

    // 添加 未支付订单id列表
    public void addUnPayOrder(String orderId) {
        int randomKey = RandomUtil.getRandomInt(0, randomMax);
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_UN_PAY.concat(randomKey + "");
        List<String> list = getUnPayOrder(randomKey + "");
        list.add(orderId);
        redisUtil.set(redisKey, list);
    }

    // 删除 未支付订单id列表
    public void removeGoblinOrder(String randomKey, String orderId) {
        if (randomKey == null) {
            for (int i = 0; i < randomMax; i++) {
                randomKey = i + "";
                String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_UN_PAY.concat(randomKey);
                List<String> list = getUnPayOrder(randomKey);
                if (list.contains(orderId)) {
                    list.remove(orderId);
                    redisUtil.set(redisKey, list);
                    break;
                }
            }
        } else {
            String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_UN_PAY.concat(randomKey + "");
            List<String> list = getUnPayOrder(randomKey);
            list.remove(orderId);
            redisUtil.set(redisKey, list);
        }
    }

    // 获取 订单id列表
    public List<String> getOrderList(String uid) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_LIST.concat(uid);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return CollectionUtil.arrayListString();
        } else {
            return (List<String>) obj;
        }
    }

    // 添加 订单id列表
    public void addOrderList(String uid, String orderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_LIST.concat(uid);
        List<String> list = getOrderList(uid);
        list.add(orderId);
        redisUtil.set(redisKey, list);
    }

    // 删除 订单id列表
    public void removeOrderList(String uid, String orderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_LIST.concat(uid);
        List<String> list = getOrderList(uid);
        list.remove(orderId);
        redisUtil.set(redisKey, list);
    }

    // 获取 订单id列表
    public List<String> getZhengzaiOrderList(String uid) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_ZHENGZAI_LIST.concat(uid);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return CollectionUtil.arrayListString();
        } else {
            return (List<String>) obj;
        }
    }

    // 添加 订单id列表
    public void addZhengzaiOrderList(String uid, String orderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_ZHENGZAI_LIST.concat(uid);
        List<String> list = getZhengzaiOrderList(uid);
        list.add(orderId);
        redisUtil.set(redisKey, list);
    }

    // 删除 订单id列表
    public void removeZhengzaiOrderList(String uid, String orderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_ZHENGZAI_LIST.concat(uid);
        List<String> list = getZhengzaiOrderList(uid);
        list.remove(orderId);
        redisUtil.set(redisKey, list);
    }


    //主订单下包含的子订单
    public void setMasterCode(String masterCode, String orderIds) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_MASTER.concat(masterCode);
        redisUtil.set(redisKey, orderIds);
    }

    public String[] getMasterCode(String masterCode) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_MASTER.concat(masterCode);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return ((String) obj).split(",");
        }
    }

    //主订单对应的出货吗
    public void setOffCode(String writeOffCode, String masterCode) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_OFFCODE.concat(writeOffCode);
        redisUtil.set(redisKey, masterCode);
    }

    public String[] getOffCode(String writeOffCode) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_OFFCODE.concat(writeOffCode);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return getMasterCode((String) obj);
        }
    }

    //覆盖 退款订单vo
    public void setBackOrderVo(String backOrderId, GoblinBackOrderVo vo) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_BACK.concat(backOrderId);
        redisUtil.set(redisKey, vo);
    }

    //覆盖 获取订单vo
    public GoblinBackOrderVo getBackOrderVo(String backOrderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_BACK.concat(backOrderId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (GoblinBackOrderVo) obj;
        }
    }

    // 获取 订单id下的退款订单id
    public List<String> getBackOrderByOrderId(String orderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_BACK_ORDER_ID.concat(orderId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return CollectionUtil.arrayListString();
        } else {
            return (List<String>) obj;
        }
    }

    // 添加 订单id下的退款订单id
    public void addBackOrderByOrderId(String orderId, String backOrderId) {
        String redisKey = GoblinRedisConst.REDIS_GOBLIN_BACK_ORDER_ID.concat(orderId);
        List<String> list = getBackOrderByOrderId(orderId);
        list.add(backOrderId);
        redisUtil.set(redisKey, list);
    }

    //商品上架、下架、删除 调用的方法
    public void deleteKeyForSelectGoods() {
        redisUtil.del(GoblinRedisConst.SELECT_GOODS_SPUIDS);
        redisUtil.del(GoblinRedisConst.SELECT_GOODS_PAGE1);
    }

    // banner 降级
    public List<GoblinFrontBanner> getListBanner() {
        List<GoblinFrontBanner> vo = (List<GoblinFrontBanner>) redisUtil.get(GoblinRedisConst.FRONT_TOP_BANNER);
        if (null == vo) {
            //降级
            return goblinMongoUtils.getListBanner();
        }
        return vo;
    }

    public List<GoblinFrontBanner> getMiddleBanner() {
        List<GoblinFrontBanner> vo = (List<GoblinFrontBanner>) redisUtil.get(GoblinRedisConst.FRONT_MIDDLE_BANNER);
        if (null == vo) {
            //降级
            return goblinMongoUtils.getMiddleBanner();
        }
        return vo;
    }

    // 热词降级
    public List<GoblinFrontHotWord> getHotWord() {
        List<GoblinFrontHotWord> vo = (List<GoblinFrontHotWord>) redisUtil.get(GoblinRedisConst.FRONT_HOTWORD);
        if (null == vo) {
            //降级
            return goblinMongoUtils.getHotWord();
        }
        return vo;
    }

    // 导航栏 降级
    //  List<GoblinFrontNavigation> list = (List<GoblinFrontNavigation>) redisUtil.get(GoblinRedisConst.FRONT_NAVIGATION);
    public List<GoblinFrontNavigation> getNavigation() {
        List<GoblinFrontNavigation> vo = (List<GoblinFrontNavigation>) redisUtil.get(GoblinRedisConst.FRONT_NAVIGATION);
        if (null == vo) {
            //降级
            return goblinMongoUtils.getNavigation();
        }
        return vo;

    }

    /* ---------------------------------------- 商铺活动:优惠券数据源 ---------------------------------------- */

    public GoblinStoreCouponVo getStoreCouponVo(String storeCouponId) {
        String rk = GoblinRedisConst.STORE_COUPON.concat(storeCouponId);
        GoblinStoreCouponVo vo = (GoblinStoreCouponVo) redisUtil.get(rk);
        if (null == vo && null != (vo = goblinMongoUtils.getStoreCouponVo(storeCouponId))) {
            redisUtil.set(rk, vo);
        }
        return vo;
    }

    public List<String> getStoreCouponSpuIds(String storeCouponId) {
        String rk = GoblinRedisConst.STORE_COUPON_RULE.concat(storeCouponId);
        String valStr = (String) redisUtil.get(rk);
        List<String> strs;
        if (StringUtils.isEmpty(valStr)) {
            GoblinStoreCouponBasicVo storeCouponBasicVo = goblinMongoUtils.getMgtStoreCouponBasicVo(storeCouponId);
            strs = null == storeCouponBasicVo ? null : storeCouponBasicVo.getSpuIdList();
            if (!CollectionUtils.isEmpty(strs)) {
                redisUtil.set(rk, JsonUtils.toJson(strs));
            }
        } else {
            strs = JsonUtils.fromJson(valStr, new TypeReference<List<String>>() {
            });
        }
        return strs;
    }

    /* ---------------------------------------- 商铺活动:优惠券剩余库存 ---------------------------------------- */

    public boolean setStoreCouponStock(String storeCouponId, Integer stock) {
        return redisUtil.set(GoblinRedisConst.STORE_COUPON_STOCK.concat(storeCouponId), stock);
    }

    public void delStoreCouponStock(String storeCouponId) {
        redisUtil.del(GoblinRedisConst.STORE_COUPON_STOCK.concat(storeCouponId));
    }

    public int getStoreCouponStock(String storeCouponId) {
        Object valObj = redisUtil.get(GoblinRedisConst.STORE_COUPON_STOCK.concat(storeCouponId));
        return null == valObj ? 0 : (int) valObj;
    }

    public int incrStoreCouponStock(String storeCouponId, Integer operStock) {
        return (int) redisUtil.incr(GoblinRedisConst.STORE_COUPON_STOCK.concat(storeCouponId), operStock);
    }

    public int decrStoreCouponStock(String storeCouponId, Integer operStock) {
        return (int) redisUtil.decr(GoblinRedisConst.STORE_COUPON_STOCK.concat(storeCouponId), operStock);
    }

    /* ---------------------------------------- 商铺活动 ---------------------------------------- */

    public boolean setStoreMarketDtos(String storeId, List<GoblinStoreMarketDto> dtos) {
        return redisUtil.set(GoblinRedisConst.STORE_MARKETS.concat(storeId), JsonUtils.toJson(dtos));
    }

    public List<GoblinStoreMarketDto> getStoreMarketDtos(String storeId) {
        String rk = GoblinRedisConst.STORE_MARKETS.concat(storeId);
        String valStr = (String) redisUtil.get(rk);
        List<GoblinStoreMarketDto> dtos;
        if (StringUtils.isEmpty(valStr)) {
            // TODO: 2022/2/17 zhanggb mongodb 查取该商品所有参与的活动并整理集合
            dtos = ObjectUtil.getGoblinStoreMarketDtoArrayList();
        } else {
            dtos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinStoreMarketDto>>() {
            });
        }
        return dtos;
    }

    public boolean addStoreMarketDto(String storeId, GoblinStoreMarketDto dto) {
        List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(storeId);
        dtos.add(dto);
        return redisUtil.set(GoblinRedisConst.STORE_MARKETS.concat(storeId), JsonUtils.toJson(dtos));
    }

    public void delStoreMarketDto(String storeId) {
        redisUtil.del(GoblinRedisConst.STORE_MARKETS.concat(storeId));
    }

    public void delStoreMarketDto(String storeId, GoblinStoreMarketDto dto) {
        String rk = GoblinRedisConst.STORE_MARKETS.concat(storeId);
        List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(storeId);
        if (!CollectionUtils.isEmpty(dtos)) {
            int beforeSize = dtos.size();
            dtos.removeIf(r -> r.getId().equals(dto.getId()) && r.getType().equals(dto.getType()));
            if (beforeSize > dtos.size()) {
                redisUtil.set(GoblinRedisConst.STORE_MARKETS.concat(storeId), JsonUtils.toJson(dtos));
            }
        }
    }

    //获取艺人标签redis
    public List<GoblinGoodsInfoVo> getMusicTagList(String musicTag, String performanceId) {
        List<GoblinGoodsInfoVo> listVo = ObjectUtil.getGoblinGoodsInfoVos();
//        String rk = GoblinRedisConst.REDIS_ZZ_MUSIC_TAG.concat(musicTag + ":").concat(performanceId);
//        Object obj = redisUtil.get(rk);
//        if (obj == null) {
        listVo = goblinMongoUtils.getMusicTagPGoods(musicTag, performanceId);
//            redisUtil.set(rk, listVo);
        return listVo;
//        } else {
//            return (List<GoblinGoodsInfoVo>) obj;
//        }
    }

    //删除艺人标签redis
    public void delMusicTagList(String musicTag, String performanceId) {
        String rk = GoblinRedisConst.REDIS_ZZ_MUSIC_TAG.concat(musicTag + ":").concat(performanceId);
        redisUtil.del(rk);
    }

    /* ---------------------------------------- 商品活动 ---------------------------------------- */

    public List<GoblinStoreCouponVo> getGoodsMarketsForCoupon(String spuId, String storeId) {
        List<GoblinStoreMarketDto> storeMarketDtos = this.getStoreMarketDtos(storeId);
        if (CollectionUtils.isEmpty(storeMarketDtos)) {
            return null;
        }
        ArrayList<GoblinStoreCouponVo> storeCouponVos = ObjectUtil.getGoblinStoreCouponVoArrayList();
        storeMarketDtos.forEach(dto -> {// 逐个活动筛选优惠券活动处理
            if (dto.getType() == 1) {// 优惠券活动
                GoblinStoreCouponVo storeCouponVo = this.getStoreCouponVo(dto.getId());
                if (null != storeCouponVo) {
                    if ("0".equals(storeCouponVo.getUseScope())) {
                        storeCouponVos.add(storeCouponVo);
                    } else {
                        List<String> storeCouponSpuIds = this.getStoreCouponSpuIds(spuId);
                        if (!CollectionUtils.isEmpty(storeCouponSpuIds) && storeCouponSpuIds.contains(spuId)) {
                            storeCouponVos.add(storeCouponVo);
                        }
                    }
                }
            }
        });
        return storeCouponVos;
    }

    /* ---------------------------------------- 商城:用户优惠券 ---------------------------------------- */

    public boolean setUserCouponVos(String uid, List<GoblinUserCouponVo> vos) {
        return redisUtil.set(GoblinRedisConst.USER_COUPON.concat(uid), JsonUtils.toJson(vos));
    }

    public List<GoblinUserCouponVo> getUserCouponVos(String uid) {
        String rk = GoblinRedisConst.USER_COUPON.concat(uid);
        String valStr = (String) redisUtil.get(rk);
        List<GoblinUserCouponVo> vos;
        if (StringUtils.isEmpty(valStr)) {
            if (!CollectionUtils.isEmpty(vos = goblinMongoUtils.getUserCouponVos(uid))) {
                redisUtil.set(rk, JsonUtils.toJson(vos));
            }
        } else {
            vos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinUserCouponVo>>() {
            });
        }
        return vos;
    }

    public boolean addUserCouponVos(String uid, GoblinUserCouponVo vo) {
        if (null == vo) return false;
        List<GoblinUserCouponVo> vos = this.getUserCouponVos(uid);
        vos.add(vo);
        return redisUtil.set(GoblinRedisConst.USER_COUPON.concat(uid), JsonUtils.toJson(vos));
    }

    /* ----------------------------------------  ---------------------------------------- */

}
