# frozen_string_literal: true module EducodeSales # 商机交付课程 class BusinessCoursesController < ApplicationController include BusinessCoursesHelper include SubjectHelper before_action :subject_members, :subject_url before_action :have_permission?, only: %i(index new create edit) # 查看cancancan Gem优化代码 def have_permission? have_permission = can?(:business, EducodeSales::BusinessDeliverSubject) || can?(:all_business, EducodeSales::BusinessDeliverSubject) have_permission || @current_admin.is_admin ? "" : authorize!(:business, BusinessDeliverSubject) end def index respond_to do |format| format.html do end format.json do @business_deliver = BusinessDeliverSubject.all unless is_commissioner_above? business_ids = @business_deliver.pluck(:business_id) last_follow_staff_business = Business.where(id: business_ids).joins(last_follow_up: [assign_follow_ups: [staff: :user]]).where("CONCAT(users.lastname, users.firstname) = '#{@current_admin.user.real_name}' ") last_follow_staff_business_ids = last_follow_staff_business.pluck("educode_sales_businesses.id") staff_business = Business.where(id: business_ids - last_follow_staff_business_ids).joins(staff: :user).where("CONCAT(users.lastname, users.firstname) = '#{@current_admin.user.real_name}' ") @business_deliver = BusinessDeliverSubject.where(business_id: last_follow_staff_business_ids + staff_business.ids) end @business_deliver = @business_deliver.joins(business: [:school, :staff, :last_follow_up]) # 查询条件 交付状态 state == 0 代表全部 if params[:q] && params[:q][:state].present? && params[:q][:state] != '0' @business_deliver = @business_deliver.where("educode_sales_business_deliver_subjects.status = :status", status: params[:q][:state]) end # 查询条件 商机id if params[:q] && params[:q][:name].present? @business_deliver = @business_deliver.where("educode_sales_businesses.name like '%#{params[:q][:name]}%'") end # 查询条件 时间 if params[:q] && params[:q][:time].present? start_time = params[:q][:time].split(" - ")[0] end_time = params[:q][:time].split(" - ")[-1] @business_deliver = @business_deliver.where("educode_sales_follow_ups.reception_at BETWEEN '#{start_time}' AND '#{end_time}' ") end # 分页查询 @business_deliver = @business_deliver.includes(business: [:school, :staff], business_subjects: :subject) \ .page(params[:page]) .per(params[:limit]) end end end def edit if params[:id] # 查询商机信息 和交付状态等 @item = BusinessDeliverSubject.joins(:business) .select("educode_sales_businesses.name, educode_sales_business_deliver_subjects.status, educode_sales_business_deliver_subjects.id") .find(params[:id]) # 查询课程是否已经有实践课程 @subjects = @item.business_subjects.pluck(:subject_id) @subjects = Subject.where(id: @subjects).map { |item| { value: item[:id], name: item[:name] } } gon.staffs = @manages @value_list = BusinessSubjectStaff.where(container_type: BusinessSubjectStaff::CONTAINER_TYPES::BUSSINESS, container_id: params[:id]) .select(:staff_id) .map { |item| item[:staff_id] } render layout: false end end def update # 开启事务 ActiveRecord::Base.transaction do @business = BusinessDeliverSubject.find_by(id: params[:id]) # 修改状态 if !@business.blank? @business.update(params.require(:business).permit(:status)) old_subject_ids = @business.business_subjects.pluck(:subject_id) new_subject_ids = params[:business][:subject_ids].split(',').map(&:to_i) del_arr = old_subject_ids - new_subject_ids # 需要新增的科目id add_arr = new_subject_ids - old_subject_ids @business.business_subjects.where(subject_id: del_arr).update_all(business_id: nil, business_deliver_subject_id: nil) @business.add_subjects(add_arr) @business.manages.destroy_all BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d| params[:business][:manage_id].split(",").each do |m| d.add [m.to_i, @business.id, BusinessSubjectStaff::CONTAINER_TYPES::BUSSINESS, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::BUSINESS_MANAGE] end end render_success else render_failure("操作失败") end end end def new staffs = Staff.where.not(role_id: 11).includes(:user) @businesses = Business # 要求只能是已签单的商机 gon.stage_id = Common.where(clazz: '商机阶段', name: ['已签单']).pluck(:id)[0] gon.staffs = @manages render layout: false end def create # 开启事务 ActiveRecord::Base.transaction do item = BusinessDeliverSubject.find_by(business_id: params[:business][:business_id]) # 判断是否存在 不存在进入下一步 if item.blank? add_item = BusinessDeliverSubject.new(business_params) add_item.add_subjects(params[:business][:subject_ids].split(",")) if add_item.save! && !params[:business][:subject_ids].blank? BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d| params[:business][:manage_id].split(",").each do |m| d.add [m.to_i, add_item.id, BusinessSubjectStaff::CONTAINER_TYPES::BUSSINESS, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::BUSINESS_MANAGE] end end render_success else render_failure("操作失败") end end end def select_business respond_to do |format| format.json do stage_id = Common.where(clazz: '商机阶段', name: ['已签单']).take.id.to_i @businesses = Business.joins(:last_follow_up, :school) .where("educode_sales_businesses.name LIKE '%#{params[:q]}%' and educode_sales_follow_ups.stage_id = #{stage_id} ") .where("educode_sales_businesses.id not in (?) ", BusinessDeliverSubject.pluck(:business_id).map(&:to_i)) # todo 数组中包含nil会导致查询失败,无法找到数据 common = Common.find_by(clazz: 'staff_type', name: '销售') @staffs = Staff.find(current_user.id) if @staffs.present? && common.id.to_i == @staffs.job_type.to_i @businesses = @businesses.where(staff_id:current_user.id) end @count = @businesses.count("educode_sales_businesses.id") @businesses = @businesses.select("educode_sales_businesses.*, schools.name as s_name, schools.id as s_id") .page(params[:page]).per(10) render json: { data: @businesses, code: 0 } end end end def list_shixuns respond_to do |format| format.html do render layout: false end format.json do shixuns = BusinessSubjectShixun if params[:id].present? business_deliver = BusinessDeliverSubject.find_by(id: params[:id]) shixuns = BusinessSubjectShixun.joins(:shixun, business_subject: [:business_deliver_subject, :subject]).left_joins(:last_dectect).where("educode_sales_business_deliver_subjects.id = #{business_deliver.id }") elsif params[:subject_id].present? shixuns = BusinessSubjectShixun.joins(:shixun, business_subject: [:business_deliver_subject, :subject]).left_joins(:last_dectect).where("educode_sales_business_subjects.id = #{params[:subject_id] }") end if params[:q].present? && params[:q][:shixun_name].present? shixuns = shixuns.where("shixuns.name like '%#{params[:q][:shixun_name]}%'") end if params[:q].present? && params[:q][:subject_name].present? shixuns = shixuns.where("subjects.name like '%#{params[:q][:subject_name]}%'") end shixuns = shixuns.search_category(params[:q][:category]) if params[:q].present? shixuns = shixuns.search_status(params[:q][:status]) if params[:q].present? shixuns = shixuns.search_level(params[:q][:level]) if params[:q].present? @count = shixuns.count @shixuns = shixuns.select("educode_sales_business_subject_shixuns.*, shixuns.name s_name, shixuns.identifier s_identifier, subjects.name as subjects_name,subjects.identifier as subjects_identifier, educode_sales_shixun_dectects.content dectect_content, educode_sales_shixun_dectects.date dectect_time ") .page(params[:page]).per(params[:limit]) end end end def list_subjects respond_to do |format| format.html do render layout: false end format.json do # 商机交付课程id bussiness_deliver_id = params[:id].to_i # subject.status => :0 编辑中 1 审核中 2 发布 # public: 2: 公开 subjects = BusinessSubject.joins("INNER JOIN subjects bs ON bs.id = educode_sales_business_subjects.subject_id INNER JOIN educode_sales_business_deliver_subjects e_s_b_d_s ON educode_sales_business_subjects.business_deliver_subject_id = e_s_b_d_s.id and e_s_b_d_s.id = #{bussiness_deliver_id} INNER JOIN educode_sales_businesses esb ON esb.id = educode_sales_business_subjects.business_id LEFT JOIN educode_sales_follow_ups fu ON fu.id = esb.last_follow_up_id ") # 搜索条件 if params[:q].present? && params[:q][:name].present? subjects = subjects.where("bs.name like '%#{params[:q][:name]}%'") end if params[:q].present? && params[:q][:p_state].present? status = params[:q][:p_state].to_i if status == 1 subjects = subjects.where("bs.public = #{status}") elsif [0, 2].include? status subjects = subjects.where("bs.status = #{status} and public not in (1,2) ") else subjects = subjects.where("bs.public = 2") end end @subjects = subjects.includes(business_subject_shixuns: :shixun).select("educode_sales_business_subjects.*, bs.identifier, bs.status status, bs.name name, bs.public, IFNULL(fu.reception_at, '--') reception_at").page(params[:page]).per(params[:limit]) end end end def subjects respond_to do |format| format.json do @subjects = Subject.unhidden.where.not(id: BusinessSubject.joins(:business).pluck(:subject_id)).where("name like '%#{params[:q]}%' ").page(params[:page]).per(10) render json: { data: @subjects.map { |item| { value: item[:id], name: item[:name] } }, code: 0, count: @subjects.total_count / 10 } end end end def business_params params.require(:business).permit(:status, :business_id) end end end