package com.czg.order.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 限时折扣dto
 * order专用 对应mk_limit_time_discount
 *
 * @author ww
 * @description
 */
@Data
public class LimitRateDTO implements Serializable {

    private Long id;
    /**
     * 折扣% 范围1-99
     */
    private Integer discountRate;

    /**
     * 折扣优先级 limit-time/vip-price
     */
    private String discountPriority;

    /**
     * 参与商品 1全部 2部分
     */
    private Integer foodType;

    /**
     * 参与商品
     */
    private String foods;

    private Set<Long> foodIds;

    public Set<Long> getFoodIds() {
        if (CollUtil.isEmpty(foodIds) && StrUtil.isNotBlank(foods)) {
            foodIds = CollUtil.newHashSet(StrUtil.split(foods, ","))
                    .stream()
                    .map(Long::parseLong)
                    .collect(Collectors.toSet());
        }
        return foodIds;
    }


    public boolean isLimitFoodId(Long foodId) {
        if (foodType == 1) {
            return true;
        } else {
            Set<Long> foodIds = getFoodIds();
            if (CollUtil.isEmpty(foodIds)) {
                return false;
            } else {
                return foodIds.contains(foodId);
            }
        }
    }
}
