package com.realnet.users.service1;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.realnet.users.entity.Sys_Accounts;
import com.realnet.users.repository.SysAccountRepo;

@Service
public class SysAccountService {

	@Autowired
	private SysAccountRepo sysAccountRepo;

	public Sys_Accounts save(Sys_Accounts sys) {

		Sys_Accounts account = findByEmail(sys.getEmail());
		if (account != null) {

		}

		sys.setActive(true);
		Sys_Accounts accounts = sysAccountRepo.save(sys);

		return accounts;

	}

//	update sysaccount

	public Sys_Accounts update(Long accountId, Sys_Accounts request) {

		Sys_Accounts account = sysAccountRepo.findById(accountId).get();
		// account_id generally auto-generated hai, update me change nahi karenge

		if (request.getCompanyName() != null) {
			account.setCompanyName(request.getCompanyName());
		}

		if (request.getWorkspace() != null) {
			account.setWorkspace(request.getWorkspace());
		}

		if (request.getGstNumber() != null) {
			account.setGstNumber(request.getGstNumber());
		}

		if (request.getMobile() != null) {
			account.setMobile(request.getMobile());
		}

		if (request.getEmail() != null) {
			account.setEmail(request.getEmail());
		}

		if (request.getPancard() != null) {
			account.setPancard(request.getPancard());
		}

		if (request.getWorking() != null) {
			account.setWorking(request.getWorking());
		}

		if (request.getActive() != null) {
			account.setActive(request.getActive());
		}

		Sys_Accounts save = sysAccountRepo.save(account);

		return save;

	}

	public List<Sys_Accounts> getall() {

		List<Sys_Accounts> getall = sysAccountRepo.findAll();
		return getall;
	}

	public Sys_Accounts getBYId(Long account_id) {

		Sys_Accounts accounts = sysAccountRepo.findById(account_id).get();
		return accounts;
	}

	public Sys_Accounts findByEmail(String email) {

		Sys_Accounts accounts = sysAccountRepo.findByEmail(email);
		return accounts;
	}

}
