package com.liquidnet.service.goblin.service.impl.manage;

import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.SqlMapping;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.goblin.dto.GoblinGoodsSpecDto;
import com.liquidnet.service.goblin.dto.manage.GoblinStoreMgtGoodsSqbAddParam;
import com.liquidnet.service.goblin.dto.manage.GoblinStoreMgtGoodsSqbEditSkuParam;
import com.liquidnet.service.goblin.dto.manage.vo.GoblinMgtCategorySpecVo;
import com.liquidnet.service.goblin.dto.vo.*;
import com.liquidnet.service.goblin.service.manage.IGoblinStoreMgtSqbGoodsService;
import com.liquidnet.service.goblin.util.GoblinMongoUtils;
import com.liquidnet.service.goblin.util.GoblinRedisUtils;
import com.liquidnet.service.goblin.util.ObjectUtil;
import com.liquidnet.service.goblin.util.QueueUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品管理:收钱吧商品服务实现
 */
@Slf4j
@Service
public class GoblinStoreMgtSqbGoodsServiceImpl implements IGoblinStoreMgtSqbGoodsService {

    @Autowired
    QueueUtils queueUtils;
    @Autowired
    GoblinRedisUtils goblinRedisUtils;
    @Autowired
    GoblinMongoUtils goblinMongoUtils;

    public void sqbGoodsAdd(GoblinGoodsInfoVo goodsInfoVo, List<GoblinGoodsSkuInfoVo> goodsSkuInfoVoList) {
        goblinMongoUtils.setGoodsInfoVo(goodsInfoVo);
        goblinMongoUtils.setGoodsSkuInfoVos(goodsSkuInfoVoList);

        if (goodsInfoVo.getShelvesHandle().equals("2")) {
            goblinRedisUtils.deleteKeyForSelectGoods();// 精选商品:商品上架、下架、删除 调用的方法
        }

        String createdBy = goodsInfoVo.getCreatedBy();
        LocalDateTime createdAt = goodsInfoVo.getCreatedAt();
        String spuId = goodsInfoVo.getSpuId();

        LinkedList<Object[]> initGoodsSkuObjs = CollectionUtil.linkedListObjectArr();
        LinkedList<Object[]> initGoodsSkuSpecValueObjs = CollectionUtil.linkedListObjectArr();
        LinkedList<Object[]> initGoodsCategorySpecObjs = CollectionUtil.linkedListObjectArr();
        {// 分类规格记录
            String cateFid = goodsInfoVo.getCateFid(), cateSid = goodsInfoVo.getCateSid(),
                    cateTid = goodsInfoVo.getCateTid();
            String filterCateId = StringUtils.isBlank(cateTid) ? (StringUtils.isBlank(cateSid) ? cateFid : cateSid)
                    : cateTid;
            GoblinMgtCategorySpecVo mgtCategorySpecVoCache = goblinRedisUtils.getCategorySpec(filterCateId);// 分类绑定的规格信息
            List<String> addSpecNameList = goodsSkuInfoVoList.get(0).getSkuSpecList().stream()
                    .map(GoblinGoodsSpecDto::getSpecName).collect(Collectors.toList());
            if (null == mgtCategorySpecVoCache) {// 根据分类ID未查取到规格信息，则Cache、数据库新增
                GoblinMgtCategorySpecVo initMgtCategorySpecVo = GoblinMgtCategorySpecVo.getNew().setCateId(filterCateId)
                        .setSpecNameList(addSpecNameList);
                goblinMongoUtils.setCategorySpecVo(initMgtCategorySpecVo);
                goblinRedisUtils.setCategorySpec(filterCateId, initMgtCategorySpecVo);
                addSpecNameList.forEach(r -> initGoodsCategorySpecObjs.add(new Object[] { filterCateId, r }));
            } else {// 根据分类ID查取到规格信息，则进一步比对判断是否新增
                List<String> confirmAddSpecNameList = addSpecNameList.stream()
                        .filter(r -> !mgtCategorySpecVoCache.getSpecNameList().contains(r))
                        .collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(confirmAddSpecNameList)) {// 不存在于`mgtCategorySpecVoCache`的，则更新Cache，数据库新增
                    mgtCategorySpecVoCache.getSpecNameList().addAll(confirmAddSpecNameList);
                    goblinMongoUtils.updateCategorySpecVo(mgtCategorySpecVoCache);
                    goblinRedisUtils.setCategorySpec(filterCateId, mgtCategorySpecVoCache);
                    confirmAddSpecNameList
                            .forEach(r -> initGoodsCategorySpecObjs.add(new Object[] { filterCateId, r }));
                }
            }
        }

