合作伙伴改版

openi_sso
daiao 6 years ago
parent 372db525bd
commit c140b70818

@ -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

@ -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

@ -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

@ -0,0 +1,5 @@
class PartnerCustomer < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :partner
belongs_to :customer
end

@ -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

@ -10,7 +10,7 @@
<span><%= index + 1 %></span>
<span class="task-hide"><%= customer.school.name %></span>
<span>
<a href="javascript:void(0)" onclick="delete_confirm_box_2('<%= delete_customers_managements_path(customer: customer.id) %>',
<a href="javascript:void(0)" onclick="delete_confirm_box_2('<%= delete_customers_managements_path(customer: customer.id, partner_id: @current_partner.try(:id)) %>',
'确定要删除该客户吗')" class="color-grey-9">删除</a>
</span>
<span>

@ -1,14 +1,12 @@
<% @partners.each_with_index do |partner,index| %>
<li class="clearfix">
<%= 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" %>
<div class="fr mr10 edu-menu-panel mt5">
<i class="iconfont icon-sandian color-grey-9 font-14"></i>
<ul class="edu-menuSmall-list edu-txt-center">
<li><a href="javascript:void(0)">查看</a></li>
<li><a href="javascript:void(0)" onclick="delPartners();">删除</a></li>
<li><a href="<%= partners_managements_path(:partner => partner) %>">查看</a></li>
<li><a href="javascript:void(0)" onclick="delPartners('<%= delete_partner_managements_path(:partner_id => partner) %>');">删除</a></li>
</ul>
</div>
<% end %>
</li>
<% end %>

@ -0,0 +1 @@
$("#partner_list").html("<%= j(render :partial => "partner_list") %>")

@ -6,7 +6,7 @@
</div>
<div class="clearfix mt20">
<div class="with25 fl">
<%= link_to "+添加",all_partners_managements_path,remote:true,class:"color-blue addOperation" %>
<%= link_to "+添加", all_partners_managements_path,remote:true,class:"color-blue addOperation" %>
<ul class="partnerList" id="partner_list">
<%= render :partial => "partner_list" %>
</ul>
@ -31,7 +31,7 @@
}
function delPartners(){
delete_confirm_box_2("","确定删除合作伙伴?");
function delPartners(url){
delete_confirm_box_2(url,"确定删除合作伙伴?");
}
</script>

@ -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

@ -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

@ -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

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :partner_customer do
end
end

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe PartnerCustomer, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading…
Cancel
Save