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

import com.liquidnet.common.third.sqb.param.response.data.MallProductsQueryData;
import com.liquidnet.commons.lang.constant.LnsRegex;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.goblin.dto.GoblinGoodsSpecDto;
import com.liquidnet.service.goblin.dto.vo.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@ApiModel(value = "GoblinStoreMgtGoodsSqbAddParam", description = "商品管理:添加收钱吧商品入参")
@Data
public class GoblinStoreMgtGoodsSqbAddParam implements Serializable {
    private static final long serialVersionUID = -6261151635859590265L;
    @ApiModelProperty(position = 10, required = true, value = "店铺ID")
    @NotNull(message = "店铺ID不能为空")
    private String storeId;
    @ApiModelProperty(position = 11, required = false, value = "商品ID[编辑时必传]")
    private String spuId;

    /**
     * ---------------------------- 基本信息 ----------------------------
     **/

    @ApiModelProperty(position = 12, required = true, value = "商品名称[36]")
    @NotBlank(message = "商品名称不能为空")
    @Size(max = 36, message = "商品名称长度超限")
    private String name;

    @ApiModelProperty(position = 15, required = true, value = "商品一级分类ID[30]")
    @NotBlank(message = "商品分类ID不能为空")
    private String cateFid;
    @ApiModelProperty(position = 16, required = true, value = "商品二级分类ID[30]")
    private String cateSid;
    @ApiModelProperty(position = 17, required = true, value = "商品三级分类ID[30]")
    private String cateTid;

    @ApiModelProperty(position = 18, required = false, value = "商品简介[256]", example = "商品简介...")
    @Size(max = 256, message = "商品简介内容过长")
    private String intro;

    @ApiModelProperty(position = 19, required = false, value = "商品标签列表")
    private List<String> tagList;
    @ApiModelProperty(position = 20, required = true, value = "商品图片列表")
    private List<String> imageList;
    @ApiModelProperty(position = 21, required = false, value = "商品图片封面")
    @Size(max = 256, message = "商品图片封面URL过长")
    private String coverPic;
    @ApiModelProperty(position = 22, required = false, value = "商品视频地址")
    @Size(max = 256, message = "商品视频地址URL过长")
    private String video;

    /**
     * ---------------------------- 价格库存 ----------------------------
     **/

    @ApiModelProperty(position = 24, required = false, value = "SKU规格信息[初次添加商品必填]")
    @Valid
    private GoblinStoreMgtGoodsSqbAddSkuParam skuParam;
    @ApiModelProperty(position = 26, required = false, value = "商品编码[默认为系统编码，也可手动输入商家自己的编码]")
    @Pattern(regexp = LnsRegex.Valid.ALPHABET_NUMBER_UNDER_50, message = "商品编码格式或长度有误")
    private String spuNo;

    /**
     * ---------------------------- 商品介绍 ----------------------------
     **/

    @ApiModelProperty(position = 27, required = true, value = "商品详情", example = "商品详情...")
    @Size(max = 10000, message = "商品详情内容过长")
    private String details;

    /**
     * ---------------------------- 销售属性 ----------------------------
     **/

    @ApiModelProperty(position = 28, required = true, value = "上架处理方式[1-等待手动上架|2-直接上架售卖|3-预约定时上架]", example = "1")
    @NotNull(message = "上架处理方式不能为空")
    @Pattern(regexp = "\\b(1|2|3)\\b", message = "规格展现方式参数无效")
    private String shelvesHandle;
    @ApiModelProperty(position = 29, required = false, value = "预约上架时间[yyyy-MM-dd HH:mm:ss][上架处理方式为3-预约定时上架时需要指定]")
    @Pattern(regexp = LnsRegex.Valid.DATETIME_FULL, message = "预约上架时间格式有误")
    private String shelvesTime;

    /**
     * ---------------------------- 服务保障 ----------------------------
     **/

    @ApiModelProperty(position = 32, required = false, value = "商品服务支持ID列表")
    private List<String> ssidList;

    /**
     * ---------------------------- 专属标签 ----------------------------
     **/

    @ApiModelProperty(position = 33, required = false, value = "商品关联音乐人、艺术家、品牌方、厂牌列表")
    private List<String> extagList;

    /**
     * ---------------------------- AR标签 ----------------------------
     **/

    @ApiModelProperty(position = 33, required = false, value = "商品关联音乐人、艺术家、品牌方、厂牌、AR列表")
    private List<String> artagList;

    /**
     * 生成券类SPU
     *
     * @return GoblinGoodsInfoVo
     */
    public GoblinGoodsInfoVo initGoodsInfoVo(String storeId, GoblinSqbPerfGoodsVo sqbGoods, String currentUid, LocalDateTime nowTime) {
        GoblinGoodsInfoVo vo = GoblinGoodsInfoVo.getNew();
        vo.setStoreId(storeId);
        // 主表使用系统ID，收钱吧ID仅通过`goblin_sqb_goods_ext`映射
        vo.setSpuId(IDGenerator.nextMilliId2());
        vo.setSpuNo(vo.getSpuId());
        vo.setSpuType(33);
        vo.setName(sqbGoods.getTitle());
        vo.setIntro("");
        vo.setDetails(sqbGoods.getProductIntroduction());
        vo.setCoverPic(CollectionUtil.isEmpty(sqbGoods.getConverImages()) ? "" : sqbGoods.getConverImages().get(0));
        vo.setVideo("");
        vo.setSpecMode("1");
        vo.setCateFid("2690132621531");
        vo.setCateSid("2690132639617");
        vo.setCateTid("2690132656988");
        vo.setShelvesHandle("1");
        vo.setShelvesTime(null);
        vo.setVirtualFlg("1");
        vo.setStatus("3");
        vo.setShelvesStatus("0");
        vo.setSpuAppear("0");
        vo.setDelFlg("0");
        vo.setImageList(sqbGoods.getConverImages());
        vo.setLogisticsTemplate("");
        vo.setCreatedBy(currentUid);
        vo.setCreatedAt(nowTime);
        return vo;
    }


    /**
     * 编辑SPU参数整理
     *
     * @return GoblinGoodsInfoVo
     */
    public GoblinGoodsInfoVo initEditGoodsInfoVo(GoblinSqbPerfGoodsVo sqbGoods,
                                                 GoblinGoodsInfoVo mgtGoodsInfoVo) {
        GoblinGoodsInfoVo vo = mgtGoodsInfoVo;
        vo.setName(sqbGoods.getTitle());
        vo.setDetails(sqbGoods.getProductIntroduction());
        vo.setCoverPic(CollectionUtil.isEmpty(sqbGoods.getConverImages()) ? "" : sqbGoods.getConverImages().get(0));
//        vo.setImageList(sqbGoods.getConverImages());
        return vo;
    }

    /**
     * 生成SKU列表以及SPU规格等信息
     *
     * @param goodsInfoVo  GoblinGoodsInfoVo
     * @param skuInfoVos   List<GoblinGoodsSkuInfoVo>
     * @param goodsSpecVos List<GoblinGoodsSpecVo>
     */
    public void initGoodsSkuInfoVo(GoblinGoodsInfoVo goodsInfoVo, List<GoblinGoodsSkuInfoVo> skuInfoVos,
                                   List<GoblinGoodsSpecVo> goodsSpecVos, GoblinSqbPerfGoodsVo sqbGoods) {
        List<MallProductsQueryData.Sku> addSkuParamSqb = sqbGoods.getSkuResults();
        if (CollectionUtil.isEmpty(addSkuParamSqb)) {
            addSkuParamSqb = new ArrayList<>();
        }
        List<String> skuIdList = CollectionUtil.arrayListString();
        for (MallProductsQueryData.Sku sku : addSkuParamSqb) {
            GoblinGoodsSkuInfoVo skuInfoVo = GoblinGoodsSkuInfoVo.getNew();
            String systemSkuId = IDGenerator.nextSnowId();
            skuInfoVo.setSpuId(goodsInfoVo.getSpuId());
            // 主表使用系统SKU_ID，收钱吧SKU_ID仅通过`goblin_sqb_goods_ext`映射
            skuInfoVo.setSkuId(systemSkuId);
            skuInfoVo.setSkuNo(systemSkuId);
            skuInfoVo.setSkuBarCode(systemSkuId);
            skuInfoVo.setSkuType(33);
            skuInfoVo.setName(sku.getSkuName());
            skuInfoVo.setSubtitle("");
            skuInfoVo.setSkuPic("");
            skuInfoVo.setSkuIsbn("");
            skuInfoVo.setStock(999);
            skuInfoVo.setSkuStock(999);
            skuInfoVo.setPrice(BigDecimal.valueOf(sku.getPrice()));
            skuInfoVo.setPriceMember(BigDecimal.valueOf(sku.getPrice()));
            skuInfoVo.setBuyFactor("0");
            skuInfoVo.setBuyLimit(0);
            skuInfoVo.setStoreId(goodsInfoVo.getStoreId());
            skuInfoVo.setVirtualFlg("1");
            skuInfoVo.setStatus("3");// 没有审核流程，默认通过
            skuInfoVo.setShelvesStatus(goodsInfoVo.getShelvesStatus());
            skuInfoVo.setSkuAppear("0");
            skuInfoVo.setDelFlg("0");
            skuInfoVo.setCreatedBy(goodsInfoVo.getCreatedBy());
            skuInfoVo.setCreatedAt(goodsInfoVo.getCreatedAt());
            skuInfoVo.setLogisticsTemplate("");
            skuInfoVo.setErpType(goodsInfoVo.getErpType());
            skuInfoVo.setErpHosting(0);

            {// 规格信息处理
                // 收钱吧商品固定规格
                GoblinGoodsSpecDto goblinGoodsSpecDto = GoblinGoodsSpecDto.getNew();
                goblinGoodsSpecDto.setSpecName("规格");
                goblinGoodsSpecDto.setSpecVname("默认");

                // SKU规格设置
                List<GoblinGoodsSpecDto> skuSpecList = new ArrayList<>();
                skuSpecList.add(goblinGoodsSpecDto);
                skuInfoVo.setSkuSpecList(skuSpecList);
            }
            skuIdList.add(systemSkuId);

            skuInfoVos.add(skuInfoVo);
        }

        // SPU规格集合
        GoblinGoodsSpecValueVo specValueVo = GoblinGoodsSpecValueVo.getNew().setSpecVname("规格").setSpecVsort(0);
        ArrayList<GoblinGoodsSpecValueVo> specValueVos = new ArrayList<>();
        specValueVos.add(specValueVo);
        goodsSpecVos.add(GoblinGoodsSpecVo.getNew().setSpecName("默认").setSpecSort(0).setSpecValues(specValueVos));

        goodsInfoVo.setPriceGe(BigDecimal.ZERO);
        goodsInfoVo.setPriceLe(BigDecimal.ZERO);
        goodsInfoVo.setSkuIdList(skuIdList);
        goodsInfoVo.setSpecVoList(goodsSpecVos);
    }
}
