package com.izouma.dingdong.service.merchant;

import com.izouma.dingdong.domain.merchant.FullReduction;
import com.izouma.dingdong.dto.FullReductionDTO;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.FullReductionRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class FullReductionService {

    private FullReductionRepo fullReductionRepo;

//    private MerchantRepo merchantRepo;

    private MerchantService merchantService;

    /*
    添加
     */
    public void add(Long userId, List<FullReductionDTO> fullReductionDTOS) {
        Long merchantId = merchantService.findMerchantId(userId);

        for (FullReductionDTO dto : fullReductionDTOS) {
            toDTO(merchantId, dto);
        }
    }

    /*
    DTO转实体
     */
    private void toDTO(Long id, FullReductionDTO dto) {
        if (dto.getFullAmount().compareTo(dto.getMinusAmount()) <= 0) {
            throw new BusinessException("减的金额不能大于等于满的金额");
        }
        FullReduction fullReduction = new FullReduction();
        fullReduction.setFullAmount(dto.getFullAmount());
        fullReduction.setMinusAmount(dto.getMinusAmount());
        //fullReduction.setUserId(userId);
        fullReduction.setMerchantId(id);
        fullReductionRepo.save(fullReduction);
    }
}
