diff --git a/app/controllers/managements_controller.rb b/app/controllers/managements_controller.rb index b7e1bd56..1c454424 100644 --- a/app/controllers/managements_controller.rb +++ b/app/controllers/managements_controller.rb @@ -1191,7 +1191,7 @@ end if params[:search].blank? @partners = Partner.includes(:school).order("partners.created_at desc") else - @partners = Partner.where("name like ? ", "%#{params[:search]}%").includes(:school).order("partners.created_at desc") + @partners = Partner.includes(:school).where("schools.name like ? ", "%#{params[:search]}%").order("partners.created_at desc") end @current_partner = nil @@ -1212,6 +1212,14 @@ end end + def delete_partner + partner = Partner.find params[:partner_id] + partner.destroy + @partners = Partner.includes(:school).order("partners.created_at desc") + @current_partner = nil + end + + # 添加客户 def customers_list @search = params[:search] @@ -1219,11 +1227,10 @@ end partner_id = params[:partner_id] @partner = Partner.find partner_id @customers = @partner.customers - if @customers.present? - @schools = School.where("(partner_id != ? or partner_id is NULL) and customer_id is NULL", @partner.id) - else - @schools = School.where("customer_id is null and (partner_id != ? or partner_id is NULL)", @partner.id) - end + existed_school_ids = @customers.pluck(:school_id) + existed_school_ids = existed_school_ids.present? ? existed_school_ids.join(",") : -1 + @schools = School.where("id not in (#{existed_school_ids})") + if params[:search] @schools = @schools.where("name like ?", "%#{@search}%") end @@ -1247,13 +1254,14 @@ end def add_customers school_ids = params[:school_ids] - if school_ids.length > 0 + partner_id = params[:partner_id] + if school_ids.length > 0 && partner_id.present? school_ids.each do |s| school = School.where("id = ?",s).first if school.present? - customer = Customer.new(partner_id: params[:partner_id]) + customer = Customer.new(school_id: s) customer.save! - school.update_attributes(:customer_id => customer.id) + PartnerCustomer.create(partner_id: partner_id,customer_id: customer.id ) end end render :json => {status: 1, message: "创建成功!"} @@ -1262,17 +1270,20 @@ end def delete_customers if params[:customer] - customer = Customer.where(id: params[:customer]).first - @current_partner = customer.partner - customer.school.update_attributes(:customer_id => nil) + customer = Customer.find(params[:customer]) + @current_partner = Partner.find(params[:partner_id]) customer.destroy end end + # 添加合作伙伴弹框数据 def all_partners @search = params[:search] @province = params[:province] - @schools = School.where("partner_id IS NULL") + # 已经选过的合作伙伴不能再再列表中显示 + used_school_ids = Partner.pluck(:school_id) + used_school_ids = used_school_ids.blank? ? -1 : used_school_ids.join(",") + @schools = School.where("id not in (#{used_school_ids})") if params[:search] @schools = @schools.where("name like ?", "%#{@search}%") @@ -1296,26 +1307,20 @@ end end end - def add_partner school_ids = params[:school_ids] if school_ids.length > 0 school_ids.each do |s| - school = School.where("id = ?",s).first - if school.present? && school.partner_id.nil? - partner = Partner.new(name: school.name) + old_partner = Partner.where(:school_id => s) + if old_partner.blank? + partner = Partner.new(school_id: s) partner.save - school.update_attributes(:partner_id => partner.id) end end end render :json => {status: 1, message: "创建成功!"} end - - - - # 删除部门管理员 def delete_depart_member DepartmentMember.where(:department_id => params[:depart], :user_id => params[:user_id]).destroy_all diff --git a/app/models/customer.rb b/app/models/customer.rb index 673acd20..14b4d710 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,6 +1,7 @@ class Customer < ActiveRecord::Base default_scope :order => 'customers.created_at desc' - belongs_to :partner - has_one :school + has_many :partners, :through => :partner_customers + has_many :partner_customers, :dependent => :destroy + belongs_to :school has_many :users end diff --git a/app/models/partner.rb b/app/models/partner.rb index 217bc1a7..31920e53 100644 --- a/app/models/partner.rb +++ b/app/models/partner.rb @@ -2,7 +2,8 @@ class Partner < ActiveRecord::Base # attr_accessible :name, :active attr_accessor :active - has_one :school - has_many :customers + belongs_to :school + has_many :customers, :through => :partner_customers + has_many :partner_customers, :dependent => :destroy has_many :users end diff --git a/app/models/partner_customer.rb b/app/models/partner_customer.rb new file mode 100644 index 00000000..c9894cc9 --- /dev/null +++ b/app/models/partner_customer.rb @@ -0,0 +1,5 @@ +class PartnerCustomer < ActiveRecord::Base + # attr_accessible :title, :body + belongs_to :partner + belongs_to :customer +end diff --git a/app/models/school.rb b/app/models/school.rb index 8d377e97..021048fb 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -16,8 +16,8 @@ class School < ActiveRecord::Base has_many :ec_majors, :through => :ec_major_schools has_many :ec_major_schools, :dependent => :destroy - belongs_to :partner - belongs_to :customer + has_many :partners, :dependent => :destroy + has_many :customers, :dependent => :destroy # banner图片信息 has_many :school_images, :dependent => :destroy diff --git a/app/views/managements/_partner_customer_list.html.erb b/app/views/managements/_partner_customer_list.html.erb index e8f643fd..bbdb2ea8 100644 --- a/app/views/managements/_partner_customer_list.html.erb +++ b/app/views/managements/_partner_customer_list.html.erb @@ -10,7 +10,7 @@ <%= index + 1 %> <%= customer.school.name %> - 删除 diff --git a/app/views/managements/_partner_list.html.erb b/app/views/managements/_partner_list.html.erb index 0ee0a60f..cddf4e9d 100644 --- a/app/views/managements/_partner_list.html.erb +++ b/app/views/managements/_partner_list.html.erb @@ -1,14 +1,12 @@ <% @partners.each_with_index do |partner,index| %>
  • - <%= link_to "#{partner.name}", partners_managements_path(:partner => partner), :class => "#{partner.id == @current_partner.id ? 'active' : ''} fl" %> - <% if false %> + <%= link_to "#{partner.school.name}", partners_managements_path(:partner => partner), :class => "#{@current_partner && partner.id == @current_partner.id ? 'active' : ''} fl" %> - <% end %>
  • <% end %> diff --git a/app/views/managements/delete_partner.js.erb b/app/views/managements/delete_partner.js.erb new file mode 100644 index 00000000..276ae32b --- /dev/null +++ b/app/views/managements/delete_partner.js.erb @@ -0,0 +1 @@ +$("#partner_list").html("<%= j(render :partial => "partner_list") %>") \ No newline at end of file diff --git a/app/views/managements/partners.html.erb b/app/views/managements/partners.html.erb index 153ab505..06ac63b0 100644 --- a/app/views/managements/partners.html.erb +++ b/app/views/managements/partners.html.erb @@ -6,7 +6,7 @@
    - <%= link_to "+添加",all_partners_managements_path,remote:true,class:"color-blue addOperation" %> + <%= link_to "+添加", all_partners_managements_path,remote:true,class:"color-blue addOperation" %>
      <%= render :partial => "partner_list" %>
    @@ -31,7 +31,7 @@ } - function delPartners(){ - delete_confirm_box_2("","确定删除合作伙伴?"); + function delPartners(url){ + delete_confirm_box_2(url,"确定删除合作伙伴?"); } \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9c70df82..73cbadc6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -719,6 +719,7 @@ RedmineApp::Application.routes.draw do ## oauth相关 post 'update_level_for_subject' post :add_customers delete :delete_customers + delete :delete_partner get :customers_list get :school_report, controller: 'managements::schools', action: 'statistics' end diff --git a/db/migrate/20190425072918_create_partner_customers.rb b/db/migrate/20190425072918_create_partner_customers.rb new file mode 100644 index 00000000..69d13714 --- /dev/null +++ b/db/migrate/20190425072918_create_partner_customers.rb @@ -0,0 +1,9 @@ +class CreatePartnerCustomers < ActiveRecord::Migration + def change + create_table :partner_customers do |t| + t.references :partner + t.references :customer + t.timestamps + end + end +end diff --git a/db/migrate/20190425073328_modify_partner_and_customer.rb b/db/migrate/20190425073328_modify_partner_and_customer.rb new file mode 100644 index 00000000..c4f7b97a --- /dev/null +++ b/db/migrate/20190425073328_modify_partner_and_customer.rb @@ -0,0 +1,29 @@ +class ModifyPartnerAndCustomer < ActiveRecord::Migration + def up + add_column :customers, :school_id, :integer + add_column :partners, :school_id, :integer + + schools = School.where("customer_id is not null or partner_id is not null") + + schools.each do |school| + if school.customer_id + customer = Customer.find_by_id(school.customer_id) + customer.update_column(:school_id, school.id) if customer + end + if school.partner_id + partner = Partner.find_by_id(school.partner_id) + partner.update_column(:school_id, school.id) if partner + end + end + + # 迁移关联关系 + customers = Customer.where(nil) + customers.each do |customer| + PartnerCustomer.create(partner_id: customer.partner_id, customer_id: customer.id) + end + + end + + def down + end +end diff --git a/spec/factories/partner_customers.rb b/spec/factories/partner_customers.rb new file mode 100644 index 00000000..6327b7ca --- /dev/null +++ b/spec/factories/partner_customers.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :partner_customer do + + end +end diff --git a/spec/models/partner_customer_spec.rb b/spec/models/partner_customer_spec.rb new file mode 100644 index 00000000..6dc614cf --- /dev/null +++ b/spec/models/partner_customer_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe PartnerCustomer, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end