        LinkedList<Object[]> initGoodsSkuCouponObjs = CollectionUtil.linkedListObjectArr();// 券类商品信息
        LinkedList<Object[]> initCandyCouponObjs = CollectionUtil.linkedListObjectArr();// 券信息
        LinkedList<Object[]> initCandyCouponRuleObjs = CollectionUtil.linkedListObjectArr();// 券规则信息
        int skuSize = goodsSkuInfoVoList.size();
        for (int i = 0; i < skuSize; i++) {
            GoblinGoodsSkuInfoVo skuInfoVo = goodsSkuInfoVoList.get(i);
            String skuId = skuInfoVo.getSkuId();
            String buyRoster = skuInfoVo.getBuyRoster();

            goblinRedisUtils.setSkuStock(null, skuId, skuInfoVo.getSkuStock());

            initGoodsSkuObjs.add(new Object[] {
                    skuId, skuInfoVo.getSpuId(), skuInfoVo.getSkuNo(), skuInfoVo.getSkuBarCode(),
                    skuInfoVo.getSkuErpCode(),
                    skuInfoVo.getErpType(), skuInfoVo.getErpHosting(), skuInfoVo.getErpWarehouseNo(),
                    skuInfoVo.getSkuType(), skuInfoVo.getName(),
                    skuInfoVo.getSubtitle(), skuInfoVo.getSellPrice(), skuInfoVo.getSkuPic(), skuInfoVo.getSkuIsbn(),
                    skuInfoVo.getStock(),
                    skuInfoVo.getSkuStock(), skuInfoVo.getWarningStock(), skuInfoVo.getPrice(),
                    skuInfoVo.getPriceMember(), skuInfoVo.getWeight(),
                    skuInfoVo.getBuyFactor(), buyRoster, skuInfoVo.getBuyLimit(), skuInfoVo.getStoreId(),
                    skuInfoVo.getSkuValidity(),
                    skuInfoVo.getVirtualFlg(), skuInfoVo.getStatus(), skuInfoVo.getShelvesStatus(),
                    skuInfoVo.getSkuAppear(), skuInfoVo.getShelvesAt(),
                    createdBy, createdAt, skuInfoVo.getLogisticsTemplate()
            });
            if (2 == goodsInfoVo.getSpuType()) {// 券类商品为一个SPU对应一个SKU
                initGoodsSkuCouponObjs.add(
                        new Object[] { skuId, skuInfoVo.getBusiType(), skuInfoVo.getCouType(), skuInfoVo.getUseScope(),
                                skuInfoVo.getValFace(), skuInfoVo.getIsTrueName(), skuInfoVo.getValidity(),
                                skuInfoVo.getEffectAt(), skuInfoVo.getExpireAt() });
                initCandyCouponObjs.add(
                        new Object[] { skuId, goodsInfoVo.getName(), goodsInfoVo.getIntro(), skuInfoVo.getBusiType(),
                                skuInfoVo.getCouType(), skuInfoVo.getValFace(), skuInfoVo.getValidity(),
                                skuInfoVo.getIsTrueName(),
                                skuInfoVo.getEffectAt(), skuInfoVo.getExpireAt(), createdBy, createdAt
                        });
                String busiName = "%s";
                switch (skuInfoVo.getUseScope()) {// 适用范围[101-音乐节｜102-小型演出（livehouse演出）｜103-巡演]
                    case 101:// 音乐节
                        busiName = String.format(busiName, "草莓、M_DSK、五百里音乐节");// 按杨要求文案'音乐节'改'草莓、M_DSK、五百里音乐节'
                        break;
                    case 102:// 小型演出（LiveHouse演出）
                        busiName = String.format(busiName, "小型演出（LiveHouse演出）系列");
                        break;
                    case 103:// 巡演
                        busiName = String.format(busiName, "巡演 系列");
                        break;
                }
                initCandyCouponRuleObjs
                        .add(new Object[] { IDGenerator.nextTimeId2(), skuId, skuInfoVo.getUseScope(), busiName, "" });
            }

            skuInfoVo.getSkuSpecList().forEach(skuSpecDto -> initGoodsSkuSpecValueObjs.add(new Object[] {
                    spuId, skuId, skuSpecDto.getSpecName(), skuSpecDto.getSpecVname()
            }));
            if (skuInfoVo.getBuyFactor().equals("2") && StringUtils.isNotBlank(buyRoster)
                    && buyRoster.startsWith("http")) {
                queueUtils.sendMsgByRedisXls(buyRoster, "1", skuId);
            }
        }

