Browse Source

Switched to global person id

Dmitry Yu Okunev 1 year ago
parent
commit
e08651ab7f

+ 25 - 15
app/controllers/people_controller.rb

@@ -23,15 +23,15 @@ class PeopleController < ApplicationController
 	end
 
 	def index
-		if params[:lastname].nil?
+		if params[:q].nil?
 			@people = Person.where(:id => -1)
 			return self.commonindex
 		end
 
-		if params[:lastname].nil? or params[:lastname] == ""
+		if params[:q].nil? or params[:q] == ""
 			@people = Person.preload(:passes).all
 		else
-			@people = Person.preload(:passes).where(:lastname => params[:lastname])
+			@people = Person.preload(:passes).where("fullname LIKE :q", "%#{params[:q]}%")
 		end
 
 		self.commonindex
@@ -66,9 +66,10 @@ class PeopleController < ApplicationController
 		if @person.IsManual?
 			patronymic = @person.Patronymic
 			patronymic = @person.Patronymic.gsub('Ё', 'Е').gsub('ё', 'е') unless patronymic.nil?
-			@availablePasses = Pass.where(:lastname => @person.Lastname.gsub('Ё', 'Е').gsub('ё', 'е'), :firstname => @person.Firstname.gsub('Ё', 'Е').gsub('ё', 'е'), :patronymic => patronymic)
-			#@availablePasses = Pass.where(:lastname => @person.Lastname.gsub('Ё', 'Е').gsub('ё', 'е'), :firstname => @person.Firstname.gsub('Ё', 'Е').gsub('ё', 'е')) if @availablePasses.nil? or @availablePasses.size == 0
-			@availablePasses = Pass.find_by_sql('SELECT `passes`.* FROM `passes` LEFT JOIN `people` ON `passes`.`person_id` = `people`.`id` WHERE `people`.`id` IS NULL') if @availablePasses.nil? or @availablePasses.size == 0
+			#@availablePasses = Pass.where(:lastname => @person.Lastname.gsub('Ё', 'Е').gsub('ё', 'е'), :firstname => @person.Firstname.gsub('Ё', 'Е').gsub('ё', 'е'), :patronymic => patronymic)
+			##@availablePasses = Pass.where(:lastname => @person.Lastname.gsub('Ё', 'Е').gsub('ё', 'е'), :firstname => @person.Firstname.gsub('Ё', 'Е').gsub('ё', 'е')) if @availablePasses.nil? or @availablePasses.size == 0
+			#@availablePasses = Pass.find_by_sql('SELECT `passes`.* FROM `passes` LEFT JOIN `people` ON `passes`.`person_id` = `people`.`id` WHERE `people`.`id` IS NULL') if @availablePasses.nil? or @availablePasses.size == 0
+			@availablePasses = Pass.all
 			hash[:availablePasses] = @availablePasses
 		end
 
@@ -108,7 +109,10 @@ class PeopleController < ApplicationController
 	def download
 		require 'net/http'
 
-		people_jraw = get_json_data_by_url "https://api.cps.mephi.ru/people?prepareFormulars=true&apiKey=#{Rails.application.config.cps_api_key}"
+		#people_jraw = get_json_data_by_url "https://api.cps.mephi.ru/people?prepareFormulars=true&apiKey=#{Rails.application.config.cps_api_key}"
+		#return if people_jraw.nil?
+
+		people_jraw = get_json_data_by_url "https://sd.mephi.ru/api/11/get_people.json?api_key=#{Rails.application.config.sd_api_key}"
 		return if people_jraw.nil?
 
 		passes_jraw = get_json_data_by_url "https://api.cps.mephi.ru/passes?apiKey=#{Rails.application.config.cps_api_key}"
@@ -116,17 +120,22 @@ class PeopleController < ApplicationController
 
 		#puts "#{people_jraw.to_yaml}"
 
-		employees = Hash.new
+		personById = Hash.new
+		Person.all.each do |person|
+			personById[person.id] = person
+		end
+
 		ActiveRecord::Base.transaction do
-			people_jraw["arg"].each do |employee|
-				person = Person.find_by_id( employee["EmpGUID"].to_i )
+			people_jraw.each do |person_jraw|
+				#person = Person.find_by_id( person_jraw["id"].to_i )
+				person = personById[ person_jraw["id"].to_i ]
 				if person.nil?
-					person = Person.ParseEmployee(employee)
+					person = Person.ParsePerson(person_jraw)
 					#puts "#{person.to_yaml}"
 					person.save
 				else
 					unless params[:dont_update_people]
-						person.UpdateEmployee(employee)
+						person.UpdatePerson(person_jraw)
 						#puts "#{person.to_yaml}"
 						person.save if person.changed?
 					end
@@ -189,7 +198,8 @@ class PeopleController < ApplicationController
 					end
 				end
 				if ofz_key == ""
-					ofz_key = "#{person.Lastname}#{person.Firstname}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
+					personNameParts = person.fullname.split
+					ofz_key = "#{personNameParts[0]}#{personNameParts[1]}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
 					score = 3
 				end
 				if ofz_links[ofz_key].nil? or ofz_links[ofz_key].size == 0
@@ -210,8 +220,8 @@ class PeopleController < ApplicationController
 				ofz_links[ofz_key].each do |pass_id|
 					next if passes_raw[ pass_id ]["score"] > score
 
