# frozen_string_literal: true # 实践项目管理 module EducodeSales class ShixunsController < ApplicationController before_action :subject_members, :subject_staffs, :create_filter, :subject_url def index authorize! :shixun, BusinessDeliverSubject # 判断类型是否存在 1 管培 2 全职 3 专职 如果不存在就默认为1 category = params[:q].present? ? params[:q][:category].to_i : BusinessSubjectShixun::CATEGORY_TYPE::TRAINING trends_category = params[:trends_category] gon.category = trends_category.present? ? trends_category.to_i : 1 gon.is_admin = is_commissioner_above? business_subject_shixuns = BusinessSubjectShixun # 根据身份获取对应数据 business_subject_shixuns = if is_commissioner_above? business_subject_shixuns else # 实训本身的成员 smembers_ids = business_subject_shixuns.joins(shixun_members: :staff).where("educode_sales_staffs.id = #{@current_admin.id} ") .ids # 父类课程经理 deliver_ids = business_subject_shixuns.joins(business_subject: [business_deliver_subject: [manages: :staff]]) .where("educode_sales_staffs.id = #{@current_admin.id} ") .ids subject_ids = business_subject_shixuns.joins(business_subject: [manages: :staff]) .where("educode_sales_business_subjects.business_id is null and educode_sales_staffs.id = #{@current_admin.id} ") .ids business_subject_shixuns.where(id: smembers_ids + deliver_ids + subject_ids) end @shixuns = business_subject_shixuns.left_joins(:last_dectect) .left_joins(:school, :shixun , business_subject: [:subject, business: :last_follow_up]) .where(category: category) # if params[:q].present? && params[:q][:business_name].present? # @shixuns = @shixuns.where("educode_sales_businesses.name like '%#{params[:q][:business_name]}%' ") # end # 项目级别 0 全部 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] && params[:q][:time].present? start_time = params[:q][:time].split(" - ")[0] end_time = params[:q][:time].split(" - ")[-1] # @shixuns = @shixuns.where("educode_sales_follow_ups.reception_at BETWEEN '#{start_time}' AND '#{end_time}' # or educode_sales_business_subject_shixuns.deliver_date BETWEEN '#{start_time}' AND '#{end_time}' ") @shixuns = @shixuns.where("educode_sales_follow_ups.reception_at BETWEEN '#{start_time}' AND '#{end_time}'") end if params[:q].present? && params[:q][:shixun_name].present? @shixuns = @shixuns.where("educode_sales_business_subject_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 shixun_staffs = {} BusinessSubjectShixun.includes(business_subject:[ business_deliver_subject: [:manages, business: [:school, last_follow_up: :assign_follow_ups]] ]).find_each do |d| b_d_s = d.business_subject if b_d_s.blank? shixun_staffs.merge!({"#{d.id}": d.shixun_staff }) elsif b_d_s.present? && b_d_s.business_deliver_subject.present? shixun_staffs.merge!({ "#{d.id}": b_d_s.business_deliver_subject.business.staff_name }) elsif b_d_s.present? shixun_staffs.merge!({ "#{d.id}": b_d_s.staffs_name }) end end @shixun_staffs = shixun_staffs.stringify_keys p @shixun_staffs @shixuns = @shixuns.select("educode_sales_business_subject_shixuns.*, educode_sales_follow_ups.reception_at, schools.name as school_name, subjects.name as subject_name, subjects.identifier as subjects_identifier, shixuns.name as shixun_name, shixuns.identifier as s_identifier, educode_sales_shixun_dectects.created_at as shixun_dectects_time, educode_sales_shixun_dectects.content as dectects_content, shixuns.identifier") .page(params[:page]) .per(params[:limit]) end def edit authorize! :shixun, BusinessDeliverSubject @item = BusinessSubjectShixun.find(params[:id]) @last_dectect = '' if @item.last_dectect.present? @last_dectect = @item.last_dectect.dectect_type end @producer_list = BusinessSubjectStaff.where(container_type: BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, container_id: params[:id], category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER) .select(:staff_id) .map { |item| item[:staff_id] } @manage_list = BusinessSubjectStaff.where(container_type: BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, container_id: params[:id], category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE) .select(:staff_id) .map { |item| item[:staff_id] } gon.staffs = @manages render layout: false end def update authorize! :shixun, BusinessDeliverSubject ActiveRecord::Base.transaction do if params[:id].present? item = BusinessSubjectShixun.find(params[:id]) if item.present? item.update!(category: params[:category], level: params[:level], deliver_money: params[:money], shixun_status: params[:status], deliver_date: params[:time], shixun_type: params[:type]) item.staffs.where(category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE).destroy_all item.staffs.where(category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER).destroy_all 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, params[:id], BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE] end end BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d| params[:producer_id].split(",").each do |m| d.add [m.to_i, params[:id], BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER] end end end render_success else render_failure("操作失败") end end end def new authorize! :shixun, BusinessDeliverSubject gon.manage = @manages gon.staffs = @staffs render layout: false end def create authorize! :shixun, BusinessDeliverSubject begin ActiveRecord::Base.transaction do attr = { name: params[:name].to_s, category: params[:category].to_i, shixun_type: params[:type].to_i, shixun_status: params[:status].to_i, level: params[:level].to_i, link: params[:link].to_s, deliver_money: params[:money].to_s, deliver_date: params[:time], school_id: params[:school_id] } if params[:shixun_type].to_i == 1 # 目前支持名称关联实训 origin_shixun = Shixun.where(hidden: 0).find_by_name(params[:name].to_s) attr = origin_shixun.blank? ? attr : attr.merge({shixun_id: origin_shixun.id}) end shixun = BusinessSubjectShixun.create!(attr) 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, shixun.id, BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE] end end BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d| params[:producer_id].split(",").each do |m| d.add [m.to_i, shixun.id, BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER] end end BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d| params[:staff_id].split(",").each do |m| d.add [m.to_i, shixun.id, BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_STAFF] end end end render_success rescue => e Rails.logger.error("操作失败:#{e}") render_failure("操作失败") end end def create_filter filter_data = { "school_name" => 0, "subject_name" => 0, "reception_at" => 0, "shixun_name" => 0, "level" => 0, "shixun_status" => 0, "deliver_date" => 0, "commit_dectect_time" => 0, "dectect_feedback" => 0, "shixun_producer" => 0, "shixun_manages" => 0, "max_manage"=> 0, "shixun_staff" => 0 } filter = Filter.create_with(extras: filter_data).find_or_create_by(staff_id: @current_admin.id, clazz: "shixuns") gon.filter = filter.extras end end end