package com.gree.mall.manager.bean.goods;

import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel
@Data
public class GoodsSortBean {

    @ApiModelProperty(value = "商品排序对应值 PRICE=销售价格 SHARE=分销金额 SOLD=销量 STOCK=库存")
    private String sortColumn;
    @ApiModelProperty(value = "true=升序 false=倒序")
    private boolean type = true ;


    public static String convertColumn(JSONArray goodsSortBeanArray) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < goodsSortBeanArray.size(); i++) {
            GoodsSortBean goodsSortBean = goodsSortBeanArray.getObject(i, GoodsSortBean.class);
            switch (goodsSortBean.getSortColumn()) {
                case "PRICE" :
                    sb.append("t2.price ").append(goodsSortBean.isType() ? "ASC," : "DESC,");
                    break;
                case "SHARE" :
                    sb.append("t2.share_amount ").append(goodsSortBean.isType() ? "ASC," : "DESC,");
                    break;
                case "SOLD" :
                    sb.append("t2.sold_num ").append(goodsSortBean.isType() ? "ASC," : "DESC,");
                    break;
                case "STOCK" :
                    sb.append("t2.stock_num ").append(goodsSortBean.isType() ? "ASC," : "DESC,");
                    break;
                default :
            }
        }
        if(sb.toString().length() == 0) {
            return null;
        }
        String resStr = sb.toString();
        return resStr.substring(0, resStr.length() - 1);
    }
}
