require_dependency "educode_sales/application_controller" module EducodeSales class IdeasController < ApplicationController before_action :find_idea, only: [:edit, :destroy, :detail, :history, :update] def index respond_to do |format| format.html do staff_ids = EducodeSales::Idea.all.pluck(:staff_id) creator_ids = EducodeSales::Idea.all.pluck(:creator_id) sale_staff_ids = EducodeSales::Idea.all.pluck(:sale_staff_id) @creator_arr = EducodeSales::Staff.joins(:user).where(id: creator_ids).pluck("concat(users.lastname,users.firstname)", :id) @staff_arr = EducodeSales::Staff.joins(:user).where(id: staff_ids).pluck("concat(users.lastname,users.firstname)", :id) @sale_staff_arr = EducodeSales::Staff.joins(:user).where(id: sale_staff_ids).pluck("concat(users.lastname,users.firstname)", :id) end format.json do @ideas = params[:is_deleted].to_s == "true" ? Idea.deleted : Idea.not_deleted if @current_admin.is_admin? @ideas = @ideas else level = @current_admin.role.role_areas.find_by(clazz: '方案管理').try(:level) case level when '自己' # business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id) # @businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids) idea_ids = Idea.all.pluck(:other_staff_ids).flatten.uniq @ideas = Idea.where("educode_sales_ideas.staff_id = ? OR educode_sales_ideas.id in (?)", @current_admin.id, idea_ids) when '区域' # 查看区域商机,需要排除掉其它人员手上的监管学校 other_staff_school_id = EducodeSales::StaffSchool.where.not(staff_id: @current_admin.id).where("school_id IN (SELECT school_id FROM educode_sales_staff_schools WHERE staff_id = #{@current_admin.id}) IS NOT TRUE").distinct.pluck :school_id school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) - other_staff_school_id idea_ids = Idea.all.pluck(:other_staff_ids).flatten.uniq @ideas = @ideas.joins(department: :school).where("schools.id in (?) OR educode_sales_ideas.staff_id = ? OR educode_sales_ideas.id in (?)", school_ids, @current_admin.id, idea_ids) else @ideas = @ideas end end @is_deleted = params[:is_deleted].to_s == "true" if params[:q] && params[:q][:created_at].present? date = params[:q][:created_at].split(" - ") @ideas = @ideas.where("educode_sales_ideas.created_at >= ? AND educode_sales_ideas.created_at <= ?", date[0] + " 00:00:00", date[1] + " 23:59:59") end if params[:q].present? && params[:q][:name].present? @ideas = @ideas.where("educode_sales_ideas.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:school].present? @ideas = @ideas.left_joins(:school).where("schools.name like ?", "%#{params[:q][:school]}%") end if params[:q].present? && params[:q][:creator_id].present? @ideas = @ideas.where("educode_sales_ideas.creator_id = ?", params[:q][:creator_id].to_i) end if params[:q].present? && params[:q][:staff_id].present? @ideas = @ideas.where("educode_sales_ideas.staff_id = ?", params[:q][:staff_id].to_i) end if params[:q].present? && params[:q][:search_sale_staff_id].present? @ideas = @ideas.where("educode_sales_ideas.sale_staff_id = ?", params[:q][:search_sale_staff_id].to_i) end if params[:q].present? && params[:q][:status].present? @ideas = @ideas.where(status: params[:q][:status]) end if params[:q].present? && params[:q][:types].present? @ideas = @ideas.where(types: params[:q][:types]) end if params[:q].present? && params[:q][:model].present? @ideas = @ideas.where(model: params[:q][:model]) end if params[:q].present? && params[:q][:history_type].present? if params[:q][:history_type].to_i == 0 @ideas = @ideas.where("educode_sales_ideas.history_type=#{params[:q][:history_type]} or educode_sales_ideas.history_type is null ") else @ideas = @ideas.where(history_type: params[:q][:history_type]) end end if params[:q].present? && params[:q][:level].present? @ideas = @ideas.where(level: params[:q][:level]) end @ideas = @ideas.page(params[:page]).per(params[:limit]) end end end def new staffs = Staff.where.not(role_id: 11).includes(:user) gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } } gon.department = { value: '', name: '' } gon.value = '' gon.attachments = [] render layout: false end def update_advise # followup = FollowUp.find(params[:id]) # followup.update(advise: params[:content]) # render_success idea = Idea.find(params[:id]) idea.update(advise: params[:content]) render_success end def create idea = Idea.new(idea_params) # idea.school_id = Department.find_by_id(idea.department_id)&.school_id idea.creator = current_user assist_staff_ids = Array(params[:assist_staff_ids].to_s.split(",")) attachment_ids = Array(params[:attachment_ids].to_s.split(",")) other_staff_ids = Array(params[:other_staff_ids].to_s.split(",")) if idea.business_id.present? idea.school_id = idea.business&.department&.school_id idea.department_id = idea.business&.department_id end idea.attachment_ids = attachment_ids idea.assist_staff_ids = assist_staff_ids idea.other_staff_ids = other_staff_ids idea.save render_success end def detail render layout: false end def history render layout: false end def update @idea.assign_attributes(idea_params) # @idea.school_id = Department.find_by_id(@idea.department_id)&.school_id assist_staff_ids = Array(params[:assist_staff_ids].to_s.split(",")) @idea.assist_staff_ids = assist_staff_ids attachment_ids = Array(params[:attachment_ids].to_s.split(",")) @idea.attachment_ids = attachment_ids other_staff_ids = Array(params[:other_staff_ids].to_s.split(",")) @idea.other_staff_ids = other_staff_ids if @idea.business_id.present? @idea.school_id = @idea.business&.department&.school_id @idea.department_id = @idea.business&.department_id end check_changes @idea.save render_success end def destroy @idea.soft_destroy(current_user.id) render_success end def edit staffs = Staff.where.not(role_id: 11).includes(:user) gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } } gon.staff_value = [{ name: @idea.staff&.user&.real_name, value: @idea.staff_id }] gon.sale_staff_value = [{ name: @idea.sale_staff&.user&.real_name, value: @idea.sale_staff_id }] gon.assist_staff_value = @idea.assist_staffs.map { |d| { name: d&.user&.real_name, value: d.id } } gon.attachment_list = @idea.attachments.map { |d| { name: d.filename, value: d.id } } gon.department = { value: @idea&.department_id, name: "#{@idea&.department&.school&.name}-#{@idea&.department&.name}" } gon.value = @idea.department_id gon.department_list = @idea.department.present? ? [{ name: @idea.department.name, value: @idea.department_id }] : [] gon.school_list = @idea.school.present? ? [{ name: @idea.school.name, value: @idea.school_id }] : [] gon.business_list = @idea.business.present? ? [{ name: @idea.business.name, value: @idea.business_id }] : [] gon.other_staff_value = @idea.other_staffs.map { |d| { name: d&.user&.real_name, value: d.id } } render layout: false end def show_teachers render layout: false end def upload_file render layout: false end def show_schools respond_to do |format| format.html do render layout: false end format.json do @data = EducodeSales::ActivityTeacher.joins(teacher: [department: :school]).where(idea_id: params[:id]).select("count(teacher_id) AS teachers_count, departments.name AS department, schools.name AS school, school_id").group("school_id") @data = @data.distinct.page(params[:page]).per(params[:limit]) end end end def new_follow_up @idea = Idea.find(params[:id]) gon.staffs = Staff.where.not(role_id: 11).includes(:user).map { |d| { name: d.user.real_name, value: d.id } } render layout: false end def follow_up idea = Idea.find(params[:id]) follow_up = IdeaFollow.new(idea_id: idea.id, staff_id: @current_admin.id, money: params[:money], content: params[:content], status: params[:status], advise: params[:advise], sale_staff_id: params[:sale_staff_id], idea_staff_id: params[:idea_staff_id]) if follow_up.save idea.last_idea_follow_id = follow_up.id idea.save render_success else render_failure follow_up end end def follow_ups @data = IdeaFollow if params[:q] && params[:q][:search_name].present? @data = @data.left_joins(idea: :business).where("educode_sales_businesses.name like ?", "%#{params[:q][:search_name]}%") .or(@data.left_joins(idea: :business).where("educode_sales_ideas.name like ?", "%#{params[:q][:search_name]}%")) end if params[:q] && params[:q][:search_school].present? @data = @data.joins(idea: [department: :school]).where("schools.name like ?", "%#{params[:q][:search_school]}%") end if params[:q] && params[:q][:search_sale_staff_id].present? @data = @data.where(sale_staff_id: params[:q][:search_sale_staff_id]) end if params[:q] && params[:q][:search_idea_staff].present? @data = @data.where(idea_staff_id: params[:q][:search_idea_staff]) end if params[:q] && params[:q][:search_created_at].present? @data = @data.where("educode_sales_idea_follows.created_at >= ? and educode_sales_idea_follows.created_at <= ?", params[:q][:search_created_at].split(" - ")[0], params[:q][:search_created_at].split(" - ")[1]) end if params[:q] && params[:q][:search_content].present? @data = @data.where("educode_sales_idea_follows.content like ?", "%#{params[:q][:search_content]}%") end # if params[:sort].present? && params[:sort][:field] # @data = @data.order("#{params[:sort][:field]} #{params[:sort][:order]}") # else # @data = @data.order("educode_sales_idea_follows.created_at desc") # end @data = @data.distinct.page(params[:page]).per(params[:limit]) end def add_advise followup = IdeaFollow.find(params[:id]) followup.update(advise: params[:content]) render_success end def files respond_to do |format| format.html do render layout: false end format.json do idea = Idea.find(params[:id]) @files = idea.files if params[:sort].present? && params[:sort][:field] @files = @files.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @files = @files.order("created_on desc") end @files = @files.page(params[:page]).per(params[:limit]) end end end private def idea_params params.permit(:name, :level, :staff_id, :status, :types, :model, :hardware, :project, :money, :end_time, :content, :department_id, :school_id, :manager_name, :manager_phone, :school_name, :department_name, :sale_staff_id, :attachment_id, :idea_type, :business_id) end def find_idea @idea = Idea.find(params[:id]) end def check_changes unless @idea.new_record? history = [] arr = @idea.attributes.except(:creator_id, :id, :history_type, :deleter_id, :is_deleted, :deleted_at, :history_record, :created_at, :updated_at).keys arr.each do |attr| if @idea.send("#{attr}_changed?") old_value, new_value = @idea.send("#{attr}_was"), @idea.send(attr) res = @idea.save_history(attr, old_value, new_value) history << res if res end end @idea.idea_histories.create(content: history, staff: current_user) if history.present? end end end end