-					passes_raw[ pass_id ]["EmpGUID"] = person.id
-					passes_raw[ pass_id ]["score"]   = score
+					passes_raw[ pass_id ]["personId"] = person.id
+					passes_raw[ pass_id ]["score"]    = score
 				end
 #				mutex.unlock
 #			}

+ 1 - 1
app/models/pass.rb

@@ -15,7 +15,7 @@ class Pass < ApplicationRecord
 		self.Lastname    = json["Lastname"  ].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
 		self.Firstname   = json["Firstname" ].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
 		self.Patronymic  = json["Patronymic"].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip unless json["Patronymic"].nil?
-		self.person_id   = json["EmpGUID"] unless self.PersonIsManual or json["EmpGUID"].blank?
+		self.person_id   = json["personId"] unless self.PersonIsManual or json["personId"].blank?
 		self.photo_id    = json["PhotoId"].to_i
 		self.is_actual   = json['IsActual']
 	end

+ 10 - 22
app/models/person.rb

@@ -1,33 +1,21 @@
 class Person < ApplicationRecord
 	has_many :passes
 
-	def self.ParseEmployee(employee)
+	def self.ParsePerson(person_raw)
 		person = Person.new
 
-		person.id          = employee["EmpGUID"].to_i
-		person.UpdateEmployee(employee)
+		person.id      = person_raw["id"].to_i
+		person.EmpGUID = person_raw["EmpGUID"].to_i
+		person.UpdatePerson(person_raw)
 
 		return person
 	end
 
-	def UpdateEmployee(employee)
-		self.Lastname    = employee["Lastname"].strip
-		self.Firstname   = employee["Firstname"].strip
-		self.Patronymic  = employee["Patronymic"].strip unless employee["Patronymic"].nil? || employee["Patronymic"].blank?
-		self.DateOfBirth = employee["BirthDay"]
-
-		formulars = []
-		unless employee["Formulars"].nil?
-			formulars = employee["Formulars"].select{ |f| f["IsActive"] }
-		end
-
-		self.is_actual = formulars.size > 0
-
-		if formulars.size > 0
-			self.Description = formulars.collect{ |f| "#{f["UnitShortName"]} (#{f["AppointmentName"]})" }.uniq.join(", ").truncate(250)
-		else
-			self.Description = "Должностей не найдено"
-		end
+	def UpdatePerson(person_raw)
+		self.fullname    = person_raw["fullname"].strip
+		self.DateOfBirth = person_raw["date_of_birth"]
+		self.is_actual   = person_raw["is_actual"].to_i
+		self.Description = person_raw["description"]
 	end
 
 	def GetPass
@@ -57,6 +45,6 @@ class Person < ApplicationRecord
 	end
 
 	def GetKey
-		return "#{self.Lastname}#{self.Firstname}#{self.Patronymic}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
+		return "#{self.Fullname}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
 	end
 end

File diff suppressed because it is too large
+ 3 - 3
app/views/people/index.html.erb


+ 7 - 0
db/migrate/20180424085720_remove_partednames_from_person.rb

@@ -0,0 +1,7 @@
+class RemovePartednamesFromPerson < ActiveRecord::Migration[5.0]
+  def change
+    remove_column :people, :Firstname, :string
+    remove_column :people, :Lastname, :string
+    remove_column :people, :Patronymic, :string
+  end
+end

+ 5 - 0
db/migrate/20180424085850_add_fullname_to_person.rb

@@ -0,0 +1,5 @@
+class AddFullnameToPerson < ActiveRecord::Migration[5.0]
+  def change
+    add_column :people, :fullname, :string
+  end
+end

+ 12 - 0
db/migrate/20180424104124_change_data_type_for_person_description.rb

@@ -0,0 +1,12 @@
+class ChangeDataTypeForPersonDescription < ActiveRecord::Migration[5.0]
+  def self.up
+    change_table :people do |t|
+      t.change :Description, :text
+    end
+  end
+  def self.down
+    change_table :people do |t|
+      t.change :Description, :string
+    end
+  end
+end

+ 5 - 0
db/migrate/20180424105114_add_emp_guid_to_people.rb

@@ -0,0 +1,5 @@
+class AddEmpGuidToPeople < ActiveRecord::Migration[5.0]
+  def change
+    add_column :people, :EmpGUID, :integer
+  end
+end

+ 10 - 7
db/schema.rb

@@ -10,7 +10,11 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20171003151446) do
+ActiveRecord::Schema.define(version: 20180424105114) do
+
+  create_table "empguid2id", primary_key: "EmpGUID", id: :bigint, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
+    t.bigint "id"
+  end
 
   create_table "passes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
     t.string   "Firstname"
@@ -39,14 +43,13 @@ ActiveRecord::Schema.define(version: 20171003151446) do
   end
 
   create_table "people", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
-    t.string   "Firstname"
-    t.string   "Lastname"
-    t.string   "Patronymic"
-    t.string   "Description"
+    t.string   "fullname"
+    t.text     "Description", limit: 65535
     t.date     "DateOfBirth"
-    t.datetime "created_at",  null: false
-    t.datetime "updated_at",  null: false
+    t.datetime "created_at",                null: false
+    t.datetime "updated_at",                null: false
     t.boolean  "is_actual"
+    t.integer  "EmpGUID"
     t.index ["is_actual"], name: "index_people_on_is_actual", using: :btree
   end