        LinkedList<String> toMqSqls = CollectionUtil.linkedListString();
        toMqSqls.add(SqlMapping.get("goblin_goods.insert_for_coupon"));
        LinkedList<Object[]> initGoodsObjs = CollectionUtil.linkedListObjectArr();
        initGoodsObjs.add(new Object[] {
                spuId, goodsInfoVo.getSpuNo(), goodsInfoVo.getSpuBarCode(), goodsInfoVo.getSpuErpCode(),
                goodsInfoVo.getErpType(), goodsInfoVo.getSpuType(),
                goodsInfoVo.getName(), goodsInfoVo.getSubtitle(), goodsInfoVo.getSellPrice(), goodsInfoVo.getPriceGe(),
                goodsInfoVo.getPriceLe(),
                goodsInfoVo.getIntro(), goodsInfoVo.getDetails(), goodsInfoVo.getCoverPic(), goodsInfoVo.getVideo(),
                goodsInfoVo.getSpecMode(),
                goodsInfoVo.getStoreId(), goodsInfoVo.getCateFid(), goodsInfoVo.getCateSid(), goodsInfoVo.getCateTid(),
                goodsInfoVo.getStoreCateFid(),
                goodsInfoVo.getStoreCateSid(), goodsInfoVo.getStoreCateTid(), goodsInfoVo.getBrandId(),
                goodsInfoVo.getShelvesHandle(), goodsInfoVo.getShelvesTime(),
                goodsInfoVo.getSpuValidity(), goodsInfoVo.getVirtualFlg(), goodsInfoVo.getStatus(),
                goodsInfoVo.getShelvesStatus(), goodsInfoVo.getSpuAppear(),
                goodsInfoVo.getShelvesAt(), createdBy, createdAt, goodsInfoVo.getLogisticsTemplate()
        });
        toMqSqls.add(SqlMapping.get("goblin_goods_sku.insert_for_coupon"));
        toMqSqls.add(SqlMapping.get("goblin_goods_sku_coupon.insert"));
        toMqSqls.add(SqlMapping.get("candy_coupon.goods_insert"));
        toMqSqls.add(SqlMapping.get("candy_coupon_rule.goods_insert"));
        toMqSqls.add(SqlMapping.get("goblin_goods_image.insert_byreplace"));
        LinkedList<Object[]> initGoodsImageObjs = CollectionUtil.linkedListObjectArr();
        if (CollectionUtils.isEmpty(goodsInfoVo.getImageList())) {
            goodsInfoVo.getImageList().forEach(imageUrl -> initGoodsImageObjs.add(new Object[] { spuId, imageUrl }));
        }
        toMqSqls.add(SqlMapping.get("goblin_goods_tag.insert_byreplace"));
        LinkedList<Object[]> initGoodsTagObjs = CollectionUtil.linkedListObjectArr();
        if (!CollectionUtils.isEmpty(goodsInfoVo.getTagVoList())) {
            goodsInfoVo.getTagVoList().forEach(
                    tagVo -> initGoodsTagObjs.add(new Object[] { spuId, tagVo.getTagId(), tagVo.getSort(), "0" }));
        }
        if (!CollectionUtils.isEmpty(goodsInfoVo.getExtagVoList())) {
            goodsInfoVo.getExtagVoList().forEach(exTagVo -> initGoodsTagObjs
                    .add(new Object[] { spuId, exTagVo.getTagId(), exTagVo.getSort(), "1" }));
        }
        toMqSqls.add(SqlMapping.get("goblin_goods_artag.insert_byreplace"));
        LinkedList<Object[]> initGoodsArTagObjs = CollectionUtil.linkedListObjectArr();
        if (!CollectionUtils.isEmpty(goodsInfoVo.getArtagVoList())) {
            goodsInfoVo.getArtagVoList().forEach(arTagVo -> initGoodsArTagObjs
                    .add(new Object[] { spuId, arTagVo.getTagId(), arTagVo.getSort(), "1" }));
        }
        toMqSqls.add(SqlMapping.get("goblin_goods_service_support.insert_byreplace"));
        LinkedList<Object[]> initGoodsServiceSupportObjs = CollectionUtil.linkedListObjectArr();
        if (!CollectionUtils.isEmpty(goodsInfoVo.getServiceSupportVoList())) {
            goodsInfoVo.getServiceSupportVoList()
                    .forEach(ssvo -> initGoodsServiceSupportObjs.add(new Object[] { spuId, ssvo.getSsid() }));
        }
        toMqSqls.add(SqlMapping.get("goblin_goods_spec.insert_byreplace"));
        LinkedList<Object[]> initGoodsSpecObjs = CollectionUtil.linkedListObjectArr();
        toMqSqls.add(SqlMapping.get("goblin_goods_spec_value.insert_byreplace"));
        LinkedList<Object[]> initGoodsSpecValueObjs = CollectionUtil.linkedListObjectArr();
        toMqSqls.add(SqlMapping.get("goblin_goods_spu_spec_value.insert_byreplace"));// SPU规格信息
        LinkedList<Object[]> initGoodsSpuSpecValueObjs = CollectionUtil.linkedListObjectArr();
        toMqSqls.add(SqlMapping.get("goblin_goods_sku_spec_value.insert_byreplace"));// SKU规格信息
        {// 规格记录
            List<GoblinGoodsSpecVo> specVoList = goodsInfoVo.getSpecVoList();
            specVoList.forEach(s -> {
                initGoodsSpecObjs.add(new Object[] { s.getSpecName(), createdAt });
                s.getSpecValues().forEach(sv -> {
                    initGoodsSpecValueObjs.add(new Object[] { s.getSpecName(), sv.getSpecVname(), createdAt });
                    initGoodsSpuSpecValueObjs
                            .add(new Object[] { spuId, s.getSpecName(), sv.getSpecVname(), sv.getSpecVsort() });
                });
            });
        }
        toMqSqls.add(SqlMapping.get("goblin_goods_category_spec.insert_byreplace"));// 分类关联规格信息

