# frozen_string_literal: true module EducodeSales include SubjectHelper # 实践课程管理 class SubjectsController < ApplicationController before_action :subject_members def index authorize! :subject, BusinessDeliverSubject respond_to do |format| format.html do end format.json do # d.last_follow_up&.assign_follow_ups.present? ? (d.last_follow_up.assign_follow_ups.map{ |d| d.staff.user.real_name}.join("、")) : d.staff&.user&.real_name business_subject = BusinessSubject.joins(:subject).left_joins(business_deliver_subject: [:manages, business: [:last_follow_up]]) if params[:q] && params[:q][:shixun].present? business_subject = business_subject.joins("INNER JOIN educode_sales_business_subject_shixuns ON educode_sales_business_subject_shixuns.business_subject_id = educode_sales_business_subjects.id") .where("educode_sales_business_subject_shixuns.name like '%#{params[:q][:shixun]}%'") 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] ]).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}": "--" }) # 先暂时处理 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.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 authorize! :subject, BusinessDeliverSubject # staffs = Staff.where.not(role_id: 11).includes(:user) gon.staffs = @manages render layout: false end def edit authorize! :subject, BusinessDeliverSubject 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 authorize! :subject, BusinessDeliverSubject # 开启事务 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) subject.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 authorize! :subject, BusinessDeliverSubject # 开启事务 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.unhidden.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 if params[:id].present? @shixuns = BusinessSubjectShixun.where("business_subject_id = #{params[:id]}") .joins(:shixun) 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") .page(params[:page]) .per(params[:limit]) end render layout: false end end end