package com.slodon.b2b2c.vo.goods;

import com.slodon.b2b2c.core.constant.GoodsRankConst;
import com.slodon.b2b2c.core.i18n.Language;
import com.slodon.b2b2c.core.util.FileUrlUtil;
import com.slodon.b2b2c.goods.pojo.GoodsRank;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import static com.slodon.b2b2c.vo.goods.GoodsRankVO.dealIsAutoUpdateValue;
import static com.slodon.b2b2c.vo.goods.GoodsRankVO.dealRankTypeValue;

/**
 *
 * 封装榜单详情VO对象
 * @author wuxy
 */
@Data
public class GoodsRankDetailVO {

    @ApiModelProperty("榜单id")
    private Integer rankId;

    @ApiModelProperty("榜单名称")
    private String rankName;

    @ApiModelProperty("榜单分类id")
    private Integer categoryId;

    @ApiModelProperty("榜单分类名称")
    private String categoryName;

    @ApiModelProperty("排序")
    private Integer sort;

    @ApiModelProperty("背景图")
    private String backgroundImage;

    @ApiModelProperty("背景图(绝对地址)")
    private String backgroundImageUrl;

    @ApiModelProperty("榜单类型：1-畅销榜；2-好评榜；3-新品榜；4-自定义")
    private Integer rankType;

    @ApiModelProperty("榜单类型：1-畅销榜；2-好评榜；3-新品榜；4-自定义")
    private String rankTypeValue;

    @ApiModelProperty("商品分类id")
    private Integer goodsCategoryId;

    @ApiModelProperty("商品分类名称")
    private String goodsCategoryName;

    @ApiModelProperty("统计时间：1-近7天；2-近30天")
    private Integer statsTime;

    @ApiModelProperty("统计时间：1-近7天；2-近30天")
    private String statsTimeValue;

    @ApiModelProperty("畅销榜计算规则：1-销量排行；2-销售额排行")
    private Integer bestSellerRankRule;

    @ApiModelProperty("新品榜计算规则：1-按照商品发布的时间降序排列；2-按照商品发布的时间升序排列")
    private Integer newProductRankRule;

    @ApiModelProperty("好评数")
    private Integer highCommentNum;

    @ApiModelProperty("好评率")
    private Integer highCommentRate;

    @ApiModelProperty("更新方式：1-自动；0-手动")
    private Integer isAutoUpdate;

    @ApiModelProperty("更新方式：1-自动；0-手动")
    private String isAutoUpdateValue;

    @ApiModelProperty("计算规则")
    private String ruleValue;

    public GoodsRankDetailVO(GoodsRank goodsRank) {
        this.rankId = goodsRank.getRankId();
        this.rankName = goodsRank.getRankName();
        this.categoryId = goodsRank.getCategoryId();
        this.categoryName = goodsRank.getCategoryName();
        this.sort = goodsRank.getSort();
        this.backgroundImage = goodsRank.getBackgroundImage();
        this.backgroundImageUrl = FileUrlUtil.getFileUrl(goodsRank.getBackgroundImage(), null);
        this.rankType = goodsRank.getRankType();
        this.rankTypeValue = dealRankTypeValue(goodsRank.getRankType());
        this.goodsCategoryId = goodsRank.getGoodsCategoryId();
        this.goodsCategoryName = goodsRank.getGoodsCategoryName();
        this.statsTime = goodsRank.getStatsTime();
        this.statsTimeValue = dealStatsTimeValue(goodsRank.getStatsTime());
        this.bestSellerRankRule = goodsRank.getBestSellerRankRule();
        this.newProductRankRule = goodsRank.getNewProductRankRule();
        this.highCommentNum = goodsRank.getHighCommentNum();
        this.highCommentRate = goodsRank.getHighCommentRate();
        this.isAutoUpdate = goodsRank.getIsAutoUpdate();
        this.isAutoUpdateValue = dealIsAutoUpdateValue(goodsRank.getIsAutoUpdate());
        this.ruleValue = dealRuleValue(goodsRank.getRankType(), goodsRank.getBestSellerRankRule(), goodsRank.getNewProductRankRule(),
                goodsRank.getHighCommentNum(), goodsRank.getHighCommentRate());
    }

    public static String dealStatsTimeValue(Integer statsTime) {
        String value = null;
        if (StringUtils.isEmpty(statsTime)) return null;
        switch (statsTime) {
            case GoodsRankConst.STATS_TIME_1:
                value = "近7天";
                break;
            case GoodsRankConst.STATS_TIME_2:
                value = "近30天";
                break;
            default:
                break;
        }
        //翻译
        value = Language.translate(value);
        return value;
    }

    public static String dealRuleValue(Integer rankType, Integer bestSellerRankRule, Integer newProductRankRule,
                                       Integer highCommentNum, Integer highCommentRate) {
        String value = null;
        if (StringUtils.isEmpty(rankType)) return null;
        if (rankType == GoodsRankConst.RANK_TYPE_1) {
            if (bestSellerRankRule == GoodsRankConst.BEST_SELLER_RANK_RULE_1) {
                value = "销量排行";
            } else {
                value = "销售额排行";
            }
        } else if (rankType == GoodsRankConst.RANK_TYPE_2) {
            value = "好评数不低于" + highCommentNum + "条,好评率不低于" + highCommentRate + "%";
        } else if (rankType == GoodsRankConst.RANK_TYPE_3) {
            if (newProductRankRule == GoodsRankConst.NEW_PRODUCT_RANK_RULE_1) {
                value = "按照商品发布的时间降序排列";
            } else {
                value = "按照商品发布的时间升序排列";
            }
        }
        //翻译
        value = Language.translate(value);
        return value;
    }

}