        queueUtils.sendMsgByRedis(MQConst.GoblinQueue.SQL_GOODS.getKey(),
                SqlMapping.gets(toMqSqls, initGoodsObjs, initGoodsSkuObjs, initGoodsSkuCouponObjs, initCandyCouponObjs,
                        initCandyCouponRuleObjs,
                        initGoodsImageObjs, initGoodsTagObjs, initGoodsArTagObjs, initGoodsServiceSupportObjs,
                        initGoodsSpecObjs,
                        initGoodsSpecValueObjs, initGoodsSpuSpecValueObjs, initGoodsSkuSpecValueObjs,
                        initGoodsCategorySpecObjs));
    }

    public boolean sqbGoodsEditSpu(String uid, GoblinStoreMgtGoodsSqbAddParam mgtGoodsAddParam,
            GoblinGoodsInfoVo mgtGoodsInfoVo) {
        String spuId = mgtGoodsAddParam.getSpuId();

        GoblinGoodsInfoVo updateSpuInfoVo = mgtGoodsAddParam.initEditGoodsInfoVo();

        boolean updateTagFlg = false, updateExtagFlg = false, updateArtagFlg = false;
        List<GoblinGoodsTagVo> befTagVoList = mgtGoodsInfoVo.getTagVoList();
        boolean befTagIsEmptyFlg = CollectionUtils.isEmpty(befTagVoList);
        List<GoblinGoodsExtagVo> befExtagVoList = mgtGoodsInfoVo.getExtagVoList();
        boolean befExtagIsEmptyFlg = CollectionUtils.isEmpty(befExtagVoList);
        List<GoblinGoodsExtagVo> befArtagVoList = mgtGoodsInfoVo.getArtagVoList();
        boolean befArtagIsEmptyFlg = CollectionUtils.isEmpty(befArtagVoList);
        boolean paramTagIsEmptyFlg, paramExtagIsEmptyFlg, paramArtagIsEmptyFlg;
        List<GoblinGoodsTagVo> paramTagVoList = ObjectUtil.getGoblinGoodsTagVoArrayList();
        List<GoblinGoodsExtagVo> paramExtagVoList = ObjectUtil.getGoblinGoodsExtagVoArrayList();
        List<GoblinGoodsExtagVo> paramArtagVoList = ObjectUtil.getGoblinGoodsExtagVoArrayList();
        {// 标签处理
            List<String> goodsTagIdList = mgtGoodsAddParam.getTagList(),
                    goodsExtagIdList = mgtGoodsAddParam.getExtagList(),
                    goodsArtagIdList = mgtGoodsAddParam.getArtagList();
            if (!CollectionUtils.isEmpty(goodsTagIdList)) {
                List<GoblinSelfTagVo> selfGoodsTagVos = goblinRedisUtils.getSelfTagVos();
                for (int i = 0; i < goodsTagIdList.size(); i++)
                    for (GoblinSelfTagVo r : selfGoodsTagVos)
                        if (r.getTagId().equals(goodsTagIdList.get(i)) && r.getTagBelong().equals("0")) {
                            paramTagVoList.add(GoblinGoodsTagVo.getNew().copy(r).setSort(i));
                            break;
                        }
            }
            List<GoblinSelfTagVo> selfExtagVos = null;
            if (!CollectionUtils.isEmpty(goodsExtagIdList)) {
                selfExtagVos = goblinRedisUtils.getSelfExtagVos();
                for (int i = 0; i < goodsExtagIdList.size(); i++)
                    for (GoblinSelfTagVo r : selfExtagVos)
                        if (r.getTagId().equals(goodsExtagIdList.get(i)) && r.getTagBelong().equals("1")
                                && !r.getTagType().equals("5")) {
                            paramExtagVoList.add(GoblinGoodsExtagVo.getNew().copy(r).setSort(i));
                            break;
                        }
            }
            if (!CollectionUtils.isEmpty(goodsArtagIdList)) {
                selfExtagVos = CollectionUtils.isEmpty(selfExtagVos) ? goblinRedisUtils.getSelfExtagVos()
                        : selfExtagVos;
                for (int i = 0; i < goodsArtagIdList.size(); i++)
                    for (GoblinSelfTagVo r : selfExtagVos)
                        if (r.getTagId().equals(goodsArtagIdList.get(i)) && r.getTagBelong().equals("1")) {
                            paramArtagVoList.add(GoblinGoodsExtagVo.getNew().copy(r).setSort(i));
                            break;
                        }
            }

            paramTagIsEmptyFlg = CollectionUtils.isEmpty(paramTagVoList);
            paramExtagIsEmptyFlg = CollectionUtils.isEmpty(paramExtagVoList);
            paramArtagIsEmptyFlg = CollectionUtils.isEmpty(paramArtagVoList);
            if (!befTagIsEmptyFlg || !paramTagIsEmptyFlg) {
                if (!befTagIsEmptyFlg && !paramTagIsEmptyFlg) {
                    if (paramTagVoList.size() != befTagVoList.size()) {
                        updateTagFlg = true;
                    } else {
                        List<String> befTagIdList = befTagVoList.stream().map(GoblinGoodsTagVo::getTagId)
                                .collect(Collectors.toList());
                        for (GoblinGoodsTagVo vo : paramTagVoList) {
                            if (!befTagIdList.contains(vo.getTagId())) {
                                updateTagFlg = true;
                                break;
                            }
                        }
                    }
                } else {
                    updateTagFlg = true;
                }
            }
            if (updateTagFlg) {
                updateSpuInfoVo
                        .setTagVoList(paramTagIsEmptyFlg ? ObjectUtil.getGoblinGoodsTagVoArrayList() : paramTagVoList);
            }

            if (!befExtagIsEmptyFlg || !paramExtagIsEmptyFlg) {
                if (!befExtagIsEmptyFlg && !paramExtagIsEmptyFlg) {
                    if (paramExtagVoList.size() != befExtagVoList.size()) {
                        updateExtagFlg = true;
                    } else {
                        List<String> befExtagIdList = befExtagVoList.stream().map(GoblinGoodsExtagVo::getTagId)
                                .collect(Collectors.toList());
                        for (GoblinGoodsExtagVo vo : paramExtagVoList) {
                            if (!befExtagIdList.contains(vo.getTagId())) {
                                updateExtagFlg = true;
                                break;
                            }
                        }
                    }
                } else {
                    updateExtagFlg = true;
                }
            }
            if (updateExtagFlg) {
                updateSpuInfoVo.setExtagVoList(
                        paramExtagIsEmptyFlg ? ObjectUtil.getGoblinGoodsExtagVoArrayList() : paramExtagVoList);
            }

            if (!befArtagIsEmptyFlg || !paramArtagIsEmptyFlg) {
                if (!befArtagIsEmptyFlg && !paramArtagIsEmptyFlg) {
                    if (paramArtagVoList.size() != befArtagVoList.size()) {
                        updateArtagFlg = true;
                    } else {
                        List<String> befArtagIdList = befArtagVoList.stream().map(GoblinGoodsExtagVo::getTagId)
                                .collect(Collectors.toList());
                        for (GoblinGoodsExtagVo vo : paramArtagVoList) {
                            if (!befArtagIdList.contains(vo.getTagId())) {
                                updateArtagFlg = true;
                                break;
                            }
                        }
                    }
                } else {
                    updateArtagFlg = true;
                }
            }
            if (updateArtagFlg) {
                updateSpuInfoVo.setArtagVoList(
                        paramArtagIsEmptyFlg ? ObjectUtil.getGoblinGoodsExtagVoArrayList() : paramArtagVoList);
            }
        }

        boolean updateSsidFlg = false;
        List<String> ssidList = mgtGoodsAddParam.getSsidList();
        boolean paramSsidListIsEmptyFlg = CollectionUtils.isEmpty(ssidList);
        List<GoblinServiceSupportVo> serviceSupportVoList = mgtGoodsInfoVo.getServiceSupportVoList();
        boolean befServiceSupportVoListIsEmptyFlg = CollectionUtils.isEmpty(serviceSupportVoList);
        {// 服务支持处理
            if (!befServiceSupportVoListIsEmptyFlg || !paramSsidListIsEmptyFlg) {
                if (!befServiceSupportVoListIsEmptyFlg && !paramSsidListIsEmptyFlg) {
                    if (serviceSupportVoList.size() != ssidList.size()) {
                        updateSsidFlg = true;
                    } else {
                        List<String> befSsidList = serviceSupportVoList.stream().map(GoblinServiceSupportVo::getSsid)
                                .collect(Collectors.toList());
                        for (String ssid : ssidList) {
                            if (!befSsidList.contains(ssid)) {
                                updateSsidFlg = true;
                                break;
                            }
                        }
                    }
                } else {
                    updateSsidFlg = true;
                }
            }
            if (updateSsidFlg) {
                updateSpuInfoVo.setServiceSupportVoList(
                        paramSsidListIsEmptyFlg ? ObjectUtil.getGoblinServiceSupportVoArrayList()
                                : goblinMongoUtils.getServiceSupportVos(ssidList));
            }
        }

        boolean updateImageFlg = false;
        List<String> paramImageList = mgtGoodsAddParam.getImageList();
        List<String> befImageList = mgtGoodsInfoVo.getImageList();
        {// 图片处理
            if (befImageList.size() != paramImageList.size()) {
                updateImageFlg = true;
            } else {
                for (String imageUrl : paramImageList) {
                    if (!befImageList.contains(imageUrl)) {
                        updateImageFlg = true;
                        break;
                    }
                }
            }
            if (updateImageFlg) {
                updateSpuInfoVo.setImageList(paramImageList);
            }
        }
        updateSpuInfoVo.setUpdatedBy(uid);
        updateSpuInfoVo.setUpdatedAt(LocalDateTime.now());
        if (goblinMongoUtils.updateGoodsInfoVo(updateSpuInfoVo)) {
            goblinRedisUtils.delGoodsInfoVo(updateSpuInfoVo.getSpuId());
            log.info("商品管理:SPU编辑[UID={},PARAMS={}]", uid, JsonUtils.toJson(mgtGoodsAddParam));
            GoblinGoodsSkuInfoVo updateSkuInfoVo = GoblinGoodsSkuInfoVo.getNew();
            updateSkuInfoVo.setSpuId(updateSpuInfoVo.getSpuId());
            updateSkuInfoVo.setSkuNo(updateSpuInfoVo.getSpuNo());
            // updateSkuInfoVo.setName(updateSpuInfoVo.getName());
            // updateSkuInfoVo.setSubtitle(updateSpuInfoVo.getSubtitle());
            updateSkuInfoVo.setVirtualFlg(updateSpuInfoVo.getVirtualFlg());
            updateSkuInfoVo.setLogisticsTemplate(updateSpuInfoVo.getLogisticsTemplate());
            updateSkuInfoVo.setSkuPic(updateSpuInfoVo.getCoverPic());// 默认使用SPU的
            updateSkuInfoVo.setUpdatedBy(updateSpuInfoVo.getUpdatedBy());
            updateSkuInfoVo.setUpdatedAt(updateSpuInfoVo.getUpdatedAt());

            LinkedList<Object[]> updateGoodsInfoObjs = CollectionUtil.linkedListObjectArr();
            LinkedList<Object[]> updateGoodsSkuObjs = CollectionUtil.linkedListObjectArr();
            // 参与活动的同一商品更新处理
            List<String> marketSpuIdList = goblinMongoUtils.updateGoodsInfoVoForMarket(spuId, mgtGoodsInfoVo.getSpuNo(),
                    updateSpuInfoVo);
            if (!CollectionUtils.isEmpty(marketSpuIdList)) {
                marketSpuIdList.forEach(marketSpuId -> {
                    goblinRedisUtils.delGoodsInfoVo(marketSpuId);
                    updateGoodsInfoObjs.add(new Object[] {
                            updateSpuInfoVo.getSpuNo(), updateSpuInfoVo.getName(), updateSpuInfoVo.getSubtitle(),
                            updateSpuInfoVo.getSellPrice(),
                            updateSpuInfoVo.getIntro(), updateSpuInfoVo.getDetails(), updateSpuInfoVo.getCoverPic(),
                            updateSpuInfoVo.getVideo(),
                            "2", updateSpuInfoVo.getCateFid(), updateSpuInfoVo.getCateSid(),
                            updateSpuInfoVo.getCateTid(),
                            updateSpuInfoVo.getShelvesHandle(), updateSpuInfoVo.getShelvesTime(),
                            updateSpuInfoVo.getSpuValidity(), updateSpuInfoVo.getVirtualFlg(),
                            updateSpuInfoVo.getLogisticsTemplate(), updateSpuInfoVo.getUpdatedBy(),
                            updateSpuInfoVo.getUpdatedAt(), updateSpuInfoVo.getSpuErpCode(),
                            marketSpuId
                    });
                    updateGoodsSkuObjs.add(new Object[] {
                            updateSkuInfoVo.getSkuNo(), updateSkuInfoVo.getVirtualFlg(),
                            updateSkuInfoVo.getLogisticsTemplate(),
                            updateSkuInfoVo.getSkuPic(), updateSkuInfoVo.getUpdatedBy(), updateSkuInfoVo.getUpdatedAt(),
                            marketSpuId
                    });
                });
            }

            // goblinMongoUtils.updateGoodsSkuInfoVoBySpuId(updateSkuInfoVo);
            goblinMongoUtils.updateGoodsSkuInfoVoPropBySpuId(updateSkuInfoVo);
            goblinRedisUtils.delGoodsSkuInfoVo(mgtGoodsInfoVo.getSkuIdList().get(0));

            LinkedList<String> toMqSqls = CollectionUtil.linkedListString();
            toMqSqls.add(SqlMapping.get("goblin_goods.update_by_edit"));
            updateGoodsInfoObjs.add(new Object[] {
                    updateSpuInfoVo.getSpuNo(), updateSpuInfoVo.getName(), updateSpuInfoVo.getSubtitle(),
                    updateSpuInfoVo.getSellPrice(),
                    updateSpuInfoVo.getIntro(), updateSpuInfoVo.getDetails(), updateSpuInfoVo.getCoverPic(),
                    updateSpuInfoVo.getVideo(),
                    updateSpuInfoVo.getSpecMode(), updateSpuInfoVo.getCateFid(), updateSpuInfoVo.getCateSid(),
                    updateSpuInfoVo.getCateTid(),
                    updateSpuInfoVo.getShelvesHandle(), updateSpuInfoVo.getShelvesTime(),
                    updateSpuInfoVo.getSpuValidity(), updateSpuInfoVo.getVirtualFlg(),
                    updateSpuInfoVo.getLogisticsTemplate(), updateSpuInfoVo.getUpdatedBy(),
                    updateSpuInfoVo.getUpdatedAt(), updateSpuInfoVo.getSpuErpCode(),
                    updateSpuInfoVo.getSpuId()
            });
            toMqSqls.add(SqlMapping.get("goblin_goods_sku.update_by_edit_coupon_spu"));
            updateGoodsSkuObjs.add(new Object[] {
                    updateSkuInfoVo.getSkuNo(), updateSkuInfoVo.getVirtualFlg(), updateSkuInfoVo.getLogisticsTemplate(),
                    updateSkuInfoVo.getSkuPic(), updateSkuInfoVo.getUpdatedBy(), updateSkuInfoVo.getUpdatedAt(),
                    updateSkuInfoVo.getSpuId()
            });
            toMqSqls.add(SqlMapping.get("candy_coupon.goods_spu_update"));
            LinkedList<Object[]> updateCandyCouponObjs = CollectionUtil.linkedListObjectArr();
            updateCandyCouponObjs.add(new Object[] { updateSpuInfoVo.getName(), updateSpuInfoVo.getIntro(),
                    updateSpuInfoVo.getUpdatedBy(), updateSpuInfoVo.getUpdatedAt(), spuId });

            toMqSqls.add(SqlMapping.get("goblin_goods_tag.delete"));
            LinkedList<Object[]> deleteGoodsTagObjs = CollectionUtil.linkedListObjectArr();
            toMqSqls.add(SqlMapping.get("goblin_goods_tag.insert_byreplace"));
            LinkedList<Object[]> initGoodsTagObjs = CollectionUtil.linkedListObjectArr();
            if (updateTagFlg) {
                if (!befTagIsEmptyFlg) {
                    deleteGoodsTagObjs.add(new Object[] { spuId, '0' });
                }
                if (!paramTagIsEmptyFlg) {
                    updateSpuInfoVo.getTagVoList().forEach(
                            vo -> initGoodsTagObjs.add(new Object[] { spuId, vo.getTagId(), vo.getSort(), "0" }));
                }
            }
            if (updateExtagFlg) {
                if (!befExtagIsEmptyFlg) {
                    deleteGoodsTagObjs.add(new Object[] { spuId, '1' });
                }
                if (!paramExtagIsEmptyFlg) {
                    updateSpuInfoVo.getExtagVoList().forEach(
                            vo -> initGoodsTagObjs.add(new Object[] { spuId, vo.getTagId(), vo.getSort(), "1" }));
                }
            }
            toMqSqls.add(SqlMapping.get("goblin_goods_artag.delete"));
            LinkedList<Object[]> deleteGoodsArTagObjs = CollectionUtil.linkedListObjectArr();
            toMqSqls.add(SqlMapping.get("goblin_goods_artag.insert_byreplace"));
            LinkedList<Object[]> initGoodsArTagObjs = CollectionUtil.linkedListObjectArr();
            if (updateArtagFlg) {
                if (!befArtagIsEmptyFlg) {
                    deleteGoodsArTagObjs.add(new Object[] { spuId, '1' });
                }
                if (!paramArtagIsEmptyFlg) {
                    updateSpuInfoVo.getArtagVoList().forEach(
                            vo -> initGoodsArTagObjs.add(new Object[] { spuId, vo.getTagId(), vo.getSort(), "1" }));
                }
            }

            toMqSqls.add(SqlMapping.get("goblin_goods_service_support.delete"));
            LinkedList<Object[]> deleteServiceSupportObjs = CollectionUtil.linkedListObjectArr();
            toMqSqls.add(SqlMapping.get("goblin_goods_service_support.insert_byreplace"));
            LinkedList<Object[]> initServiceSupportObjs = CollectionUtil.linkedListObjectArr();
            if (updateSsidFlg) {
                if (!befServiceSupportVoListIsEmptyFlg) {
                    deleteServiceSupportObjs.add(new Object[] { spuId });
                }
                if (!paramSsidListIsEmptyFlg) {
                    updateSpuInfoVo.getServiceSupportVoList()
                            .forEach(vo -> initServiceSupportObjs.add(new Object[] { spuId, vo.getSsid() }));
                }
            }

            toMqSqls.add(SqlMapping.get("goblin_goods_image.delete"));
            LinkedList<Object[]> deleteGoodsImageObjs = CollectionUtil.linkedListObjectArr();
            toMqSqls.add(SqlMapping.get("goblin_goods_image.insert_byreplace"));
            LinkedList<Object[]> initGoodsImageObjs = CollectionUtil.linkedListObjectArr();
            if (updateImageFlg) {
                deleteGoodsImageObjs.add(new Object[] { spuId });
                updateSpuInfoVo.getImageList()
                        .forEach(imageUrl -> initGoodsImageObjs.add(new Object[] { spuId, imageUrl }));
            }

            queueUtils.sendMsgByRedis(MQConst.GoblinQueue.SQL_GOODS.getKey(),
                    SqlMapping.gets(toMqSqls, updateGoodsInfoObjs, updateGoodsSkuObjs, updateCandyCouponObjs,
                            deleteGoodsTagObjs, initGoodsTagObjs, deleteGoodsArTagObjs, initGoodsArTagObjs,
                            deleteServiceSupportObjs, initServiceSupportObjs, deleteGoodsImageObjs,
                            initGoodsImageObjs));
            return true;
        }
        return false;
    }


    public boolean sqbGoodsEditSku(String uid, GoblinStoreMgtGoodsSqbEditSkuParam mgtGoodsEditSkuParam,
            GoblinGoodsInfoVo goodsInfoVo) {
        LocalDateTime nowTime = LocalDateTime.now();

        GoblinGoodsSkuInfoVo updateSkuInfoVo = mgtGoodsEditSkuParam.initEditGoodsSkuInfoVo();
        updateSkuInfoVo.setUpdatedBy(uid);
        updateSkuInfoVo.setUpdatedAt(nowTime);
        String skuId = updateSkuInfoVo.getSkuId();
        if (goblinMongoUtils.updateGoodsSkuInfoVo(updateSkuInfoVo)) {
            goblinRedisUtils.delGoodsSkuInfoVo(skuId);
            log.info("商品管理:SKU编辑[UID={},PARAMS={}]", uid, JsonUtils.toJson(mgtGoodsEditSkuParam));
            LinkedList<Object[]> updateGoodsSkuForMarketObjs = CollectionUtil.linkedListObjectArr();
            // 参与活动的同一商品更新处理
            List<String> marketSkuIdList = goblinRedisUtils.getSkuRe(updateSkuInfoVo.getSkuId());
            if (!CollectionUtils.isEmpty(marketSkuIdList)) {
                goblinMongoUtils.updateGoodsSkuInfoVoForMarket(marketSkuIdList, updateSkuInfoVo);
                marketSkuIdList.forEach(marketSkuId -> {
                    goblinRedisUtils.delGoodsSkuInfoVo(marketSkuId);
                    updateGoodsSkuForMarketObjs.add(new Object[] {
                            updateSkuInfoVo.getStock(), updateSkuInfoVo.getIsTrueName(), updateSkuInfoVo.getUseScope(),
                            updateSkuInfoVo.getValFace(), updateSkuInfoVo.getEffectAt(), updateSkuInfoVo.getExpireAt(),
                            updateSkuInfoVo.getUpdatedBy(), updateSkuInfoVo.getUpdatedAt(), marketSkuId
                    });
                });
            }

            BigDecimal priceGe = BigDecimal.ZERO, priceLe = BigDecimal.ZERO;
            for (String skuIdStr : goodsInfoVo.getSkuIdList()) {
                GoblinGoodsSkuInfoVo skuInfoVo = goblinRedisUtils.getGoodsSkuInfoVo(skuIdStr);
                if (null != skuInfoVo && skuInfoVo.getDelFlg().equals("0")) {
                    priceGe = priceGe.compareTo(BigDecimal.ZERO) == 0 ? skuInfoVo.getPrice()
                            : (priceGe.compareTo(skuInfoVo.getPrice()) > 0 ? skuInfoVo.getPrice() : priceGe);
                    priceLe = priceLe.compareTo(skuInfoVo.getPrice()) < 0 ? skuInfoVo.getPrice() : priceLe;
                }
            }
            boolean updateGoodsInfoVoFlg = false;
            LinkedList<Object[]> updateGoodsObjs = CollectionUtil.linkedListObjectArr();
            if (priceGe.compareTo(goodsInfoVo.getPriceGe()) != 0 || priceLe.compareTo(goodsInfoVo.getPriceLe()) != 0) {
                updateGoodsInfoVoFlg = true;
                goodsInfoVo.setPriceGe(priceGe);
                goodsInfoVo.setPriceLe(priceLe);
                goodsInfoVo.setUpdatedBy(uid);
                goodsInfoVo.setUpdatedAt(nowTime);

                updateGoodsObjs.add(new Object[] { priceGe, priceLe, uid, nowTime, goodsInfoVo.getSpuId() });
            }
            if (updateGoodsInfoVoFlg) {
                goblinMongoUtils.updateGoodsInfoVo(goodsInfoVo);
                goblinRedisUtils.delGoodsInfoVo(goodsInfoVo.getSpuId());
            }

            LinkedList<String> toMqSqls = CollectionUtil.linkedListString();
            toMqSqls.add(SqlMapping.get("goblin_goods_sku.update_by_edit_for_market2"));
            // LinkedList<Object[]> updateGoodsSkuForMarketObjs =
            // CollectionUtil.linkedListObjectArr();
            toMqSqls.add(SqlMapping.get("goblin_goods_sku.update_by_edit_for_coupon"));
            LinkedList<Object[]> updateGoodsSkuObjs = CollectionUtil.linkedListObjectArr();
            updateGoodsSkuObjs.add(new Object[] {
                    updateSkuInfoVo.getPrice(), updateSkuInfoVo.getPriceMember(), updateSkuInfoVo.getStock(),
                    updateSkuInfoVo.getSkuStock(),
                    updateSkuInfoVo.getBuyLimit(), updateSkuInfoVo.getUpdatedBy(), updateSkuInfoVo.getUpdatedAt(), skuId
            });
            toMqSqls.add(SqlMapping.get("goblin_goods_sku_coupon.update"));
            LinkedList<Object[]> updateGoodsSkuCouponObjs = CollectionUtil.linkedListObjectArr();
            updateGoodsSkuCouponObjs.add(new Object[] {
                    updateSkuInfoVo.getUseScope(), updateSkuInfoVo.getValFace(), updateSkuInfoVo.getIsTrueName(),
                    updateSkuInfoVo.getEffectAt(), updateSkuInfoVo.getExpireAt(), skuId });
            toMqSqls.add(SqlMapping.get("candy_coupon.goods_sku_update"));
            LinkedList<Object[]> updateCandyCouponObjs = CollectionUtil.linkedListObjectArr();
            updateCandyCouponObjs.add(new Object[] {
                    updateSkuInfoVo.getValFace(), updateSkuInfoVo.getIsTrueName(), updateSkuInfoVo.getEffectAt(),
                    updateSkuInfoVo.getExpireAt(), updateSkuInfoVo.getUpdatedBy(), updateSkuInfoVo.getUpdatedAt(), skuId
            });
            toMqSqls.add(SqlMapping.get("candy_coupon_rule.goods_sku_update"));
            LinkedList<Object[]> updateCandyCouponRuleObjs = CollectionUtil.linkedListObjectArr();
            String busiName = "%s";
            switch (updateSkuInfoVo.getUseScope()) {// 适用范围[101-音乐节｜102-小型演出（livehouse演出）｜103-巡演]
                case 101:// 音乐节
                    busiName = String.format(busiName, "草莓、M_DSK、五百里音乐节");// 按杨要求文案'音乐节'改'草莓、M_DSK、五百里音乐节'
                    break;
                case 102:// 小型演出（LiveHouse演出）
                    busiName = String.format(busiName, "小型演出（LiveHouse演出）系列");
                    break;
                case 103:// 巡演
                    busiName = String.format(busiName, "巡演 系列");
                    break;
            }
            updateCandyCouponRuleObjs.add(new Object[] { updateSkuInfoVo.getUseScope(), busiName, skuId });
            toMqSqls.add(SqlMapping.get("goblin_goods.update_by_edit_sku"));
            // LinkedList<Object[]> updateGoodsObjs = CollectionUtil.linkedListObjectArr();

            queueUtils.sendMsgByRedis(MQConst.GoblinQueue.SQL_GOODS.getKey(),
                    SqlMapping.gets(toMqSqls, updateGoodsSkuForMarketObjs, updateGoodsSkuObjs, updateGoodsSkuCouponObjs,
                            updateCandyCouponObjs, updateCandyCouponRuleObjs, updateGoodsObjs));
            return true;
        }
        return false;
    }

}
