package com.realnet.vpspack.Services;
import com.realnet.vpspack.Repository.Test_visaRepository;
import com.realnet.vpspack.Entity.Test_visa
;import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import com.realnet.SequenceGenerator.Service.SequenceService;
import org.springframework.data.domain.Page;
import com.realnet.realm.Entity.Realm;
import com.realnet.realm.Services.RealmService;
import org.springframework.data.domain.Pageable;
import org.springframework.http.*;
import com.realnet.users.service1.AppUserServiceImpl;
import com.realnet.users.entity1.AppUser;






import com.realnet.vpspack.Entity.Visa_status;
import com.realnet.vpspack.Services.Visa_statusService;



import com.realnet.vpspack.Entity.Visa_duration;
import com.realnet.vpspack.Services.Visa_durationService;



	import org.springframework.stereotype.Service;

@Service
 public class Test_visaService {
@Autowired
private Test_visaRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private Visa_statusService visa_statusserv;

@Autowired
	private Visa_durationService visa_durationserv;

public Test_visa Savedata(Test_visa data) {







 
if (data.getVisa_status() != null) {
			Visa_status get =  visa_statusserv.getdetailsbyId(data.getVisa_status());
			data.setVisa_statusname(get.getName());

		}

 



 
if (data.getVisa_duration() != null) {
			Visa_duration get =  visa_durationserv.getdetailsbyId(data.getVisa_duration());
			data.setVisa_durationname(get.getName());

		}

 


	data.setUpdatedBy(getUser().getUserId());
		data.setCreatedBy(getUser().getUserId());
		data.setAccountId(getUser().getAccount().getAccount_id());
Test_visa save = Repository.save(data);
				return save;	
			}


//	get all with pagination
	public Page<Test_visa> getAllWithPagination(Pageable page) {
		return Repository.findAll(page, getUser().getUserId());
	}			
public List<Test_visa> getdetails() {  
		List<Realm> realm = realmService.findByUserId(getUser().getUserId());
List<Test_visa> all = Repository.findAll(getUser().getUserId());
		
		return all ;		}


public Test_visa getdetailsbyId(Integer id) {
	return Repository.findById(id).get();
			}


	public void delete_by_id(Integer id) {
 Repository.deleteById(id);
}


public Test_visa update(Test_visa data,Integer id) {
	Test_visa old = Repository.findById(id).get();
old.setName(data.getName());

old.setDescription(data.getDescription());

old.setActive (data.isActive());

old.setVisa_status(data.getVisa_status());

old.setVisa_duration(data.getVisa_duration());

final Test_visa test = Repository.save(old);
		data.setUpdatedBy(getUser().getUserId());
  return test;} 










 public AppUser getUser() {
		AppUser user = userService.getLoggedInUser();
		return user;

	}}
