# frozen_string_literal: true module EducodeSales include SubjectHelper # 实践课程管理 class SubjectsController < ApplicationController before_action :subject_members, :subject_url before_action :have_permission?, only: %i(index new create edit) # 查看cancancan Gem优化代码 def have_permission? have_permission = can?(:subject, EducodeSales::BusinessDeliverSubject) || can?(:all_subject, EducodeSales::BusinessDeliverSubject) have_permission || @current_admin.is_admin ? "" : authorize!(:subject, BusinessDeliverSubject) end def index respond_to do |format| format.html do end format.json do if is_commissioner_above?("subject") business_subject = BusinessSubject else deliver_staffs = BusinessSubject.joins(business_deliver_subject: [manages: :staff]) business_subject_ids = deliver_staffs.where("educode_sales_staffs.id = #{@current_admin.id} ").ids subject_staffs = BusinessSubject.where(id: BusinessSubject.ids - deliver_staffs.ids).joins(manages: :staff).where("educode_sales_staffs.id = #{@current_admin.id} ") business_subject = BusinessSubject.where(id: business_subject_ids + subject_staffs.ids ) end business_subject = business_subject.joins(:subject).left_joins(business_deliver_subject: [:manages, business: [:last_follow_up]]) if params[:q] && params[:q][:business_name].present? business_subject = business_subject.joins(:business).where("educode_sales_businesses.name like '%#{params[:q][:business_name]}%' ") end # 查询条件 实践课程名称 if params[:q] && params[:q][:subjects_name].present? business_subject = business_subject.where("subjects.name like '%#{params[:q][:subjects_name]}%'") end # 查询条件 时间 if params[:q] && params[:q][:time].present? start_time = params[:q][:time].split(" - ")[0] end_time = params[:q][:time].split(" - ")[-1] business_subject = business_subject.where("educode_sales_business_subjects.deliver_date BETWEEN '#{start_time}' AND '#{end_time}' OR educode_sales_follow_ups.reception_at BETWEEN'#{start_time}' AND '#{end_time}'") end business_subject_schools = {} business_subject_manges = {} business_subject_staffs = {} BusinessSubject.includes(:school, :manages, :subject, :staffs, business_deliver_subject: [:manages, business: [:school, last_follow_up: :assign_follow_ups] ]).find_each do |d| b_d_s = d.business_deliver_subject b_d_s.present? ? business_subject_schools.merge!({ "#{d.id}": d.business.school.name }) : business_subject_schools.merge!({"#{d.id}": d.school_name }) b_d_s.present? ? business_subject_manges.merge!({ "#{d.id}": b_d_s.subject_manages}) : business_subject_manges.merge!({"#{d.id}": d.manges_name }) b_d_s.present? ? business_subject_staffs.merge!({ "#{d.id}": d.business.staff_name }) : business_subject_staffs.merge!({"#{d.id}": d.staffs_name }) end @business_subject_schools = business_subject_schools.stringify_keys @business_subject_staffs = business_subject_staffs.stringify_keys @business_subject_manges = business_subject_manges.stringify_keys business_subject = business_subject.select("subjects.name s_name, subjects.identifier s_identifier, subjects.status status, subjects.public public, educode_sales_business_subjects.id, IFNULL(educode_sales_follow_ups.reception_at, educode_sales_business_subjects.deliver_date) deliver_date, IFNULL(educode_sales_businesses.name, '--' ) b_name ") @count = business_subject.count("educode_sales_business_subjects.id") @business_subject = business_subject.page(params[:page]).per(params[:limit]) end end end def new # staffs = Staff.where.not(role_id: 11).includes(:user) gon.staffs = @manages render layout: false end def edit if params[:id] business_subject = BusinessSubject.find(params[:id]) @item = business_subject.subject @select = @item.shixuns.map { |item| { value: item.id, name: item.name } } @id = business_subject.id @school_id = business_subject.school_id @default_select = BusinessSubjectShixun.where("business_subject_id = #{params[:id]}") .select(:shixun_id) .map { |item| item.shixun_id } render layout: false end end def create # 开启事务 ActiveRecord::Base.transaction do subject = Subject.where.not(id: BusinessSubject.pluck(:subject_id) ).find_by(id: params[:subject_id]) if subject.present? attr = { deliver_date: params[:deliver_date], school_id: params[:school_id], subject_id: subject.id } new_object = BusinessSubject.create(attr) new_object.add_subject_shixuns(params[:shixun_ids].split(',').map(&:to_i)) BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d| params[:manage_id].split(",").each do |m| d.add [m.to_i, new_object.id, BusinessSubjectStaff::CONTAINER_TYPES::SUBJECT, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SUBJECT_MANAGE ] end params[:sale_id].split(",").each do |s| d.add [s.to_i, new_object.id, BusinessSubjectStaff::CONTAINER_TYPES::SUBJECT, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SUBJECT_STAFF ] end end render_success else render_failure("操作失败") end end end def update # 开启事务 ActiveRecord::Base.transaction do business_subject = BusinessSubject.find_by(id: params[:id]) if business_subject new_shixun_ids = params[:shixun_ids].split(',').map(&:to_i) old_shixun_ids = business_subject.business_subject_shixuns.pluck(:shixun_id) # 需要删除的科目id change_shixuns = old_shixun_ids - new_shixun_ids add_shixuns = new_shixun_ids - old_shixun_ids # 取消关联 business_subject.business_subject_shixuns.where(shixun_id: change_shixuns).update_all(business_subject_id: nil) # 创建关联 business_subject.add_subject_shixuns(add_shixuns) render_success else render_failure("操作失败") end end end def shixun_list respond_to do |format| format.json do @list = Shixun.where(hidden: 0).where.not(id: BusinessSubjectShixun.joins(:business_subject).pluck(:shixun_id) ) # 1先创建实践项目下,通过实践课程无法选该 if params[:q].present? @list = @list.where("name like '%#{params[:q]}%'").page(params[:page]).per(10) end if params[:subject_id].present? @list = @list.joins(:stage_shixuns).where("subject_id = #{params[:subject_id]}") end render json: { data: @list.map { |item| { value: item[:id], name: item[:name] } }, code: 0 } end end end def search_subjects respond_to do |format| format.json do @subjects = Subject.unhidden.where.not(id: BusinessSubject.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 list_shixuns @shixuns = BusinessSubjectShixun.left_joins(:last_dectect) .left_joins(:shixun) .where("business_subject_id = #{params[:id]}") 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][:level].present? && params[:q][:level].to_i != 0 @shixuns = @shixuns.where(level: params[:q][:level]) end # 项目状态 0 全部 if params[:q].present? && params[:q][:status].present? && params[:q][:status].to_i != 0 @shixuns = @shixuns.where(shixun_status: params[:q][:status]) end if params[:q].present? && params[:q][:category].present? && params[:q][:category].to_i != 0 @shixuns = @shixuns.where(category: params[:q][:category]) end @shixuns = @shixuns.select("educode_sales_business_subject_shixuns.*, shixuns.name as shixun_name, shixuns.identifier, educode_sales_shixun_dectects.created_at as shixun_dectects_time, educode_sales_shixun_dectects.content as dectects_content") .page(params[:page]) .per(params[:limit]) render layout: false end end end