package com.slodon.b2b2c.controller.goods.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slodon.b2b2c.core.constant.*;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.i18n.Language;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.enums.SeoTypeConstant;
import com.slodon.b2b2c.enums.WebSiteConstant;
import com.slodon.b2b2c.goods.dto.CalculateExpressDTO;
import com.slodon.b2b2c.goods.dto.GoodsAddDTO;
import com.slodon.b2b2c.goods.dto.GoodsRankListDTO;
import com.slodon.b2b2c.goods.example.*;
import com.slodon.b2b2c.goods.pojo.*;
import com.slodon.b2b2c.member.example.GoodsAnnexExample;
import com.slodon.b2b2c.member.example.MemberAddressExample;
import com.slodon.b2b2c.member.example.MemberFollowProductExample;
import com.slodon.b2b2c.member.example.MemberFollowStoreExample;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.member.pojo.MemberAddress;
import com.slodon.b2b2c.member.pojo.MemberFollowProduct;
import com.slodon.b2b2c.member.pojo.MemberFollowStore;
import com.slodon.b2b2c.model.goods.*;
import com.slodon.b2b2c.model.member.MemberAddressModel;
import com.slodon.b2b2c.model.member.MemberFollowProductModel;
import com.slodon.b2b2c.model.member.MemberFollowStoreModel;
import com.slodon.b2b2c.model.promotion.PromotionCommonModel;
import com.slodon.b2b2c.model.seller.StoreModel;
import com.slodon.b2b2c.model.seller.StoreNameModel;
import com.slodon.b2b2c.model.seller.StoreSiteInfoModel;
import com.slodon.b2b2c.model.system.RegionCityModel;
import com.slodon.b2b2c.seller.example.StoreNameExample;
import com.slodon.b2b2c.seller.pojo.Store;
import com.slodon.b2b2c.seller.pojo.StoreName;
import com.slodon.b2b2c.system.example.RegionCityExample;
import com.slodon.b2b2c.system.pojo.RegionCity;
import com.slodon.b2b2c.system.pojo.SysSeo;
import com.slodon.b2b2c.vo.goods.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "front-商品详情")
@RestController
@RequestMapping("v3/goods/front/goods")
public class GoodsDetailsController extends BaseController {

    @Resource
    private GoodsModel goodsModel;
    @Resource
    private GoodsExtendModel goodsExtendModel;
    @Resource
    private ProductModel productModel;
    @Resource
    private GoodsPictureModel goodsPictureModel;
    @Resource
    private GoodsLabelModel goodsLabelModel;
    @Resource
    private GoodsCommentModel goodsCommentModel;
    @Resource
    private GoodsSpecValueModel goodsSpecValueModel;
    @Resource
    private GoodsFreightTemplateModel goodsFreightTemplateModel;
    @Resource
    private GoodsRelatedTemplateModel goodsRelatedTemplateModel;
    @Resource
    private GoodsPromotionModel goodsPromotionModel;
    @Resource
    private StoreModel storeModel;
    @Resource
    private MemberFollowProductModel memberFollowProductModel;
    @Resource
    private MemberFollowStoreModel memberFollowStoreModel;
    @Resource
    private RegionCityModel regionCityModel;
    @Resource
    private MemberAddressModel memberAddressModel;
    @Resource
    private PromotionCommonModel promotionCommonModel;
    @Resource
    private GoodsBindRankModel goodsBindRankModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate<String, Object> objectRedisTemplate;

    @Resource
    private GoodsAnnexModel goodsAnnexModel;
    @Resource
    private StoreSiteInfoModel storeSiteInfoModel;
    @Resource
    private StoreNameModel  storeNameModel;

