package com.crm.pay.service.impl;

import com.crm.pay.dao.repository.FinanceDepositSerialMappingRepository;
import com.crm.pay.service.FinanceDepositSerialMappingService;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.entity.finance.serial.mapping.FinanceDepositSerialMappingEntity;
import com.crm.rely.backend.model.pojo.table.FinanceDepositSerialMappingTable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FinanceDepositSerialMappingServiceImpl implements FinanceDepositSerialMappingService {


    @Autowired
    private FinanceDepositSerialMappingRepository financeDepositSerialMappingRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FinanceDepositSerialMappingTable save(FinanceDepositSerialMappingEntity financeDepositSerialMappingEntity) throws ServiceException {

        FinanceDepositSerialMappingTable financeDepositSerialMappingTable = new FinanceDepositSerialMappingTable();

        BeanUtils.copyProperties(financeDepositSerialMappingEntity, financeDepositSerialMappingTable);

        financeDepositSerialMappingRepository.save(financeDepositSerialMappingTable);
        return financeDepositSerialMappingTable;
    }

    @Override
    public FinanceDepositSerialMappingTable getById(Long id) throws ServiceException {
        FinanceDepositSerialMappingTable financeDepositSerialMappingTable =
                financeDepositSerialMappingRepository.getFirstById(id);

        return financeDepositSerialMappingTable;
    }

    @Override
    public FinanceDepositSerialMappingTable getById(Long id, String payType) throws ServiceException {
        FinanceDepositSerialMappingTable financeDepositSerialMappingTable =
                financeDepositSerialMappingRepository.getFirstByIdAndPayType(id, payType);

        return financeDepositSerialMappingTable;
    }
}
