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) @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) end format.json do @ideas = Idea.not_deleted 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][: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 = '' render layout: false 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(",")) idea.assist_staff_ids = assist_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 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.department = { value: @idea&.department_id, name: "#{@idea&.department&.school&.name}-#{@idea&.department&.name}" } gon.value = @idea.department_id render layout: false 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,:sale_staff_id,:attachment_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 # def upload_file(file) # folder = File.join(Rails.root, "public", "files", 'educode_sales') # upload_file = file # raise "未上传文件" unless upload_file # save_path = File.join(folder) # ext = file_ext(upload_file.original_filename) # local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext) # content_type = upload_file.content_type.presence || 'application/octet-stream' # disk_filename = local_path[save_path.size + 1, local_path.size] # @attachment = Attachment.where(disk_filename: disk_filename, # author_id: current_user.id).first # if @attachment.blank? # @attachment = Attachment.new # @attachment.filename = upload_file.original_filename # @attachment.description = "popup_windows" # @attachment.disk_filename = local_path[save_path.size + 1, local_path.size] # @attachment.filesize = upload_file.tempfile.size # @attachment.content_type = content_type # @attachment.digest = digest # @attachment.author_id = current_user.id # @attachment.container_id = current_user.id # @attachment.save! # else # logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" # end # @attachment.id # # render_ok # end # def edu_setting name # EduSetting.get(name) # end # # # def file_ext(file_name) # ext = '' # exts = file_name.split(".") # if exts.size > 1 # ext = ".#{exts.last}" # end # ext # end # # def file_save_to_local(save_path, temp_file, ext) # unless Dir.exists?(save_path) # FileUtils.mkdir_p(save_path) ##不成功这里会抛异常 # end # # digest = md5_file(temp_file) # digest = "#{digest}_#{(Time.now.to_f * 1000).to_i}" # local_file_path = File.join(save_path, digest) + ext # save_temp_file(temp_file, local_file_path) # # [local_file_path, digest] # end # # # def md5_file(temp_file) # md5 = Digest::MD5.new # temp_file.rewind # while (buffer = temp_file.read(8192)) # md5.update(buffer) # end # md5.hexdigest # end # # def save_temp_file(temp_file, save_file_path) # File.open(save_file_path, 'wb') do |f| # temp_file.rewind # while (buffer = temp_file.read(8192)) # f.write(buffer) # end # end # end end end