require_dependency "educode_sales/application_controller" module EducodeSales class IdeasController < ApplicationController before_action :find_idea, only: [:edit, :destroy, :detail, :history, :update, :assign, :assign_staff, :assign_sale_staff, :update_sale_staff] def assign selected_staff_ids = @idea.assist_staff_ids.map(&:to_i) staffs = Staff.where.not(role_id: 11).includes(:user) gon.ideas_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: selected_staff_ids.include?(d.id) } } render layout: false end # 指派协作人 assist_staff_ids协作人 def assign_staff @idea.assist_staff_ids = params[:to_id].to_s.split(",") @idea.save render_success end def assign_sale_staff selected_staff_ids = Array(@idea.staff_id) staffs = Staff.where.not(role_id: 11).includes(:user) gon.sales_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: selected_staff_ids.include?(d.id) } } render layout: false end # 指派方案经理 staff_id def update_sale_staff @idea.staff_id = params[:to_id] @idea.save render_success end 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) filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "ideas") gon.filter = filter.extras || {} #协助人 @assist_staffs = Staff.where.not(role_id: 11).includes(:user).map{ |d| {name: d.user.real_name, value: d.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 '自己' idea_ids = @ideas.select { |d| Array(d.other_staff_ids).include?(@current_admin.id.to_s) || d.sale_staff_id == @current_admin.id || Array(d.assist_staff_ids).include?(@current_admin.id.to_s) || d.staff_id == @current_admin.id }.map { |d| d.id } @ideas = @ideas.where("educode_sales_ideas.creator_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 = @ideas.select { |d| Array(d.other_staff_ids).include?(@current_admin.id.to_s) || d.sale_staff_id == @current_admin.id || Array(d.assist_staff_ids).include?(@current_admin.id.to_s) || d.staff_id == @current_admin.id }.map { |d| d.id } @ideas = @ideas.left_joins(department: :school).where("schools.id in (?) OR educode_sales_ideas.creator_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.joins(:business).where("educode_sales_ideas.name like ? or educode_sales_businesses.name like ?", "%#{params[:q][:name]}%", "%#{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 if params[:q].present? && params[:q][:assist_staffs].present? @ideas = @ideas.select{|d| d&.assist_staff_ids.include?(params[:q][:assist_staffs])} end @ideas_count = @ideas.count @ideas = paginate @ideas 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 params[:business_id].present? idea.business_id = params[:business_id] idea.school_id = idea.business&.department&.school_id elsif params[:school_id].present? idea.name = params[:project_name] end if idea.school_id.blank? idea.school_id = params[:school_id] end idea.attachment_ids = attachment_ids # idea.assist_staff_ids = assist_staff_ids # idea.other_staff_ids = other_staff_ids idea.save attachment_ids.each do |a| attachment = Attachment.find_by_id(a) attachment.container_id = idea.id attachment.container_type = 'EducodeSales::Idea' attachment.save end 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 params[:business_id].present? @idea.business_id = params[:business_id] @idea.school_id = @idea.business&.department&.school_id elsif params[:school_id].present? @idea.name = params[:project_name] end if @idea.school_id.blank? @idea.school_id = params[:school_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]) staffs = Staff.where.not(role_id: 11).includes(:user) gon.sale_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea.sale_staff_id } } gon.idea_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea.staff_id } } render layout: false end def show_follow @idea_follow = IdeaFollow.find(params[:id]) staffs = Staff.where.not(role_id: 11).includes(:user) gon.sale_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.sale_staff_id } } gon.idea_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.idea_staff_id } } respond_to do |format| format.html do render layout: false end format.json do end end end def follow_list @idea = Idea.find(params[:id]) respond_to do |format| format.html do render layout: false end format.json do @follow_ups = @idea.idea_follows.includes(:staff, :sale_staff, :idea_staff).order(id: :desc) @latest = @follow_ups.order(id: :desc).first @follow_ups = @follow_ups.page(params[:page]).per(params[:limit]) end end end def edit_follow_record @idea_follow = IdeaFollow.find(params[:id]) @idea = @idea_follow.idea staffs = Staff.where.not(role_id: 11).includes(:user) gon.sale_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.sale_staff_id } } gon.idea_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.idea_staff_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 attachment_ids = Array(params[:attachment_ids].to_s.split(",")) attachment_ids.each do |a| attachment = Attachment.find_by_id(a) attachment.container_id = idea.id attachment.container_type = 'EducodeSales::Idea' attachment.description = follow_up.id.to_s attachment.save end 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, :manager_name, :manager_phone, :school_name, :department_name, :attachment_id, :idea_type, :school_id, :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