package cn.ysk.cashier.service.shop;

import cn.ysk.cashier.dto.shop.TbPurchaseNoticeDto;
import cn.ysk.cashier.mapper.shop.TbPurchaseNoticeMapper;
import cn.ysk.cashier.pojo.shop.TbPurchaseNotice;
import cn.ysk.cashier.repository.shop.TbPurchaseNoticeRepository;
import cn.ysk.cashier.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class TbPurchaseNoticeService{

    private final TbPurchaseNoticeRepository tbPurchaseNoticeRepository;
    private final TbPurchaseNoticeMapper tbPurchaseNoticeMapper;

    @Transactional(rollbackFor = Exception.class)
    public TbPurchaseNoticeDto create(TbPurchaseNotice resources) {
        return tbPurchaseNoticeMapper.toDto(tbPurchaseNoticeRepository.save(resources));
    }

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

    
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tbPurchaseNoticeRepository.deleteById(id);
        }
    }
}