Commit 7d4dbe07 by dongfeng

基本的修改

parent 053dd2fa
PATH
remote: .
specs:
crm_java_search (0.1.3)
GEM
remote: https://rubygems.org/
specs:
rake (10.4.2)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.16)
crm_java_search!
rake (~> 10.0)
BUNDLED WITH
1.16.6
lib = File.expand_path('lib', __dir__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "crm_java_search/version" require 'crm_java_search/version'
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "crm_java_search" spec.name = 'crm_java_search'
spec.version = CrmJavaSearch::VERSION spec.version = CrmJavaSearch::VERSION
spec.authors = ["dongfeng"] spec.authors = ['dongfeng']
spec.email = ["dongfeng@dongfengdeMacBook-Pro.local"] spec.email = ['dongfeng@dongfengdeMacBook-Pro.local']
spec.summary = %q{crm project java search} spec.summary = 'crm project java search'
spec.description = %q{crm project java search} spec.description = 'crm project java search'
spec.homepage = "http://gitlab.ikcrm.com/crm/crm_java_search" spec.homepage = 'http://gitlab.ikcrm.com/crm/crm_java_search'
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host. # to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata) if spec.respond_to?(:metadata)
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'" spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
spec.metadata["homepage_uri"] = spec.homepage spec.metadata['homepage_uri'] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here." spec.metadata['source_code_uri'] = 'http://gitlab.ikcrm.com/crm/crm_java_search'
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." spec.metadata['changelog_uri'] = 'http://gitlab.ikcrm.com/crm/crm_java_search'
else else
raise "RubyGems 2.0 or newer is required to protect against " \ raise 'RubyGems 2.0 or newer is required to protect against ' \
"public gem pushes." 'public gem pushes.'
end end
# Specify which files should be added to the gem when it is released. # Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git. # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end end
spec.bindir = "exe" spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"] spec.require_paths = ['lib']
spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency 'rake', '~> 10.0'
end end
require "crm_java_search/version" require 'crm_java_search/version'
require 'crm_java_search/config'
require 'crm_java_search/app'
require 'crm_java_search/log'
module CrmJavaSearch Dir[File.dirname(__FILE__) + '/crm_java_search/trans/*.rb'].sort.each do |path|
# Your code goes here... filename = File.basename(path)
require "crm_java_search/trans/#{filename}"
end
Dir[File.dirname(__FILE__) + '/crm_java_search/searchers/*.rb'].sort.each do |path|
filename = File.basename(path)
require "crm_java_search/searchers/#{filename}"
end
Dir[File.dirname(__FILE__) + '/crm_java_search/checkers/*.rb'].sort.each do |path|
filename = File.basename(path)
require "crm_java_search/checkers/#{filename}"
end
Dir[File.dirname(__FILE__) + '/crm_java_search/agents/*.rb'].sort.each do |path|
filename = File.basename(path)
p filename
require "crm_java_search/agents/#{filename}"
end end
module CrmJavaSearch
module Agents
class BaseAgent
attr_reader :organization_id, :entity_class_name, :record_class_name, :record_id
def initialize(organization_id:, entity_class_name:, record_class_name:, record_id:)
@organization_id = organization_id
@entity_class_name = entity_class_name
@record_class_name = record_class_name
@record_id = record_id
end
def save
Trans::HttpHandler.post_form(url, id: record_id)
self
end
def destroy
Trans::HttpHandler.post_form(url, id: record_id, delete: true)
self
end
class << self
def create(organization_id:, entity_class_name:, record_class_name:, record_id:)
object = new(
organization_id: organization_id,
entity_class_name: entity_class_name,
record_class_name: record_class_name,
record_id: record_id
)
object.save
end
def repeat_search(entity_class_name, organization_id, params)
repeat_search_url = Trans::Router.repeat_search_url(entity_class_name, organization_id)
Trans::HttpHandler.post_json(repeat_search_url, params)
end
def repeat_check(entity_class_name, organization_id, params)
repeat_check_url = Trans::Router.repeat_check_url(entity_class_name, organization_id)
Trans::HttpHandler.post_json(repeat_check_url, params)
end
end
end
end
end
module CrmJavaSearch
module Agents
class ContactAgent < BaseAgent
def url
Trans::Router.sync_url(:contact)
end
end
end
end
module CrmJavaSearch
module Agents
class ContractAgent < BaseAgent
def url
Trans::Router.sync_url(:contract)
end
end
end
end
module CrmJavaSearch
module Agents
class CustomFieldAgent < BaseAgent
def url
Trans::Router.mapping_url(entity_class_name.to_s.downcase)
end
end
end
end
module CrmJavaSearch
module Agents
class CustomerAgent < BaseAgent
def url
Trans::Router.sync_url(:customer)
end
end
end
end
module CrmJavaSearch
module Agents
class LeadAgent < BaseAgent
def url
Trans::Router.sync_url(:lead)
end
end
end
end
module CrmJavaSearch
class App
def self.server
Config.settings
end
def self.use_java_es?
!server.empty? && !server[:host].to_s.empty? && server[:enabled] == true
end
def self.api_base_url
if use_java_es?
server[:host]
else
''
end
end
# 查重搜索
def self.search_enabled?
use_java_es? && server[:search_enabled] == true
end
# 查重校验(新增客户)
def self.check_enabled?
use_java_es? && server[:check_enabled] == true
end
# 同步数据到es
def self.sync_enabled?
use_java_es? && server[:sync_enabled] == true
end
end
end
module CrmJavaSearch
module Checkers
class ApiCheckService
attr_accessor :user, :organization, :record, :exclude_ids, :id_source, :klass
def initialize(user: , record: , exclude_ids: [], id_source: nil)
@user = user
@record = record
@organization = user.organization
@exclude_ids = exclude_ids
@id_source = id_source
@klass = record.class.name
end
def check_duplicate_custom_fields(tail = false, &_proc)
return unless organization.duplicate_check_enabled?(key: @klass.underscore)
organization.send("#{@klass.underscore}_duplicate_custom_fields").each do |custom_field|
raw_field_value = custom_field.raw_field_value_from(record)
field = organization.send("#{@klass.underscore}_duplicate_field_custom_field_map").invert[custom_field.name]
next if field.blank? || raw_field_value.blank?
dup_params = {field: field, field_value: raw_field_value, exclude_ids: exclude_ids, id_source: id_source }.stringify_keys
checker = "CrmJavaSearch::Checkers::#{@klass}Checker".constantize.new(user: user, entity: record, params: dup_params)
checker.check
if ['1', '2'].include?(checker.result[:code])
message = checker.result[:remark]
message += "-#{raw_field_value}" if tail
_proc.call(custom_field) if block_given?
return raise EntityDuplicateError.new(message)
end
end
end
end
end
end
module CrmJavaSearch
module Checkers
class BaseChecker
def initialize(user:, entity:, params:)
params = params.with_indifferent_access
@user = user
@entity = entity
@organization = user.organization
@field = params["field"]
@field_value = params["field_value"]
@exclude_ids = params["exclude_ids"].compact || []
@exclude_ids.push(@entity.id) if @entity && @entity.id
@logger_helper = LoggerHelper.new(@organization)
@id_source = params["id_source"] || ''
end
def es_check(klass)
@duplicated = false
@model_human_name = @organization.feature_label(klass.name)
@custom_field_name = @organization.send("#{klass.name.underscore}_duplicate_field_custom_field_map")[@field]
@custom_field = @organization.send("#{klass.name.underscore}_duplicate_custom_fields").
find{ |custom_field|custom_field.name == @custom_field_name }
return field_not_found if @custom_field.blank?
@field_label = @custom_field.label
@cross_fields = get_cross_fields
unless (@organization.duplicate_check_enabled?(key: klass.name.underscore) &&
@organization.send("should_#{klass.name.underscore}_check_duplicate_for?", @field))
return field_check_disabled
end
check_service(klass)
end
def check_service(klass)
es_query = {@custom_field.name => @field_value, "exclude_ids" => @exclude_ids, "cross_fields" => @cross_fields, "id_source" => @id_source}
@logger_helper.info("microservices es request params es_query: #{es_query} ")
@res = CrmJavaSearch::Agents.const_get("#{klass.name}Agent").repeat_check(@organization.id, es_query)
@logger_helper.info("microservices es response #{@res}")
return unknown_error unless [0, 500, 502].include? @res['code'].to_i
return system_error if @res['code'].to_i == 500
return parameter_error if @res['code'].to_i == 502
if @res["data"]['repeat'] && @res["data"]['entities'].present?
@res["data"]['entities'].select{ |entity_hash|
return duplicate_in_owns(entity_hash['cross_fields']) if entity_hash['user_id'].to_i == @user.id
duplicate_in_org(entity_hash['cross_fields'])
}
else
no_duplicates
end
end
def cross_field_from_setting
RelationDuplicate::RelationService.new(@organization).send("#{@entity.class.name.underscore}_cross")[@custom_field.name]
end
def get_cross_fields
base_field = {@entity.class.name.underscore => @custom_field.name}
if cross_field_from_setting
cross_field_from_setting.merge(base_field).with_indifferent_access
else
base_field.with_indifferent_access
end
end
def field_check_disabled
@code = "0"
@remark = "#{@model_human_name}模块下#{@field_label}字段未开启查重"
end
def field_not_found
@code = "0"
@remark = "#{@model_human_name}模块下未找到查重字段#{@field}对应的业务字段"
end
def no_duplicates
@code = "0"
@remark = "未发现相同#{@field_label}#{@model_human_name}"
end
def duplicate_in_owns(cross_field=nil)
@code = "1"
if cross_field.present?
@remark = "你已经有" + cross_fields_duplicate(cross_field)
else
@remark = "你已经有相同#{@field_label}#{@model_human_name}"
@duplicated = true
end
end
def duplicate_in_org(cross_field=nil)
@code = "2"
if cross_field.present?
@remark = "公司内已有" + cross_fields_duplicate(cross_field)
else
@remark = "公司内已有相同#{@field_label}#{@model_human_name}"
@duplicated = true
end
end
def system_error
@duplicated = true
@code = "3"
@remark = "#{@model_human_name}查重系统异常"
end
def parameter_error
@duplicated = true
@code = "4"
@remark = "#{@model_human_name}查重参数错误"
end
def unknown_error
@duplicated = true
@code = "5"
@remark = "#{@model_human_name}查重为定义错误(code!=0)"
end
def cross_fields_duplicate(cross_field)
@duplicated = true
labels = if @entity.present? && cross_field.keys.include?(@entity.class.name.underscore)
{ 'klass_to_show' => @entity.class.name,
'klass_model_label' => @organization.feature_label(@entity.class.name) }
else
{ 'klass_to_show' => cross_field.keys.first,
'klass_model_label' => @organization.feature_label(cross_field.keys.first.classify) }
end
if labels['klass_to_show'] == 'customer' && @custom_field_name == 'company_name'
customer_field_label = @organization.customer_duplicate_custom_fields.detect{ |c| c.name == 'name' }.try(:label)
"相同#{ customer_field_label }#{ labels['klass_model_label'] }"
elsif labels['klass_to_show'] == 'lead' && @custom_field_name == 'name'
lead_field_label = @organization.lead_duplicate_custom_fields.detect{ |c| c.name == 'company_name'}.try(:label)
"相同#{ lead_field_label }#{ labels['klass_model_label'] }"
else
"相同#{ @field_label }#{ labels['klass_model_label'] }"
end
end
def duplicated?
!!@duplicated
end
def result
{ code: @code, remark: @remark }
end
def remark
@remark
end
end
end
end
module CrmJavaSearch
module Checkers
class ContactChecker < BaseChecker
def check
es_check(Contact)
end
end
end
end
module CrmJavaSearch
module Checkers
class ContractChecker < BaseChecker
def check
es_check(Contract)
end
end
end
end
module CrmJavaSearch
module Checkers
class CustomerChecker < BaseChecker
def check
es_check(Customer)
end
end
end
end
module CrmJavaSearch
module Checkers
class LeadChecker < BaseChecker
def check
es_check(Lead)
end
end
end
end
module CrmJavaSearch
module Config
class << self
def init(opts = {})
@settings = opts || default_settings
end
def default_settings
{
enabled: true,
search_enabled: true,
check_enabled: true,
sync_enabled: true,
host: 'http://dev-pc-search.ikcrm.com'
}
end
def settings
@settings ||= default_settings
end
end
end
end
require 'logger'
module CrmJavaSearch
module Log
class << self
attr_writer :logger
def logger
@logger ||= Logger.new(STDERR)
end
end
end
end
module CrmJavaSearch
module Searchers
class BaseSearcher
def initialize(query:, keys:, organization:)
@organization = organization
@es_query = {
value: query,
fields: keys,
page: {
pageNum: 1,
pageSize: 21
}
}
end
end
end
end
module CrmJavaSearch
module Searchers
class ContactSearcher < BaseSearcher
def search
data = Agents::ContactAgent.repeat_search(:contact, @organization.id, @es_query)["data"]
if data
Contact.where(organization_id: @organization.id).where(id: data.map{|datai| datai['id']})
else
Contact.none
end
end
end
end
end
module CrmJavaSearch
module Searchers
class ContractSearcher < BaseSearcher
def search
data = Agents::ContractAgent.repeat_search(:contract, @organization.id, @es_query)["data"]
if data
Contract.where(organization_id: @organization.id).where(id: data.map{|datai| datai['id']})
else
Contract.none
end
end
end
end
end
module CrmJavaSearch
module Searchers
class CustomerSearcher < BaseSearcher
def search
data = Agents::CustomerAgent.repeat_search(:customer, @organization.id, @es_query)["data"]
if data
Customer.where(organization_id: @organization.id).where(id: data.map{|datai| datai['id']})
else
Customer.none
end
end
end
end
end
module CrmJavaSearch
module Searchers
class LeadSearcher < BaseSearcher
def search
repeat_search_data = Agents::LeadAgent.repeat_search(:lead, @organization.id, @es_query)
Log.logger.info("repeat_search_data: #{repeat_search_data}")
if repeat_search_data['data']
Lead.where(organization_id: @organization.id).where(id: data.map{|datai| datai['id']})
else
Lead.none
end
end
end
end
end
require 'json'
module CrmJavaSearch
module Trans
module HttpHandler
extend self
DEFAULT_TIME_OUT = 20
def wrapp_loger(url, params)
Log.logger.info("Request java es service api url: #{url} params: #{params}")
start_time = Time.now.getlocal
res = yield(url, params) if block_given?
end_time = Time.now.getlocal
Log.logger.info("start_time: #{start_time} end_time: #{end_time}")
Log.logger.info("ESRequest Time: #{end_time - start_time} ")
res
end
def post_json(url, params)
wrapp_loger(url, params) do
HTTParty.post url,
body: params.to_json,
headers: {
'Accept' => 'application/json;',
'Content-Type' => 'application/json; charset=utf8;'
},
timeout: DEFAULT_TIME_OUT
end
end
def post_form(url, params)
wrapp_loger(url, params) do
HTTParty.post "#{url}?#{params.to_query}",
headers: {
'Accept' => 'application/json;'
},
timeout: DEFAULT_TIME_OUT
end
end
end
end
end
module CrmJavaSearch
module Trans
module Router
class << self
def repeat_search_url(entity_class_name, organization_id)
base_url + "/#{entity_class_name}/#{organization_id}/repeat/search"
end
# 新增 查重校验
def repeat_check_url(entity_class_name, organization_id)
base_url + "/#{entity_class_name}/#{organization_id}/add/check"
end
def mapping_url(entity_class_name)
base_url + "/#{entity_class_name}/mapping"
end
def sync_url(entity_class_name)
base_url + "/#{entity_class_name}/sync"
end
def base_url
CrmJavaSearch::App.api_base_url
end
end
end
end
end
module CrmJavaSearch module CrmJavaSearch
VERSION = "0.1.0" VERSION = "0.1.3"
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment