package com.slodon.b2b2c.vo.goods;

import com.alibaba.fastjson.JSON;
import com.slodon.b2b2c.core.util.FileUrlUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.goods.dto.GoodsPublishFrontParamDTO;
import com.slodon.b2b2c.goods.pojo.GoodsPlatform;
import com.slodon.b2b2c.goods.pojo.GoodsPlatformBindReserveName;
import com.slodon.b2b2c.goods.pojo.GoodsPlatformPicture;
import com.slodon.b2b2c.goods.pojo.ProductPlatform;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 封装商品库商品详情VO对象
 * @author wuxy
 * @date 2021.10.26 19:19
 */
@Data
public class GoodsPlatformDetailVO implements Serializable {

    private static final long serialVersionUID = 6457572793017181777L;
    @ApiModelProperty(value = "平台库商品ID")
    private Long platformGoodsId;
    @ApiModelProperty("商品名称")
    private String goodsName;
    @ApiModelProperty("商品广告语")
    private String goodsBrief;
    @ApiModelProperty("品牌ID")
    private Integer brandId;
    @ApiModelProperty("品牌名称")
    private String brandName;
    @ApiModelProperty("1级分类ID")
    private Integer categoryId1;
    @ApiModelProperty("2级分类ID")
    private Integer categoryId2;
    @ApiModelProperty("3级分类ID")
    private Integer categoryId3;
    @ApiModelProperty("分类路径")
    private String categoryPath;
    @ApiModelProperty(value = "检索属性列表")
    private List<GoodsPublishFrontParamDTO.AttributeAndParameter.Attribute> attributeList;
    @ApiModelProperty(value = "自定义参数分组")
    private GoodsPublishFrontParamDTO.AttributeAndParameter.ParameterGroup parameterGroup;

    @ApiModelProperty("规格列表，多规格必传")
    private List<SpecVO> specInfoList = new ArrayList<>();
    @ApiModelProperty("货品列表，多规格必传")
    private List<ProductPlatformDetailVO> productList = new ArrayList<>();
    @ApiModelProperty(value = "市场价，无规格填写")
    private BigDecimal marketPrice;
    @ApiModelProperty(value = "销售价，无规格填写")
    private BigDecimal productPrice;
    @ApiModelProperty(value = "重量kg，无规格填写")
    private BigDecimal weight;
    @ApiModelProperty(value = "长度cm，无规格填写")
    private BigDecimal length;
    @ApiModelProperty(value = "宽度cm，无规格填写")
    private BigDecimal width;
    @ApiModelProperty(value = "高度cm，无规格填写")
    private BigDecimal height;
    @ApiModelProperty(value = "条形码，无规格填写")
    private String barCode;

    @ApiModelProperty("图片列表，无规格或未设置主规格时使用此数据")
    private List<ImageVO> imageList = new ArrayList<>();
    @ApiModelProperty(value = "商品视频")
    private String goodsVideo;
    @ApiModelProperty(value = "商品视频绝对地址")
    private String goodsVideoUrl;

    @ApiModelProperty(value = "商品详情信息")
    private String goodsDetails;
    @ApiModelProperty("状态：1-待刊登；2-已刊登；3-已下架")
    private Integer state;

    @ApiModelProperty("是否是虚拟商品：1-实物商品；2-虚拟商品")
    private Integer isVirtualGoods;
    @ApiModelProperty("售后服务：0-不支持退款；1-支持退款(虚拟商品必填)")
    private Integer afterSaleService;
    @ApiModelProperty("用户预留信息列表")
    private List<GoodsPlatformBindReserveName> reserveList;

    public GoodsPlatformDetailVO(GoodsPlatform goodsPlatform, List<GoodsPlatformPicture> goodsPictureList, //无规格时的图片
                                 List<ProductPlatform> dbProductList) {
        this.platformGoodsId = goodsPlatform.getPlatformGoodsId();
        this.goodsName = goodsPlatform.getGoodsName();
        this.goodsBrief = goodsPlatform.getGoodsBrief();
        this.brandId = goodsPlatform.getBrandId();
        this.brandName = goodsPlatform.getBrandName();
        this.categoryPath = goodsPlatform.getCategoryPath();
        this.categoryId1 = goodsPlatform.getCategoryId1();
        this.categoryId2 = goodsPlatform.getCategoryId2();
        this.categoryId3 = goodsPlatform.getCategoryId3();
        if (!StringUtils.isEmpty(goodsPlatform.getAttributeJson())) {
            this.attributeList = JSON.parseArray(goodsPlatform.getAttributeJson(), GoodsPublishFrontParamDTO.AttributeAndParameter.Attribute.class);
        }
        if (!StringUtils.isEmpty(goodsPlatform.getGoodsParameter())) {
            this.parameterGroup = JSON.parseObject(goodsPlatform.getGoodsParameter(), GoodsPublishFrontParamDTO.AttributeAndParameter.ParameterGroup.class);
        }
        //规格信息
        if (StringUtils.isEmpty(goodsPlatform.getSpecJson())) {
            //单规格
            this.marketPrice = dbProductList.get(0).getMarketPrice();
            this.productPrice = dbProductList.get(0).getProductPrice();
            this.weight = dbProductList.get(0).getWeight();
            this.length = dbProductList.get(0).getLength();
            this.width = dbProductList.get(0).getWidth();
            this.height = dbProductList.get(0).getHeight();
            this.barCode = dbProductList.get(0).getBarCode();
        } else {
            //多规格
            List<GoodsPublishFrontParamDTO.SpecInfo> dbSpecInfoList = JSON.parseArray(goodsPlatform.getSpecJson(), GoodsPublishFrontParamDTO.SpecInfo.class);
            dbSpecInfoList.forEach(specInfo -> {
                this.specInfoList.add(new SpecVO(specInfo));
            });
        }
        dbProductList.forEach(product -> {
            this.productList.add(new ProductPlatformDetailVO(product));
        });

        //图片，设置了主规格时，图片信息在规格信息中存放
        if (StringUtil.isNullOrZero(goodsPlatform.getMainSpecId())) {
            //未设置主规格
            goodsPictureList.forEach(goodsPicture -> {
                this.imageList.add(new ImageVO(goodsPicture));
            });
        }

        //视频
        this.goodsVideo = goodsPlatform.getGoodsVideo();
        this.goodsVideoUrl = FileUrlUtil.getFileUrl(goodsPlatform.getGoodsVideo(), null);

        //商品详情
        this.goodsDetails = goodsPlatform.getGoodsDetails();
        this.state = goodsPlatform.getState();

        this.isVirtualGoods = goodsPlatform.getIsVirtualGoods();
        this.afterSaleService = goodsPlatform.getAfterSaleService();
    }

