package com.izouma.dingdong.service.user;

import com.izouma.dingdong.converter.StringArrayConverter;
import com.izouma.dingdong.domain.user.UserCoupon;
import com.izouma.dingdong.repo.user.UserCouponRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class UserCouponService {

    private UserCouponRepo userCouponRepo;

    public void issue(Long userId, String couponId) {
        //转换
        StringArrayConverter converter = new StringArrayConverter();
        List<String> strings = converter.convertToEntityAttribute(couponId);
        strings.forEach(id -> {
            UserCoupon build = UserCoupon.builder()
                    .couponId(Long.parseLong(id))
                    .isUsed(false)
                    .userId(userId)
                    .build();
            userCouponRepo.save(build);
        });
    }
}
