# frozen_string_literal: true # 实践项目管理 module EducodeSales class ShixunsController < ApplicationController before_action :subject_members, :subject_staffs, :create_filter def index # 判断类型是否存在 1 管培 2 全职 3 专职 如果不存在就默认为1 category = params[:q].present? ? params[:q][:category].to_i : BusinessSubjectShixun::CATEGORY_TYPE::TRAINING @shixuns = BusinessSubjectShixun.left_joins(:school, :shixun , business_subject: [:subject, business: :last_follow_up]) .where(category: category) # 项目级别 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 @shixuns = @shixuns.select("educode_sales_business_subject_shixuns.*, educode_sales_follow_ups.reception_at, schools.name as school_name, subjects.name as subject_name, shixuns.name as shixun_name, shixuns.identifier") .page(params[:page]) .per(params[:limit]) end def edit @item = BusinessSubjectShixun.find(params[:id]) @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 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 gon.manage = @manages gon.staffs = @staffs render layout: false end def create 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.unhidden.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