package com.xinelu.manage.vo.goodsCategory;

import com.xinelu.manage.domain.goodsCategory.GoodsCategory;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.Comparator;
import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * Create by 2023/1/13 15:07
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GoodsCategoryVO extends GoodsCategory {
    private static final long serialVersionUID = 7306585265130625106L;

    @ApiModelProperty(value = "父节点名称")
    private String goodsCategoryName;

    @ApiModelProperty("子分类列表")
    private List<GoodsCategoryVO> children;

    public GoodsCategoryVO(GoodsCategory category) {
        BeanUtils.copyProperties(category, this);
    }

    public GoodsCategoryVO(Long id,
                           Long parentId,
                           String goodsCategoryName,
                           String goodsCategoryCode,
                           String goodsCategoryPicture,
                           String categoryRemark,
                           Integer categoryLevel,
                           Integer sort) {
        super(id,
                parentId,
                goodsCategoryName,
                goodsCategoryCode,
                goodsCategoryPicture,
                categoryRemark,
                categoryLevel,
                sort);
    }


    public List<GoodsCategoryVO> getChildren() {

        if (children != null) {
            children.sort(new Comparator<GoodsCategoryVO>() {
                @Override
                public int compare(GoodsCategoryVO o1, GoodsCategoryVO o2) {
                    return o1.getSort().compareTo(o2.getSort());
                }
            });
            return children;
        }
        return null;
    }
}
