diff --git a/app/assets/javascripts/admins/salesman_channels/index.js b/app/assets/javascripts/admins/salesman_channels/index.js new file mode 100644 index 000000000..cc5109160 --- /dev/null +++ b/app/assets/javascripts/admins/salesman_channels/index.js @@ -0,0 +1,74 @@ +$(document).on('turbolinks:load', function() { + if ($('body.admins-salesman-channels-index-page').length > 0) { + + // ============= 添加销售人员 ============== + var $addMemberModal = $('.admin-add-salesman-channel-user-modal'); + var $addMemberForm = $addMemberModal.find('.admin-add-salesman-channel-user-form'); + var $memberSelect = $addMemberModal.find('.salesman-channel-user-select'); + var $salesmanIdInput = $('.salesman-channel-list-form').find(".btn-primary"); + + $addMemberModal.on('show.bs.modal', function(event){ + var $link = $(event.relatedTarget); + // var salesmanId = $link.data('salesman_id'); + // $salesmanIdInput.val(salesmanId); + + $memberSelect.select2('val', ' '); + }); + + $memberSelect.select2({ + theme: 'bootstrap4', + placeholder: '请输入要添加的单位', + multiple: true, + minimumInputLength: 1, + ajax: { + delay: 500, + url: '/admins/schools', + dataType: 'json', + data: function(params){ + return { keyword: params.term }; + }, + processResults: function(data){ + return { results: data.schools } + } + }, + templateResult: function (item) { + if(!item.id || item.id === '') return ''; + return $("" + item.name + ""); + }, + templateSelection: function(item){ + if (item.id) { + } + return item.name || ''; + } + }); + + $addMemberModal.on('click', '.submit-btn', function(){ + $addMemberForm.find('.error').html(''); + + // var salesmanId = $salesmanIdInput.val(); + var memberIds = $memberSelect.val(); + if (memberIds && memberIds.length > 0) { + $.ajax({ + method: 'POST', + dataType: 'json', + url: '/admins/salesman_channels/batch_add', + data: { salesman_id: $salesmanIdInput.data("salesman-id"), school_ids: memberIds }, + success: function(){ + $.notify({ message: '创建成功' }); + $addMemberModal.modal('hide'); + + setTimeout(function(){ + window.location.reload(); + }, 500); + }, + error: function(res){ + var data = res.responseJSON; + $form.find('.error').html(data.message); + } + }); + } else { + $addMemberModal.modal('hide'); + } + }); + } +}); \ No newline at end of file diff --git a/app/controllers/admins/salesman_channels_controller.rb b/app/controllers/admins/salesman_channels_controller.rb new file mode 100644 index 000000000..2abf02698 --- /dev/null +++ b/app/controllers/admins/salesman_channels_controller.rb @@ -0,0 +1,28 @@ +class Admins::SalesmanChannelsController < Admins::BaseController + before_action :set_salesman + + def index + @channels = SalesmanChannel.all + end + + def batch_add + channel_ids = @salesman.salesman_channels.pluck(:school_id) + school_ids = params[:school_ids] - channel_ids + school_ids.each do |school_id| + school = School.find_by(id: school_id) + next if school.blank? + @salesman.salesman_channels.create!(school_id: school.id) + end + render_ok + end + + def destroy + @salesman.salesman_channels.find_by!(id: params[:id]).destroy + end + + private + + def set_salesman + @salesman = Salesman.find params[:salesman_id] + end +end diff --git a/app/controllers/admins/schools_controller.rb b/app/controllers/admins/schools_controller.rb index 8c4f1d59e..ca1dcb9a3 100644 --- a/app/controllers/admins/schools_controller.rb +++ b/app/controllers/admins/schools_controller.rb @@ -4,7 +4,7 @@ class Admins::SchoolsController < Admins::BaseController params[:sort_direction] ||= 'desc' schools = Admins::SchoolQuery.call(params) - + @total_count = schools.map(&:id).count @schools = paginate schools school_ids = @schools.map(&:id) diff --git a/app/models/salesman_channel.rb b/app/models/salesman_channel.rb index 473cdbba9..b5ae17e0e 100644 --- a/app/models/salesman_channel.rb +++ b/app/models/salesman_channel.rb @@ -1,3 +1,27 @@ class SalesmanChannel < ApplicationRecord belongs_to :salesman, :touch => true, counter_cache: true + belongs_to :school + + def school_name + school.name + end + + def teacher_count + UserExtension.where(school_id: school_id).where.not(identity: 1).count + end + + def student_count + UserExtension.where(school_id: school_id, identity: 1).count + end + + def course_count + Course.where(school_id: school_id).count + end + + def shixuns_count + ShixunMember.joins("join user_extensions on user_extensions.user_id = shixun_members.user_id") + .where(user_extensions: {school_id: school_id}).pluck(:shixun_id).uniq.count + end + + end diff --git a/app/views/admins/salesman_channels/destroy.js.erb b/app/views/admins/salesman_channels/destroy.js.erb new file mode 100644 index 000000000..d00e4fa08 --- /dev/null +++ b/app/views/admins/salesman_channels/destroy.js.erb @@ -0,0 +1,2 @@ +$.notify({ message: '操作成功' },{ type: 'success' }); +$("<%= params[:element]%>").remove(); \ No newline at end of file diff --git a/app/views/admins/salesman_channels/index.html.erb b/app/views/admins/salesman_channels/index.html.erb new file mode 100644 index 000000000..5e15d63eb --- /dev/null +++ b/app/views/admins/salesman_channels/index.html.erb @@ -0,0 +1,15 @@ +<% define_admin_breadcrumbs do %> + <% add_admin_breadcrumb("#{@salesman.name}的渠道", admins_salesmans_path) %> +<% end %> + +
序号 | +名称 | +教师数 | +学生数 | +课堂数 | +实训数 | +操作 | +
---|---|---|---|---|---|---|
<%= index + 1 %> | ++ <%= channel.school_name %> + | ++ <%= channel.teacher_count %> + | ++ <%= channel.student_count %> + | ++ <%= channel.course_count %> + | ++ <%= channel.shixuns_count %> + | ++ <%= delete_link '删除', admins_salesman_channel_path(channel, salesman_id: channel.salesman_id, element: ".salesman-channel-item-#{channel.id}"), class: 'delete-salesman-action' %> + | +