package com.gci.boot.modules.projects.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.common.util.ValidateUtil;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.projects.entity.TbContractVehicle;
import com.gci.boot.modules.projects.mapper.TbContractVehicleMapper;
import com.gci.boot.modules.projects.service.ITbContractVehicleService;

import cn.hutool.core.util.StrUtil;


@Service
public class TbContractVehicleServiceImpl extends ServiceImpl<TbContractVehicleMapper, TbContractVehicle> implements ITbContractVehicleService {

	@Override
	public List<TbContractVehicle> getByContractId(String contractId) {
		LambdaQueryWrapper<TbContractVehicle> query = new LambdaQueryWrapper<>();
		query.eq(TbContractVehicle::getContractId,contractId);
		return list(query);
	}
	
	@Override
	public void saveItem(List<TbContractVehicle> list) {
		int count = 1;
		Set<String> vehicleModel = new HashSet<>();
		for (TbContractVehicle tbContractVehicle : list) {
			String valid = ValidateUtil.valid(tbContractVehicle, SaveValidated.class);
	        if (StrUtil.isNotBlank(valid)) {
	            throw new GciBootException("车型明细：第"+count+"行："+valid);
	        }
	        vehicleModel.add(tbContractVehicle.getVehicleModel());
	        count++;
		}
		if(vehicleModel.size()<list.size()) {
			throw new GciBootException("车型明细：车辆型号不能重复！");
		}
		saveOrUpdateBatch(list);
	}
	
}
