package com.bonus.canteen.core.nutrition.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.nutrition.mapper.HealthInfoChronicMapper;
import com.bonus.canteen.core.nutrition.common.model.HealthInfoChronic;
import com.bonus.canteen.core.nutrition.service.HealthChronicService2;
import com.bonus.canteen.core.nutrition.service.HealthInfoChronicService;
import com.bonus.common.houqin.utils.id.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HealthInfoChronicServiceImpl extends ServiceImpl<HealthInfoChronicMapper, HealthInfoChronic> implements HealthInfoChronicService {
   @Autowired
   private HealthChronicService2 healthChronicService;

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void insertAssociateBatch(Long userId, List<Long> chronicIds) {
      List<HealthInfoChronic> healthInfoChronics = (List)chronicIds.stream().map((chronicId) -> {
         return HealthInfoChronic.builder().id(Id.next()).userId(userId).chronicId(chronicId).chronicName(this.healthChronicService.selectNameById(chronicId)).build();
      }).collect(Collectors.toList());
      this.saveBatch(healthInfoChronics);
   }

}
