package cn.ysk.cashier.service.impl.order;

import cn.ysk.cashier.pojo.order.TbGroupOrderCoupon;
import cn.ysk.cashier.repository.order.TbGroupOrderCouponRepository;
import cn.ysk.cashier.service.order.TbGroupOrderCouponService;
import cn.ysk.cashier.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TbGroupOrderCouponServiceImpl implements TbGroupOrderCouponService {
    private final TbGroupOrderCouponRepository tbGroupOrderCouponRepository;

    @Override
    public List<TbGroupOrderCoupon> queryAll(Integer orderId){
        return tbGroupOrderCouponRepository.queryByOrderId(orderId);
    }

    @Override
    public List<TbGroupOrderCoupon> queryNoRefundByOrderId(Integer orderId) {
        return tbGroupOrderCouponRepository.queryByOrderIdAndAndIsRefund(orderId,0);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbGroupOrderCoupon resources) {
        TbGroupOrderCoupon tbGroupOrderCoupon = tbGroupOrderCouponRepository.findById(resources.getId()).orElseGet(TbGroupOrderCoupon::new);
        ValidationUtil.isNull( tbGroupOrderCoupon.getId(),"TbGroupOrderCoupon","id",resources.getId());
        tbGroupOrderCoupon.copy(resources);
        tbGroupOrderCouponRepository.save(tbGroupOrderCoupon);
    }
}
