这次是需要写一个接口,用来传递数据,其他用户使用我们系统的时候有一些部门我们表里是不存在的,people表和department表是有关联,所以第一个是传递过来的数据进行整理,然后把不存在的部门插入到表中.
skip_before_filter :check_if_login_required //跳过用户验证
before_filter :set_app_user #使用token进行用户验证
@err_mess = []
niu_department = params[:totle].to_s.split("-")[0]
niu_center = params[:totle].to_s.split("-")[1]
niu_office_location = params[:totle].to_s.split("-")[2]
current_system_id = Irm::ExternalSystem.current_system.id
source_system_id = Irm::ExternalSystem.current_system.id
这是对传过来的参数进行处理,如”蒙牛-财务-西安”,需要用分隔符取出来
if !Niu::OrganizationalStructure.find_by(organizational_structure_code:params[:niu_department_id]).present?
begin
ActiveRecord::Base.transaction do
department = Niu::OrganizationalStructure.new(:organizational_structure_code => params[:niu_department_id],
:organizational_structure_type => "department",
:description => niu_department,
:current_system_id => current_system_id,
:source_system_id => source_system_id )
end
rescue => e
err_mess << ["#{department.errors.to_a}"]
end
end
先判断这个部门id存在不存在,如果不存在,则进行new操作,部门的就是这样
员工则需要判断这个员工是否存在,不存在则创建,存在则进行更新
@err_mess = []
if params[:login_name].present?
if Irm::Person.find_by(login_name:params[:login_name]).present?
person = Irm::Person.find_by(login_name:params[:login_name])
update_attributes_hash = {}
update_attributes_hash[:first_name] = params[:first_name] if params[:first_name].present?
update_attributes_hash[:email_address] = params[:email_address] if params[:email_address].present?
``````
if !person.update_attributes(update_attributes_hash)
@err_mess << ["#{person.errors.to_a}"]
end
end
这个是更新,如果不存在,需要创建,跟创建部门类似