update for order process
Add activereocrd-import gem for bulk insert
This commit is contained in:
@@ -9,13 +9,22 @@ class SeedGenerator < ApplicationRecord
|
||||
|
||||
cur_val, next_val = self.update_seed(model_name)
|
||||
|
||||
if (cur_val == 0)
|
||||
cur_val, next_val = self.execute_query(model_name)
|
||||
padding_len = 15 - prefix.length
|
||||
saleOrderId = prefix +"-"+ cur_val.to_s.to_s.rjust(padding_len,'0')
|
||||
return saleOrderId
|
||||
end
|
||||
|
||||
def self.generate_ids(model, prefix, count = 1)
|
||||
model_name = self.get_model_name(model)
|
||||
|
||||
if ENV["SERVER_MODE"] == 'cloud'
|
||||
prefix = "C#{prefix}"
|
||||
end
|
||||
|
||||
padding_len = 15 - prefix.length
|
||||
saleOrderId = prefix +"-"+ cur_val.to_s.to_s.rjust((14-prefix.length)+1,'0')
|
||||
return saleOrderId
|
||||
start = self.update_seed(model_name, count)
|
||||
stop = start + count - 1
|
||||
length = 15 - prefix.length
|
||||
(start..stop).map { |c| "#{prefix}-#{c.to_s.rjust(length, '0')}" }
|
||||
end
|
||||
|
||||
def self.sync_seed_generator_records(seed_generators)
|
||||
@@ -58,7 +67,7 @@ class SeedGenerator < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
return new_receipt_no
|
||||
return new_receipt_no
|
||||
end
|
||||
|
||||
# Generate for 4 digit Code
|
||||
@@ -85,7 +94,7 @@ class SeedGenerator < ApplicationRecord
|
||||
# padding_len = 6 - prefix.length
|
||||
# count = 5-prefix.length
|
||||
# end
|
||||
# next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0')
|
||||
# next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0')
|
||||
# return next_code
|
||||
# end
|
||||
|
||||
@@ -100,41 +109,13 @@ class SeedGenerator < ApplicationRecord
|
||||
return model_name
|
||||
end
|
||||
|
||||
def self.execute_query(model)
|
||||
current = 0
|
||||
nex = 0
|
||||
|
||||
sql = "INSERT INTO seed_generators (model, created_at, updated_at)
|
||||
VALUES('#{ model }', NOW(), NOW())
|
||||
ON DUPLICATE KEY UPDATE current = current + 1, next = next + 1;"
|
||||
|
||||
select_sql = "select * from seed_generators where model='#{model}';"
|
||||
ActiveRecord::Base.connection.execute(sql);
|
||||
select_result = ActiveRecord::Base.connection.execute(select_sql);
|
||||
|
||||
select_result.each do |row|
|
||||
current = row [3]
|
||||
nex = row[4]
|
||||
def self.update_seed(model, count = 1)
|
||||
SeedGenerator.transaction do
|
||||
seed = SeedGenerator.lock.find_by_model(model)
|
||||
seed.next = seed.next + (count * seed.increase_by)
|
||||
seed.current = seed.next - seed.increase_by
|
||||
seed.save!
|
||||
seed.next_before_last_save
|
||||
end
|
||||
|
||||
return current, nex
|
||||
end
|
||||
|
||||
def self.update_seed(model)
|
||||
current = 0
|
||||
nex = 0
|
||||
|
||||
update_sql = "UPDATE seed_generators set current = next, next = next + 1 WHERE model='#{model}';"
|
||||
select_sql = "select * from seed_generators where model='#{model}';"
|
||||
update_result = ActiveRecord::Base.connection.execute(update_sql);
|
||||
|
||||
select_result = ActiveRecord::Base.connection.execute(select_sql);
|
||||
|
||||
select_result.each do |row|
|
||||
current = row [3]
|
||||
nex = row[4]
|
||||
end
|
||||
|
||||
return current, nex
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user