diff --git a/app/assets/javascripts/admins/salesman_channels/index.js b/app/assets/javascripts/admins/salesman_channels/index.js index 9b6659761..6d25688fe 100644 --- a/app/assets/javascripts/admins/salesman_channels/index.js +++ b/app/assets/javascripts/admins/salesman_channels/index.js @@ -3,19 +3,11 @@ $(document).on('turbolinks:load', function() { // ============= 添加销售人员 ============== var $addMemberModal = $('.admin-add-salesman-channel-user-modal'); - var searchscForm = $(".saleman-channel-list-form .search-form"); - var scFormUrl = searchscForm.data('data-search-form-url'); 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"); - var $start_time = $('#start_time'); - var $end_time = $('#end_time'); + var $form = $addMemberModal.find('form.admin-add-salesman-user-form'); $addMemberModal.on('show.bs.modal', function(event){ - var $link = $(event.relatedTarget); - // var salesmanId = $link.data('salesman_id'); - // $salesmanIdInput.val(salesmanId); - $memberSelect.select2('val', ' '); }); @@ -52,12 +44,12 @@ $(document).on('turbolinks:load', function() { // var salesmanId = $salesmanIdInput.val(); var memberIds = $memberSelect.val(); if (memberIds && memberIds.length > 0) { - alert(start_time); + var url = $form.data('url'); $.ajax({ method: 'POST', dataType: 'json', - url: '/admins/salesman_channels/batch_add', - data: { salesman_id: $salesmanIdInput.data("salesman-id"), school_ids: memberIds }, + url: url, + data: $form.serialize(), success: function(){ $.notify({ message: '创建成功' }); $addMemberModal.modal('hide'); @@ -68,7 +60,7 @@ $(document).on('turbolinks:load', function() { }, error: function(res){ var data = res.responseJSON; - $form.find('.error').html(data.message); + $addMemberForm.find('.error').html(data.message); } }); } else { @@ -76,6 +68,16 @@ $(document).on('turbolinks:load', function() { } }); + // 搜索 + var searchscForm = $(".saleman-channel-list-form .search-form"); + + // 清空 + searchscForm.on('click', '.clear-btn', function () { + searchscForm.find('.start_date').val(''); + searchscForm.find('.end_date').val('').trigger('change'); + searchscForm.find('input[name="keyword"]').val(''); + }); + // 时间跨度 var baseOptions = { autoclose: true, @@ -102,8 +104,8 @@ $(document).on('turbolinks:load', function() { }); var submitForm = function(){ + var url = searchscForm.data('search-form-url'); var form = searchscForm; - var url = "/admins/salesman_channels"; $.ajax({ url: url, data: form.serialize(), diff --git a/app/controllers/admins/salesman_channels_controller.rb b/app/controllers/admins/salesman_channels_controller.rb index a1cdcb254..385503d3a 100644 --- a/app/controllers/admins/salesman_channels_controller.rb +++ b/app/controllers/admins/salesman_channels_controller.rb @@ -2,10 +2,12 @@ class Admins::SalesmanChannelsController < Admins::BaseController before_action :set_salesman def index - @channels = SalesmanChannel.all - @start_time = params[:start_time] - @end_time = params[:end_time] || Time.now - @keyword = params[:keyword] + @channels = @salesman.salesman_channels + if params[:keyword].present? + @channels = @channels.joins(:school).where("schools.name like ?", "%#{params[:keyword]}%") + end + @start_time = params[:start_date] + @end_time = params[:end_date].blank? ? Time.now : params[:end_date] end def batch_add @@ -17,6 +19,8 @@ class Admins::SalesmanChannelsController < Admins::BaseController @salesman.salesman_channels.create!(school_id: school.id) end render_ok + rescue Exception => ex + render_error(ex.message) end def destroy diff --git a/app/models/salesman_channel.rb b/app/models/salesman_channel.rb index 0aee5f635..020baf75a 100644 --- a/app/models/salesman_channel.rb +++ b/app/models/salesman_channel.rb @@ -6,22 +6,20 @@ class SalesmanChannel < ApplicationRecord school.name end - def teacher_count(start_time, end_time, keyword) - UserExtension.joins(:school).where("schools.name like '%#{keyword}%' and user_extensions.identity=0 - and user_extensions.created_at between '#{start_time}' and '#{end_time}'").count + def teacher_count(start_time, end_time) + UserExtension.where("identity = 0 and school_id = #{school_id} and created_at between '#{start_time}' and '#{end_time}'").count # UserExtension.where(school_id: school_id).where(query).count end - def student_count(start_time, end_time, keyword) - UserExtension.joins(:school).where("schools.name like '%#{keyword}%' and user_extensions.identity=1 - and user_extensions.created_at between '#{start_time}' and '#{end_time}'").count + def student_count(start_time, end_time) + UserExtension.where("identity = 1 and school_id = #{school_id} and created_at between '#{start_time}' and '#{end_time}'").count end - def course_count(start_time, end_time, keyword) - Course.joins(:school).where("schools.name like '%#{keyword}%' and courses.created_at between '#{start_time}' and '#{end_time}'").count + def course_count(start_time, end_time) + Course.where("school_id = #{school_id} and courses.created_at between '#{start_time}' and '#{end_time}'").count end - def shixuns_count(start_time, end_time, keyword) + def shixuns_count(start_time, end_time) ShixunMember.joins("join user_extensions on user_extensions.user_id = shixun_members.user_id and shixun_members.created_at between '#{start_time}' and '#{end_time}'") .where(user_extensions: {school_id: school_id}).pluck(:shixun_id).uniq.count end diff --git a/app/views/admins/salesman_channels/index.html.erb b/app/views/admins/salesman_channels/index.html.erb index 237ef61d5..75847a542 100644 --- a/app/views/admins/salesman_channels/index.html.erb +++ b/app/views/admins/salesman_channels/index.html.erb @@ -7,13 +7,13 @@ <% end %>