    /**
     * 规格信息
     */
    @Data
    public static class SpecVO implements Serializable {

        private static final long serialVersionUID = -2528848599942357078L;
        @ApiModelProperty(value = "规格id")
        private Integer specId;
        @ApiModelProperty(value = "规格名称")
        private String specName;
        @ApiModelProperty(value = "是否主规格,1-是，0-不是")
        private Integer isMainSpec;
        @ApiModelProperty(value = "规格值列表")
        private List<SpecValueVO> specValueList = new ArrayList<>();

        public SpecVO(GoodsPublishFrontParamDTO.SpecInfo specInfo) {
            this.specId = specInfo.getSpecId();
            this.specName = specInfo.getSpecName();
            this.isMainSpec = specInfo.getIsMainSpec();
            specInfo.getSpecValueList().forEach(specValueInfo -> {
                this.specValueList.add(new SpecValueVO(specValueInfo));
            });
        }

        /**
         * 规格值信息
         */
        @Data
        public static class SpecValueVO implements Serializable {

            private static final long serialVersionUID = -7395299823386870544L;
            @ApiModelProperty(value = "规格值id")
            private Integer specValueId;
            @ApiModelProperty(value = "规格值")
            private String specValue;
            @ApiModelProperty(value = "图片路径")
            private List<ImageVO> imageList = new ArrayList<>();

            public SpecValueVO(GoodsPublishFrontParamDTO.SpecInfo.SpecValueInfo specValueInfo) {
                this.specValueId = specValueInfo.getSpecValueId();
                this.specValue = specValueInfo.getSpecValue();
                if (!CollectionUtils.isEmpty(specValueInfo.getImageList())) {
                    specValueInfo.getImageList().forEach(imageInfo -> {
                        this.imageList.add(new ImageVO(imageInfo));
                    });
                }
            }
        }
    }

    /**
     * 图片信息
     */
    @Data
    public static class ImageVO implements Serializable {
        private static final long serialVersionUID = 3435456712233455641L;
        @ApiModelProperty("是否主图：1、主图；2、非主图，主图只能有一张")
        private Integer isMain;
        @ApiModelProperty(value = "图片路径")
        private String image;
        @ApiModelProperty(value = "图片完整路径")
        private String imageUrl;

        public ImageVO(GoodsPlatformPicture goodsPicture) {
            this.isMain = goodsPicture.getIsMain();
            this.image = goodsPicture.getImagePath();
            this.imageUrl = FileUrlUtil.getFileUrl(goodsPicture.getImagePath(), null);
        }

        public ImageVO(GoodsPublishFrontParamDTO.ImageInfo imageInfo) {
            this.isMain = imageInfo.getIsMain();
            this.image = imageInfo.getImage();
            this.imageUrl = FileUrlUtil.getFileUrl(imageInfo.getImage(), null);
        }
    }

    /**
     * 商品表信息
     */
    @Data
    public static class ProductPlatformDetailVO implements Serializable {
        private static final long serialVersionUID = -5954461203857054266L;
        @ApiModelProperty("平台库货品Id")
        private Long platformProductId;

        @ApiModelProperty("规格值，用逗号分隔")
        private String specValues;

        @ApiModelProperty("规格值的ID，用逗号分隔")
        private String specValueIds;

        @ApiModelProperty(value = "货品价格")
        private BigDecimal productPrice;

        @ApiModelProperty("市场价")
        private BigDecimal marketPrice;

        @ApiModelProperty("商品条形码（标准的商品条形码）")
        private String barCode;

        @ApiModelProperty(value = "重量kg")
        private BigDecimal weight;

        @ApiModelProperty(value = "长度cm")
        private BigDecimal length;

        @ApiModelProperty(value = "宽度cm")
        private BigDecimal width;

        @ApiModelProperty(value = "高度cm")
        private BigDecimal height;

        @ApiModelProperty(value = "是否启用，1-启用；2-不启用")
        private Integer state;

        @ApiModelProperty(value = "是否默认展示或加车货品：0-否；1-是")
        private Integer isDefault;

        public ProductPlatformDetailVO(ProductPlatform productPlatform) {
            this.platformProductId = productPlatform.getPlatformProductId();
            this.specValues = productPlatform.getSpecValues();
            this.specValueIds = productPlatform.getSpecValueIds();
            this.productPrice = productPlatform.getProductPrice();
            this.marketPrice = productPlatform.getMarketPrice();
            this.barCode = productPlatform.getBarCode();
            this.weight = productPlatform.getWeight();
            this.length = productPlatform.getLength();
            this.width = productPlatform.getWidth();
            this.height = productPlatform.getHeight();
            this.state = productPlatform.getState();
            this.isDefault = productPlatform.getIsDefault();
        }
    }
}