    @ApiOperation("获取商品详情---静态数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "货品Id", required = true, paramType = "query"),
    })
    @GetMapping("details")
    public JsonResult<GoodsFrontDetailVO> getGoodsDetails1(HttpServletRequest request, Long productId) {
        Product product = productModel.getProductByProductId(productId);
        AssertUtil.notNull(product, "货品不存在，请重试！");
        Long goodsId = product.getGoodsId();
        //从redis中查询缓存数据
        Object goodsDetail = objectRedisTemplate.opsForHash().get(RedisConst.GOODS_DETAIL, String.valueOf(goodsId));
        if (goodsDetail != null) {
            //缓存数据不为空，直接返回缓存
            String goodsDetailStr = JSON.toJSONString(goodsDetail);
            GoodsFrontDetailVO vo = JSON.parseObject(goodsDetailStr, GoodsFrontDetailVO.class);
            return SldResponse.success(vo);
        }

        //缓存不存在，查库并缓存
        Goods goods = goodsModel.getGoodsByGoodsId(goodsId);
//        AssertUtil.notNull(goods, "商品不存在，请重试！");
//        if (!goods.getState().equals(GoodsConst.GOODS_STATE_UPPER)) {
//            return SldResponse.fail("商品已下架，请重试！");
//        }
        GoodsExtend goodsExtend = goodsExtendModel.getGoodsExtendByGoodsId(goodsId);
//        AssertUtil.notNull(goodsExtend, "商品不存在，请重试！");

        SysSeoVO sysSeoVO = storeModel.getRecordByTargetIdAndTypeForGoodsDetail(goodsId, SeoTypeConstant.SEO_TYPE_GOODS);


        //查询货品信息，如果没有传货品ID则获取默认货品
        ProductExample productExample = new ProductExample();
        productExample.setGoodsId(goodsId);
        productExample.setStateIn(GoodsConst.PRODUCT_STATE_1 + "," + GoodsConst.PRODUCT_STATE_3);
        productExample.setOrderBy("is_default desc");
        List<Product> productList = productModel.getProductList(productExample, null);
        if (product.getState().equals(GoodsConst.PRODUCT_STATE_2) || product.getState().equals(GoodsConst.PRODUCT_STATE_0)) {
            //查询的货品失效，默认取第一个有效的货品
            product = productList.get(0);
        }

        //获取登录用户信息
        Member member = UserUtil.getUser(request, Member.class);
        Boolean isFavorite = checkFavorite(member.getMemberId(), goodsId, product.getProductId());

        //是否关注店铺
        Boolean isFollowStore = checkIsFollowStore(member.getMemberId(), product.getStoreId());

        List<GoodsPicture> goodsPictureList = new ArrayList<>();
        if (goodsExtend != null) {
            //查询规格图片
            GoodsPictureExample goodsPictureExample = new GoodsPictureExample();
            goodsPictureExample.setOrderBy("is_main asc,picture_id asc");
            goodsPictureExample.setGoodsId(goodsId);
            //有主规格
            if (goodsExtend.getMainSpecId() != 0) {
                goodsPictureExample.setSpecValueIdIn(product.getSpecValueIds());
            }
            goodsPictureList = goodsPictureModel.getGoodsPictureList(goodsPictureExample, null);
        }

        String webSite = WebUtil.getWebSite(request);
        Store store = storeModel.getStoreByStoreId(goods.getStoreId());
        Integer isOwnStore = store.getIsOwnStore();
        String deliverScore = store.getDeliverScore();
        Integer followNumber=store.getFollowNumber();
        String descriptionScore = store.getDescriptionScore();
        String serviceScore=store.getServiceScore();
        if (WebSiteConstant.MEMBER_DISTRIBUTOR.equals(webSite)) {
            store = storeSiteInfoModel.getStoreByStoreIdAndWebSite(goods.getStoreId(), webSite);
            StoreNameExample storeNameExample = new StoreNameExample();
            storeNameExample.setStoreId(goods.getStoreId());
            storeNameExample.setWebSite(webSite);
            StoreName storeName = storeNameModel.getStoreNameByExample(storeNameExample);
            store.setStoreName(storeName != null ? storeName.getStoreName() : "未知");
            store.setIsOwnStore(isOwnStore);
            store.setDeliverScore(deliverScore);
            store.setFollowNumber(followNumber);
            store.setDescriptionScore(descriptionScore);
            store.setServiceScore(serviceScore);
        }
        //默认店铺logo
        if (StringUtils.isEmpty(store.getStoreLogo())) {
            store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
        }
        GoodsPromotion goodsPromotion = goodsPromotionModel.getGoodsPromotionByProductId(product.getProductId());
        if (goodsPromotion != null && goodsPromotion.getPromotionType() == PromotionConst.PROMOTION_TYPE_104) {
            //秒杀库存
            String seckillStock = stringRedisTemplate.opsForValue().get(RedisConst.REDIS_SECKILL_PRODUCT_STOCK_PREFIX + product.getProductId());
            Integer stock = seckillStock == null ? 0 : Integer.parseInt(seckillStock);
            //秒杀活动未开始（商品库存减去秒杀库存），已开始取redis库存
            if (promotionCommonModel.isPromotionAvailable(PromotionConst.PROMOTION_TYPE_104, product.getProductId().toString())) {
                //活动正在进行
                product.setProductStock(stock);
            } else {
                //活动未开始
                product.setProductStock(product.getProductStock() - stock);
            }
        }
        List<GoodsFrontDetailVO.ServiceLabelVO> LabelVOList = getLabelNames(goods.getServiceLabelIds(), goods);

        //物流发货信息
        GoodsFrontDetailVO.DeliverInfo deliverInfo = null;
        if (goods.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_1) {
            if (goodsExtend != null) {
                deliverInfo = getDeliverInfo(goodsExtend, product, member);
            }
        }
        //商品说明书
        GoodsAnnexExample goodsAnnexExample = new GoodsAnnexExample();
        goodsAnnexExample.createCriteria().andGoodsIdEqualTo(goodsId).andStatusEqualTo(GoodsAnnexStatusConst.IS_STATUS);
        //只查状态为1的数据
//        goodsAnnexExample.createCriteria().andStatusEqualTo(GoodsAnnexStatusConst.IS_STATUS);
        List<GoodsAnnex> goodsAnnexList = null;
        if(goods.getState() == 20 || goods.getState() == 6){
        }else{
            goodsAnnexList = goodsAnnexModel.goodsAnnexList(goodsAnnexExample);
        }

        GoodsFrontDetailVO detailVO = new GoodsFrontDetailVO(goods, isFavorite, LabelVOList, goodsExtend, product,
                productList, goodsPictureList, store, isFollowStore, deliverInfo, goodsPromotion,sysSeoVO,goodsAnnexList);

        //顶部模版内容
        detailVO.setTopTemplateContent(getRelatedTemplateContent(goodsExtend.getRelatedTemplateIdTop()));
        //底部模版内容
        detailVO.setBottomTemplateContent(getRelatedTemplateContent(goodsExtend.getRelatedTemplateIdBottom()));

        //从redis查询购买商品赠送多少积分
        String goodsBuy = stringRedisTemplate.opsForValue().get("integral_present_goods_buy");
        int buyGoodsIntegral = StringUtil.isEmpty(goodsBuy) ? 0 : Integer.parseInt(goodsBuy);
        //订单最多赠送积分
        String orderMax = stringRedisTemplate.opsForValue().get("integral_present_order_max");
        int orderMaxIntegral = StringUtil.isEmpty(orderMax) ? 0 : Integer.parseInt(orderMax);
        int sendIntegral = 0;
        if (buyGoodsIntegral > 0) {
            if (orderMaxIntegral > 0 && buyGoodsIntegral > orderMaxIntegral) {
                sendIntegral = orderMaxIntegral;
            } else {
                sendIntegral = buyGoodsIntegral;
            }
        }
        detailVO.setIntegral(sendIntegral);
        //缓存
        objectRedisTemplate.opsForHash().put(RedisConst.GOODS_DETAIL, String.valueOf(goodsId), detailVO);
        return SldResponse.success(detailVO);
    }

    @ApiOperation("获取商品详情---动态数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "货品Id", required = true, paramType = "query"),
    })
    @GetMapping("details2")
    public JsonResult<GoodsFrontDetailVO> getGoodsDetails2(HttpServletRequest request, Long productId) {
        Product product = productModel.getProductByProductId(productId);
        AssertUtil.notNull(product, "货品不存在，请重试！");
        Long goodsId = product.getGoodsId();

        Goods goods = goodsModel.getGoodsByGoodsId(goodsId);
//        AssertUtil.notNull(goods, "商品不存在，请重试！");
//        if (!goods.getState().equals(GoodsConst.GOODS_STATE_UPPER)) {
//            return SldResponse.fail("商品已下架，请重试！");
//        }
        GoodsExtend goodsExtend = goodsExtendModel.getGoodsExtendByGoodsId(goodsId);
//        AssertUtil.notNull(goodsExtend,  "商品不存在，请重试！");

        //seo
        SysSeoVO sysSeoVO = storeModel.getRecordByTargetIdAndTypeForGoodsDetail(goodsId, SeoTypeConstant.SEO_TYPE_GOODS);

        //查询商品说明(上传的文件)
        GoodsAnnexExample goodsAnnexExample = new GoodsAnnexExample();
        goodsAnnexExample.createCriteria().andGoodsIdEqualTo(goodsId).andStatusEqualTo(GoodsAnnexStatusConst.IS_STATUS);
        //只查状态为1的数据
//        goodsAnnexExample.createCriteria().andStatusEqualTo(GoodsAnnexStatusConst.IS_STATUS);
        List<GoodsAnnex> goodsAnnexList = null;
        if(goods.getState() == 20 || goods.getState() == 6){
        }else{
            goodsAnnexList = goodsAnnexModel.goodsAnnexList(goodsAnnexExample);
        }


        //查询货品信息，如果没有传货品ID则获取默认货品
        ProductExample productExample = new ProductExample();
        productExample.setGoodsId(goodsId);
        productExample.setStateIn(GoodsConst.PRODUCT_STATE_1 + "," + GoodsConst.PRODUCT_STATE_3);
        productExample.setOrderBy("is_default desc");
        List<Product> productList = productModel.getProductList(productExample, null);
        if (product.getState().equals(GoodsConst.PRODUCT_STATE_2) || product.getState().equals(GoodsConst.PRODUCT_STATE_0)) {
            //查询的货品失效，默认取第一个有效的货品
            product = productList.get(0);
        }

        //获取登录用户信息
        Member member = UserUtil.getUser(request, Member.class);
        Boolean isFavorite = checkFavorite(member.getMemberId(), goodsId, product.getProductId());

        //是否关注店铺
        Boolean isFollowStore = checkIsFollowStore(member.getMemberId(), product.getStoreId());

        List<GoodsPicture> goodsPictureList = new ArrayList<>();
        if (goodsExtend != null) {
            //查询规格图片
            GoodsPictureExample goodsPictureExample = new GoodsPictureExample();
            goodsPictureExample.setOrderBy("is_main asc,picture_id asc");
            goodsPictureExample.setGoodsId(goodsId);
            //有主规格
            if (goodsExtend.getMainSpecId() != 0) {
                goodsPictureExample.setSpecValueIdIn(product.getSpecValueIds());
            }
            goodsPictureList = goodsPictureModel.getGoodsPictureList(goodsPictureExample, null);
        }


        Store store = storeModel.getStoreByStoreId(goods.getStoreId());
        String webSite = WebUtil.getWebSite(request);
        Integer isOwnStore = store.getIsOwnStore();
        String deliverScore = store.getDeliverScore();
        Integer followNumber=store.getFollowNumber();
        String descriptionScore = store.getDescriptionScore();
        String serviceScore=store.getServiceScore();
        if (WebSiteConstant.MEMBER_DISTRIBUTOR.equals(webSite)) {
            store = storeSiteInfoModel.getStoreByStoreIdAndWebSite(goods.getStoreId(), webSite);
            StoreNameExample storeNameExample = new StoreNameExample();
            storeNameExample.setStoreId(goods.getStoreId());
            storeNameExample.setWebSite(webSite);
            StoreName storeName = storeNameModel.getStoreNameByExample(storeNameExample);
            store.setStoreName(storeName != null ? storeName.getStoreName() : "未知");
            store.setIsOwnStore(isOwnStore);
            store.setDeliverScore(deliverScore);
            store.setFollowNumber(followNumber);
            store.setDescriptionScore(descriptionScore);
            store.setServiceScore(serviceScore);
        }
        //默认店铺logo
        if (StringUtils.isEmpty(store.getStoreLogo())) {
            store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
        }
        GoodsPromotion goodsPromotion = goodsPromotionModel.getGoodsPromotionByProductId(product.getProductId());
        if (goodsPromotion != null && goodsPromotion.getPromotionType() == PromotionConst.PROMOTION_TYPE_104) {
            //秒杀库存
            String seckillStock = stringRedisTemplate.opsForValue().get(RedisConst.REDIS_SECKILL_PRODUCT_STOCK_PREFIX + product.getProductId());
            Integer stock = seckillStock == null ? 0 : Integer.parseInt(seckillStock);
            //秒杀活动未开始（商品库存减去秒杀库存），已开始取redis库存
            if (promotionCommonModel.isPromotionAvailable(PromotionConst.PROMOTION_TYPE_104, product.getProductId().toString())) {
                //活动正在进行
                product.setProductStock(stock);
            } else {
                //活动未开始
                product.setProductStock(product.getProductStock() - stock);
            }
        }
//        List<GoodsFrontDetailVO.ServiceLabelVO> LabelVOList = getLabelNames(goods.getServiceLabelIds());

        //物流发货信息
        GoodsFrontDetailVO.DeliverInfo deliverInfo = null;
        if (goods.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_1) {
            if (goodsExtend != null) {
                deliverInfo = getDeliverInfo(goodsExtend, product, member);
            }
        }

        GoodsFrontDetailVO goodsFrontDetailVO = new GoodsFrontDetailVO(goods, isFavorite, null, goodsExtend,
                product, productList, goodsPictureList, store, isFollowStore, deliverInfo, goodsPromotion,sysSeoVO,goodsAnnexList);

//        goodsFrontDetailVO.setGoodsDetails(getDetailsRelatedTemplate(goodsExtend));

        //从redis查询购买商品赠送多少积分
        String goodsBuy = stringRedisTemplate.opsForValue().get("integral_present_goods_buy");
        int buyGoodsIntegral = StringUtil.isEmpty(goodsBuy) ? 0 : Integer.parseInt(goodsBuy);
        //订单最多赠送积分
        String orderMax = stringRedisTemplate.opsForValue().get("integral_present_order_max");
        int orderMaxIntegral = StringUtil.isEmpty(orderMax) ? 0 : Integer.parseInt(orderMax);
        int sendIntegral = 0;
        if (buyGoodsIntegral > 0) {
            if (orderMaxIntegral > 0 && buyGoodsIntegral > orderMaxIntegral) {
                sendIntegral = orderMaxIntegral;
            } else {
                sendIntegral = buyGoodsIntegral;
            }
        }
        goodsFrontDetailVO.setIntegral(sendIntegral);

        return SldResponse.success(goodsFrontDetailVO);
    }

    @ApiOperation("获取货品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品Id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "specValueIds", value = "规格属性值Id", paramType = "query"),
    })
    @GetMapping("productInfo")
    public JsonResult<ProductInfoVO> getProductInfo(HttpServletRequest request, Long goodsId, String specValueIds) {
        //获得货品信息 包含某规格商品的库存及价格信息
        Goods goods = goodsModel.getGoodsByGoodsId(goodsId);
        AssertUtil.notNull(goods, "商品不存在，请重试！");
        GoodsExtend goodsExtend = goodsExtendModel.getGoodsExtendByGoodsId(goodsId);
        AssertUtil.notNull(goodsExtend, "未查询到货品，请重试！");
        //查询所有有效规格
        ProductExample productExample = new ProductExample();
        productExample.setGoodsId(goodsId);
        productExample.setStateIn(GoodsConst.PRODUCT_STATE_1 + "," + GoodsConst.PRODUCT_STATE_3);
        List<Product> productList = productModel.getProductList(productExample, null);
        List<String> effectSpecValueIds = new ArrayList<>();
        productList.forEach(product1 -> effectSpecValueIds.add(product1.getSpecValueIds()));

        Product product = null;//要选中的货品信息

        //查询要选中的规格id组合
        String[] specAttrIdArr = specValueIds.split(",");
        Arrays.sort(specAttrIdArr);
        for (Product product1 : productList) {
            String[] effArr = product1.getSpecValueIds().split(",");
            Arrays.sort(effArr);
            if (Arrays.equals(effArr, specAttrIdArr)) {
                product = product1;
                break;
            }
        }
        if (product == null) {
            //未找到货品，默认第一个
            product = productList.get(0);
        }

        //无规格时specValueId取0
        Integer specValueId = 0;
        //查询主规格值列表
        //有主规格
        if (goodsExtend.getMainSpecId() != 0) {
            GoodsSpecValueExample goodsSpecValueExample = new GoodsSpecValueExample();
            goodsSpecValueExample.setSpecId(goodsExtend.getMainSpecId());
            goodsSpecValueExample.setSpecValueIdIn(product.getSpecValueIds());
            List<GoodsSpecValue> goodsSpecValueList = goodsSpecValueModel.getGoodsSpecValueList(goodsSpecValueExample, null);
            specValueId = goodsSpecValueList.get(0).getSpecValueId();
        }

        //查询对应主规格下的图片
        GoodsPictureExample goodsPictureExample = new GoodsPictureExample();
        goodsPictureExample.setGoodsId(goodsId);
        goodsPictureExample.setSpecValueId(specValueId);
        goodsPictureExample.setOrderBy("is_main asc,picture_id asc");
        List<GoodsPicture> goodsPictureList = goodsPictureModel.getGoodsPictureList(goodsPictureExample, null);

        List<GoodsFrontDetailVO.SpecVO> specs = new ArrayList<>();//规格列表
        //构造规格列表
        for (GoodsAddDTO.SpecInfo specInfo : JSONObject.parseArray(goodsExtend.getSpecJson(), GoodsAddDTO.SpecInfo.class)) {
            if (specInfo.getIsMainSpec() == GoodsConst.IS_MAIN_SPEC_YES) {
                //主规格，放在规格列表的第一位
                specs.add(0, new GoodsFrontDetailVO.SpecVO(specInfo, product.getSpecValueIds(), effectSpecValueIds));
            } else {
                specs.add(new GoodsFrontDetailVO.SpecVO(specInfo, product.getSpecValueIds(), effectSpecValueIds));
            }
        }
        Member member = UserUtil.getUser(request, Member.class);
        //物流发货信息
        GoodsFrontDetailVO.DeliverInfo deliverInfo = null;
        if (goods.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_1) {
            deliverInfo = getDeliverInfo(goodsExtend, product, member);
        }
        GoodsPromotion goodsPromotion = goodsPromotionModel.getGoodsPromotionByProductId(product.getProductId());
        if (goodsPromotion != null && goodsPromotion.getPromotionType() == PromotionConst.PROMOTION_TYPE_104) {
            //秒杀库存
            String seckillStock = stringRedisTemplate.opsForValue().get(RedisConst.REDIS_SECKILL_PRODUCT_STOCK_PREFIX + product.getProductId());
            Integer stock = seckillStock == null ? 0 : Integer.parseInt(seckillStock);
            //秒杀活动未开始（商品库存减去秒杀库存），已开始取redis库存
            if (promotionCommonModel.isPromotionAvailable(PromotionConst.PROMOTION_TYPE_104, product.getProductId().toString())) {
                //活动正在进行
                product.setProductStock(stock);
            } else {
                //活动未开始
                product.setProductStock(product.getProductStock() - stock);
            }
        }
        ProductInfoVO productInfoVO = new ProductInfoVO(goodsExtend, product, productList, goodsPictureList, deliverInfo, goodsPromotion);
        return SldResponse.success(productInfoVO);
    }

    @ApiOperation("商品评价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "货品Id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "评价等级[(好评)high,(有图)hasPic,(中评)middle,(差评)low]", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("comment")
    public JsonResult<GoodsCommentsInfoVO> commentsList(HttpServletRequest request, Long productId, String type) {
        //分页设置
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        //根据productId查询商品
        Product product = productModel.getProductByProductId(productId);
        GoodsCommentsInfoVO vo = goodsCommentModel.getGoodsCommentList(product.getGoodsId(), type, pager);

        return SldResponse.success(vo);
    }

    @ApiOperation("活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "货品Id", required = true, paramType = "query")
    })
    @GetMapping("activityList")
    public JsonResult<List<GoodsPromotionVO>> activityList(HttpServletRequest request, Long productId) {
        Product product = productModel.getProductByProductId(productId);
        if (product == null) {
            return SldResponse.success(new ArrayList<>());
        }
        //商品参与的活动
        GoodsPromotionExample promotionExample = new GoodsPromotionExample();
        promotionExample.setIsEffective(PromotionConst.IS_EFFECTIVE_YES);
        promotionExample.setStartTimeBefore(new Date());
        promotionExample.setEndTimeAfter(new Date());
        //一级活动，绑定商品
//        promotionExample.setBindType(PromotionConst.BIND_TYPE_1);
//        promotionExample.setGoodsId(product.getGoodsId());
//        List<GoodsPromotion> list = goodsPromotionModel.getGoodsPromotionList(promotionExample, null);
        //二级活动，绑定店铺
        promotionExample.setGoodsId(null);
        promotionExample.setStoreId(product.getStoreId());
        promotionExample.setBindType(PromotionConst.BIND_TYPE_2);
//        list.addAll(goodsPromotionModel.getGoodsPromotionList(promotionExample, null));
        List<GoodsPromotion> list = goodsPromotionModel.getGoodsPromotionList(promotionExample, null);
        //三级活动，绑定三级分类
        promotionExample.setStoreId(null);
        promotionExample.setGoodsCategoryId3(product.getCategoryId3());
        promotionExample.setBindType(PromotionConst.BIND_TYPE_3);
        list.addAll(goodsPromotionModel.getGoodsPromotionList(promotionExample, null));
        List<GoodsPromotionVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(goodsPromotion -> {
                GoodsPromotionVO promotionVO = new GoodsPromotionVO();
                promotionVO.setGoodsPromotionId(goodsPromotion.getGoodsPromotionId());
                promotionVO.setPromotionId(goodsPromotion.getPromotionId());
                promotionVO.setPromotionName(promotionCommonModel.getPromotionName(goodsPromotion.getPromotionType()));
                promotionVO.setDescriptionList(promotionCommonModel.getPromotionDescription(goodsPromotion.getPromotionType(), goodsPromotion.getPromotionId()));
                promotionVO.setPromotionType(goodsPromotion.getPromotionType());
                promotionVO.setPromotionGrade(goodsPromotion.getPromotionGrade());
                vos.add(promotionVO);
            });
        }
        return SldResponse.success(vos);
    }

    @ApiOperation("计算运费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "货品Id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "cityCode", value = "城市编码", required = true, paramType = "query")
    })
    @GetMapping("calculateExpress")
    public JsonResult<BigDecimal> calculateExpressFee(HttpServletRequest request, Long productId, String cityCode) {
        Member member = UserUtil.getUser(request, Member.class);
        AssertUtil.notNullOrZero(productId, "货品id不能为空");
        AssertUtil.notEmpty(cityCode, "城市编码不能为空");

        CalculateExpressDTO calculateExpressDTO = new CalculateExpressDTO();
        Product product = productModel.getProductByProductId(productId);
        AssertUtil.notNull(product, "货品信息为空");
        CalculateExpressDTO.ProductInfo productInfo = new CalculateExpressDTO.ProductInfo();
        try {
            PropertyUtils.copyProperties(productInfo, product);
        } catch (Exception e) {
            e.printStackTrace();
        }
        productInfo.setNumber(1);
        calculateExpressDTO.setStoreId(product.getStoreId());
        calculateExpressDTO.setCityCode(cityCode);
        calculateExpressDTO.getProductList().add(productInfo);
        BigDecimal productFee = goodsFreightTemplateModel.calculateExpressFee(calculateExpressDTO);
        return SldResponse.success(productFee);
    }

    @ApiOperation("排行榜列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品Id", required = true, paramType = "query")
    })
    @GetMapping("rankList")
    public JsonResult<List<GoodsRankListVO>> rankList(HttpServletRequest request, Long goodsId) {
        List<GoodsRankListVO> vos = new ArrayList<>();
        //榜单绑定商品查询
        GoodsBindRankExample example = new GoodsBindRankExample();
        example.setGoodsId(goodsId);
        List<GoodsRankListDTO> list = goodsBindRankModel.getGoodsRankList(example);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(goodsRank -> {
                GoodsRankListVO vo = new GoodsRankListVO();
                vo.setRankId(goodsRank.getRankId());
                vo.setRankName(goodsRank.getRankName());
                vo.setGoodsRank(goodsRank.getGoodsRank());
                vo.setRankType(goodsRank.getRankType());
                vo.setTypeName(getRankType(goodsRank.getRankType()));
                vos.add(vo);
            });
        }
        return SldResponse.success(vos);
    }

    /**
     * 根据类型获取对应名称
     *
     * @param rankType
     * @return
     */
    private String getRankType(Integer rankType) {
        String value = "好物榜";
        if (StringUtils.isEmpty(rankType)) {
            return value;
        }
        switch (rankType) {
            case GoodsRankConst.RANK_TYPE_1:
                value = "畅销榜";
                break;
            case GoodsRankConst.RANK_TYPE_2:
                value = "好评榜";
                break;
            case GoodsRankConst.RANK_TYPE_3:
                value = "新品榜";
                break;
            case GoodsRankConst.RANK_TYPE_4:
                value = "好物榜";
                break;
            default:
                break;
        }
        return value;
    }

    /**
     * 构造服务标签
     *
     * @param LabelIds
     * @return
     */
    private List<GoodsFrontDetailVO.ServiceLabelVO> getLabelNames(String LabelIds, Goods goods) {
        List<GoodsFrontDetailVO.ServiceLabelVO> list = new ArrayList<>();
        if (!StringUtils.isEmpty(LabelIds)) {
            GoodsLabelExample example = new GoodsLabelExample();
            example.setLabelIdIn(LabelIds);
            example.setOrderBy("sort asc, create_time desc");
            List<GoodsLabel> labels = goodsLabelModel.getGoodsLabelList(example, null);
            if (!CollectionUtils.isEmpty(labels)) {
                labels.forEach(goodsLabel -> list.add(new GoodsFrontDetailVO.ServiceLabelVO(goodsLabel)));
            }
        }
        if (goods.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_1) {
            return list;
        }
        //虚拟商品构造信息
        if (goods.getAfterSaleService() == GoodsConst.AFTER_SALE_SERVICE_NO) {
            GoodsLabel goodsLabel = new GoodsLabel();
            goodsLabel.setLabelName("不支持退货");
            goodsLabel.setDescription("不支持退货");
            list.add(new GoodsFrontDetailVO.ServiceLabelVO(goodsLabel));
        } else {
            GoodsLabel goodsLabel = new GoodsLabel();
            goodsLabel.setLabelName("支持退货");
            goodsLabel.setDescription("支持退货");
            list.add(new GoodsFrontDetailVO.ServiceLabelVO(goodsLabel));
        }
        return list;
    }

    /**
     * 获取物流信息
     *
     * @param goodsExtend
     * @return DeliverInfo
     */
    private GoodsFrontDetailVO.DeliverInfo getDeliverInfo(GoodsExtend goodsExtend, Product product, Member member) {
        String cityCode = goodsExtend.getCityCode();
        GoodsFrontDetailVO.DeliverInfo deliverInfo = new GoodsFrontDetailVO.DeliverInfo();

        //查发货地
        if (!StringUtils.isEmpty(cityCode)) {
            RegionCityExample regionCityExample = new RegionCityExample();
            regionCityExample.setRemarkCode(cityCode);
            List<RegionCity> regionCityList = regionCityModel.getRegionCityList(regionCityExample, null);
            if (!CollectionUtils.isEmpty(regionCityList)) {
                deliverInfo.setCityName(regionCityList.get(0).getCityName());
            }
        }

        Integer freightId = goodsExtend.getFreightId();
        //查运费
        CalculateExpressDTO calculateExpressDTO = new CalculateExpressDTO();
        calculateExpressDTO.setStoreId(product.getStoreId());
        calculateExpressDTO.getProductList()
                .add(new CalculateExpressDTO.ProductInfo(
                        goodsExtend.getGoodsId(),
                        product.getProductId(),
                        product.getProductPrice(),
                        1));
        //有运费模版根据运费模版计算运费
        if (freightId != null && freightId != 0) {
            //获取会员地址
            if (member.getMemberId() != null) {
                MemberAddressExample memberAddressExample = new MemberAddressExample();
                memberAddressExample.setMemberId(member.getMemberId());
                List<MemberAddress> memberAddressList = memberAddressModel.getMemberAddressList(memberAddressExample, null);
                if (!CollectionUtils.isEmpty(memberAddressList)) {
                    MemberAddress defaultMemberAddress = null;
                    for (MemberAddress memberAddress : memberAddressList) {
                        if (MemberConst.IS_DEFAULT_1 == (memberAddress.getIsDefault())) {
                            defaultMemberAddress = memberAddress;
                        }
                    }
                    if (defaultMemberAddress == null) {
                        defaultMemberAddress = memberAddressList.get(0);
                    }

                    //设置收货地城市编码
                    calculateExpressDTO.setCityCode(defaultMemberAddress.getCityCode());
                }
            }
            //根据运费模版获取发货时间
            GoodsFreightTemplate goodsFreightTemplate = goodsFreightTemplateModel.getGoodsFreightTemplateByFreightTemplateId(freightId);
            AssertUtil.notNull(goodsFreightTemplate, "商品运费模板有误");
            deliverInfo.setTransTime(goodsFreightTemplate.getDeliverTime());
        }
        BigDecimal expressFee = goodsFreightTemplateModel.calculateExpressFee(calculateExpressDTO);
        deliverInfo.setExpressFee(expressFee);

        return deliverInfo;
    }

    //判断用户是否收藏商品
    private Boolean checkFavorite(Integer memberId, long goodsId, long productId) {
        boolean isFavorite = false;
        if (!StringUtil.isNullOrZero(memberId)) {
            MemberFollowProductExample memberFollowProductExample = new MemberFollowProductExample();
            memberFollowProductExample.setGoodsId(goodsId);
            memberFollowProductExample.setMemberId(memberId);
            memberFollowProductExample.setProductId(productId);
            List<MemberFollowProduct> list = memberFollowProductModel.getMemberFollowProductList(memberFollowProductExample, null);
            if (!CollectionUtils.isEmpty(list)) {
                isFavorite = true;
            }
        }
        return isFavorite;
    }

    //判断用户是否关注店铺
    private Boolean checkIsFollowStore(Integer memberId, Long storeId) {
        boolean isFollowStore = false;
        if (!StringUtil.isNullOrZero(memberId)) {
            MemberFollowStoreExample memberFollowStoreExample = new MemberFollowStoreExample();
            memberFollowStoreExample.setMemberId(memberId);
            memberFollowStoreExample.setStoreId(storeId);
            List<MemberFollowStore> list = memberFollowStoreModel.getMemberFollowStoreList(memberFollowStoreExample, null);
            if (!CollectionUtils.isEmpty(list)) {
                isFollowStore = true;
            }
        }
        return isFollowStore;
    }

    /**
     * 获取关联版式内容
     *
     * @param templateId 模板id
     * @return
     */
    private String getRelatedTemplateContent(Integer templateId) {
        String templateContent = "";
        if (!StringUtil.isNullOrZero(templateId)) {
            GoodsRelatedTemplate goodsRelatedTemplate = goodsRelatedTemplateModel.getGoodsRelatedTemplateByTemplateId(templateId);
            if (goodsRelatedTemplate != null) {
                templateContent = goodsRelatedTemplate.getTemplateContent();
            }
        }
        return templateContent;
    }

}
