package com.ycwl.basic.repository;

import com.ycwl.basic.pricing.entity.PriceOnePriceConfig;
import com.ycwl.basic.pricing.service.IOnePricePurchaseService;
import com.ycwl.basic.model.pc.price.entity.PriceConfigEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class PriceRepository {
    @Autowired
    private IOnePricePurchaseService onePricePurchaseService;

    public PriceConfigEntity getPriceConfigByScenicTypeGoods(Long scenicId, Integer type, String goodsId) {
        if (type == -1) {
            PriceOnePriceConfig config = onePricePurchaseService.getActiveConfigByScenic(scenicId);
            if (config == null || !config.isTimeValid() || !config.isScenicMatch(scenicId)) {
                return null;
            }
            PriceConfigEntity entity = new PriceConfigEntity();
            entity.setId(config.getId().intValue());
            entity.setScenicId(config.getScenicId());
            entity.setType(type);
            entity.setSlashPrice(config.getOriginalPrice());
            entity.setPrice(config.getOnePrice());
            entity.setGoodsIds(goodsId);
            return entity;
        }
        return null;
    }

    public PriceConfigEntity getPriceConfig(Integer id) {
        PriceOnePriceConfig config = onePricePurchaseService.getConfigById(Long.valueOf(id));
        if (config != null && config.isTimeValid() && config.getIsActive()) {
            PriceConfigEntity priceConfig = new PriceConfigEntity();
            priceConfig.setId(config.getId().intValue());
            priceConfig.setScenicId(config.getScenicId());
            priceConfig.setType(-1);
            priceConfig.setGoodsIds("");
            priceConfig.setSlashPrice(config.getOriginalPrice());
            priceConfig.setPrice(config.getOnePrice());
            return priceConfig;
        }
        return null;
    }

}
