package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.CustomCompanyRepository;
import com.crm.manager.service.CustomCompanyService;
import com.crm.rely.backend.core.entity.custom.company.CustomCompanySaveEntity;
import com.crm.rely.backend.core.pojo.table.CustomCompanyTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: houn
 * @Date: 2020/12/28 12:01
 * @Description:
 */
@Service
public class CustomCompanyServiceImpl implements CustomCompanyService {

    @Autowired
    private CustomCompanyRepository customCompanyRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomCompanyTable save(CustomCompanySaveEntity entity) throws ServiceException {

        CustomCompanyTable customCompanyTable = null;
        if (entity.getId() != null) {
            customCompanyTable = customCompanyRepository.findFirstById(entity.getId());
            if (customCompanyTable != null) {
                customCompanyTable.setAddIp(entity.getIp());
                customCompanyTable.setAddTime(entity.getTime());
                customCompanyTable.setAddUser(entity.getUser());
            }
        }
        if (customCompanyTable == null) {
            customCompanyTable = new CustomCompanyTable();
            customCompanyTable.setModifyIp(entity.getIp());
            customCompanyTable.setModifyTime(entity.getTime());
            customCompanyTable.setModifyUser(entity.getUser());
            customCompanyTable.setId(null);
        }

        BeanUtils.copyProperties(entity, customCompanyTable);
        customCompanyRepository.save(customCompanyTable);

        return customCompanyTable;
    }
}
