require_dependency "educode_sales/application_controller" module EducodeSales class BusinessesController < ApplicationController skip_before_action :verify_authenticity_token,only: [:following] def index authorize! :read, Business respond_to do |format| format.html do common = Common.find_by(clazz: 'staff_type', name: '销售') @staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| [d.user.real_name, d.id]} # [[], [], ............] # @more = can?(:create, EducodeSales::SalePlan) || can?(:update, EducodeSales::Business) || can?(:destroy, EducodeSales::Business) || can?(:show_file, EducodeSales::Business) @more = true gon.menus = [] gon.export_menus = [] gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : [] bussiness_type = [] if params[:type] # 项目类型 bussiness_type = EducodeSales::Business.include_types(params[:type]) || [] end business_step = [] if params[:step] # 项目阶段 business_step = EducodeSales::Business.include_steps(params[:step]) || [] end if params[:business_year].present? && params[:business_year] != '全部' gon.business_year = params[:business_year] end if params[:bidded_date_year].present? && params[:bidded_date_year] != '全部' gon.bidded_date = "2015-01-01" + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d") end if params[:signed_date_year].present? && params[:signed_date_year] != '全部' gon.signed_date = Time.new(params[:signed_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:signed_date_year]).end_of_year.strftime("%Y-%m-%d") end if params[:date_at].present? && params[:date_at] != '全部' # 回款时间 gon.date_at = Time.new(params[:date_at]).strftime("%Y-%m-%d") + " - " + Time.new(params[:date_at]).end_of_year.strftime("%Y-%m-%d") end gon.business_type = Common.where(clazz: 'business_type').map do |d| {value: d.id, name: d.name, selected: bussiness_type.include?(d.extras)} end gon.business_step = Common.where(clazz: 'business_step').order("position").map do |d| {value: d.id, name: d.name, selected: business_step.include?(d.name)} end if can?(:create, EducodeSales::SalePlan) # gon.menus << { title: '添加团队建议', event: 'suggest' } gon.menus << { title: '添加周计划', event: 'week' } gon.menus << { title: '添加月计划', event: 'month' } end gon.menus << { title: '关注', event: 'following' } gon.menus << { title: '跟进时间线', event: 'time_line' } if can?(:show_file, EducodeSales::Business) gon.menus << { title: '附件管理', event: 'file' } end if can?(:update, EducodeSales::Business) # gon.menus << { title: '编辑', event: 'edit' } end if can?(:destroy, EducodeSales::Business) gon.menus << { title: '删除', event: 'delete' } end if can?(:export_business, EducodeSales::Business) gon.export_menus << { title: '导出到Csv文件', event: 'export_csv' } gon.export_menus << { title: '导出到Excel文件', event: 'export_excel' } end gon.export_menus << { title: '导出记录', event: 'export_records' } filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "businesses_list") if filter.extras.present? if filter.extras['area'].nil? filter.extras['area'] = 1 filter.extras['property'] = 1 filter.extras['sale_way'] = 1 end filter.save gon.filter = filter.extras else gon.filter = {'area': 1, 'property': 1, 'sale_way': 1} end end format.json do if @current_admin.is_admin? @businesses = Business else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').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) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) 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.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_businesses.staff_id = #{@current_admin.id} OR educode_sales_businesses.id in (?)", school_ids, business_ids) else @businesses = Business end end if params[:q].present? && params[:q][:name].present? @year = params[:q][:name].split("-")[1].present? ? params[:q][:name].split("-")[1] : '' end # @budget_stage_ids = Common.where(clazz: '商机阶段', name: ['初步接洽', '准备方案','已交方案', '已立项']).pluck(:id) if params[:q] && params[:q][:signed_date].present? date = params[:q][:signed_date].split(" - ") @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", date[0], date[1]) end if params[:q]&& params[:q][:date_at].present? date = params[:q][:date_at].split(" - ") x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= 1").where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", date[0] + " 00:00:00", date[1] + " 23:59:59").distinct end if params[:q] && params[:q][:bidded_date].present? date = params[:q][:bidded_date].split(" - ") @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", date[0], date[1]) end if params[:q].present? && params[:q][:all].present? @businesses = @businesses end if params[:q].present? && params[:q][:name].present? @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) end if params[:q].present? && params[:q][:staff_id].present? part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[:q][:staff_id]).ids part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.staff_id = ?", params[:q][:staff_id]).ids business_ids = part_a_ids + part_b_ids @businesses = EducodeSales::Business.where(id:business_ids) end if params[:q].present? && params[:q][:business_type].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.clazz_id in (?)", params[:q][:business_type].split(",").map(&:to_i)) end if params[:q].present? && params[:q][:business_step].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.stage_id in (?)", params[:q][:business_step].split(",").map(&:to_i)) end if params[:q].present? && params[:q][:place_id].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.place_id = ?", params[:q][:place_id]) end if params[:q].present? && params[:q][:business_year].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.year = ?", params[:q][:business_year]) end if params[:q].present? && params[:q][:o_business_deployment].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.o_business_deployment = ?", params[:q][:o_business_deployment]) end if params[:q].present? && params[:q][:area].present? p = EducodeSales::Common.find(params[:q][:area]).name if @current_admin.is_admin? @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level if level == "区域" @businesses = @businesses.joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end end end if params[:q].present? && params[:q][:date].present? date = params[:q][:date].split(" - ") @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59') end if params[:q].present? && params[:q][:invitation_at].present? date = params[:q][:invitation_at].split(" - ") @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59') end # 根据编号获取商机 if params[:q].present? && params[:q][:number].present? @businesses = @businesses.where("number like :data", data: "%#{params[:q][:number].strip}%") end if params[:q].present? && params[:q][:clazz].present? # 我的关注 if params[:q][:clazz] == 'followes' my_ids = BusinessRelationShip.where(user_id:@current_admin.user_id).pluck(:business_id) @businesses = @businesses.where(id: my_ids) elsif params[:q][:clazz] == 'me' ids = AssignFollowUp.joins("JOIN educode_sales_follow_ups ON educode_sales_follow_ups.id = educode_sales_assign_follow_ups.follow_up_id").where(staff_id: @current_admin.id).pluck(:business_id) @businesses = @businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids, @current_admin.id) end end if params[:sort].present? && params[:sort][:field] if params[:sort][:field] == "service_end_time" @businesses = @businesses.order("service_time_long #{params[:sort][:order]}") else @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}") end else @businesses = @businesses.order("educode_sales_businesses.created_at desc") end @business_count = @businesses.count # mysql -select @businesses = @businesses.select(" educode_sales_businesses.*, last_follow.invitation_at, last_follow.budget_amount, last_follow.o_business_deployment, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.reception_at, last_follow.bidded_date, last_follow.signed_date, last_follow.year, last_follow.created_at as latest_time, last_follow.actual_amount, last_follow.divide_rate, last_follow.divide_amount, (last_follow.total_amount - last_follow.actual_amount) as divide_money, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount, total_follow_ups.follow_ups_count ").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL LEFT JOIN ( SELECT COUNT(*) AS follow_ups_count, educode_sales_follow_ups.business_id FROM educode_sales_follow_ups WHERE educode_sales_follow_ups.deleted_at IS NULL GROUP BY educode_sales_follow_ups.business_id ) AS total_follow_ups ON educode_sales_businesses.id = total_follow_ups.business_id ").includes(:users, last_follow_up: [:clazz, :assign_follow_ups]).page(params[:page]).per(params[:limit]) end end end def daily_paper @followups = EducodeSales::FollowUp.where(staff_id: @current_admin.id).where("updated_at > ?", Time.now.beginning_of_day).order("updated_at") ab = EducodeSales::Common.where(extras: [EducodeSales::Common::ATYPE, EducodeSales::Common::BTYPE]).pluck(:id) business = EducodeSales::FollowUp.joins(" JOIN educode_sales_businesses ON educode_sales_businesses.id = educode_sales_follow_ups.business_id JOIN educode_sales_follow_ups AS last_up ON last_up.id = educode_sales_businesses.last_follow_up_id ").where("educode_sales_businesses.staff_id = ? AND educode_sales_follow_ups.staff_id = ? AND last_up.clazz_id IN (?) AND last_up.staff_id = #{@current_admin.id}", @current_admin.id, @current_admin.id, ab) @month = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_month).count("DISTINCT(educode_sales_follow_ups.business_id)") @quarter = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_quarter).count("DISTINCT(educode_sales_follow_ups.business_id)") @year = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_year).count("DISTINCT(educode_sales_follow_ups.business_id)") render layout: false end def create department = Department.find(params[:department_id]) business = @current_admin.businesses.build(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id) if business.save render_success else render_failure business end end def new render layout: false end def edit @business = Business.find(params[:id]) gon.department = {value: @business.department_id, name: "#{@business.department.school.name}-#{@business.department.name}"} gon.value = @business.department_id render layout: false end def update business = Business.find(params[:id]) department = Department.find(params[:department_id]) if business.update(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id) render_success else render_failure business end end def updateMoney business = Business.find(params[:id]) if business.update(return_money: params[:return_money]) render_success else render_failure business end end def destroy business = Business.find(params[:id]) business.soft_destroy(@current_admin.id) render_success rescue ActiveRecord::DeleteRestrictionError => e render_failure '该商机已其它关联数据产生,暂不能删除' end def show_keys render layout: false end def add_keys render layout: false end def show_follow load_business respond_to do |format| format.html do render layout: false end format.json do @follow_ups = @business.follow_ups @latest = @follow_ups.order(created_at: :desc).first if params[:field] @follow_ups = @follow_ups.order("#{params[:field]} #{params[:order]}") else @follow_ups = @follow_ups.order("created_at desc") end @follow_ups = @follow_ups.page(params[:page]).per(params[:limit]) end end end def new_follow_record load_business @years = (2015..Time.now.year).to_a.reverse @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id) @stages = Common.where(clazz: 'business_step').order("position").pluck(:name, :id) @deployment_type = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT @places = Place.order("created_at desc").pluck(:name, :id) @last_follow_up = @business.last_follow_up if @last_follow_up gon.sales = @last_follow_up.assign_follow_ups.map do |d| {value: d.staff_id, name: d.staff.user.full_name} end gon.place = @last_follow_up.place.present? ? [{ value: @last_follow_up.place&.id, name: @last_follow_up.place&.name }] : [] end render layout: false end def unfinish_plans load_business @plans = @business.sale_plans.where.not(finish_rate: 100).page(params[:page]).per(params[:limit]) end def show_follow_record follow_up_id = params[:follow_up_id] @follow_up = FollowUp.find(follow_up_id) id = @follow_up.id @follow_ups = Business.find(@follow_up.business_id).follow_ups ids = @follow_ups.ids size = ids.size ids = ids.reverse index = ids.index(id) EducodeSales::FollowUp::BUSINESS_DEPLOYMENT[1..-1].each do |m| if @follow_up.o_business_deployment == m[1] @show_follow_recored_deployment = m[0] break end end @next_id = (index != size - 1 && size > 1) ? ids[index + 1] : 0 @pre_id = (index != 0 && size > 1) ? ids[index - 1] : 0 render layout: false end def edit_follow_record @years = (2015..Time.now.year).to_a.reverse @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name @deployment_type = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT @follow_up = FollowUp.find(params[:follow_up_id]) @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id) @stages = Common.where(clazz: 'business_step').order("position").pluck(:name, :id) @places = Place.order("created_at desc").pluck(:name, :id) gon.sales = @follow_up.assign_follow_ups.map do |d| {value: d.staff_id, name: d.staff.user.full_name} end gon.place = @follow_up.place.present? ? [{ value: @follow_up.place&.id, name: @follow_up.place&.name }] : [value: '', name: ''] render layout: false end def edit_plan render layout: false end def file gon.folder = edu_setting('attachment_folder') + '/' render layout: false end def upload_file render layout: false end def time_line @business = Business.find(params[:business_id]) respond_to do |format| format.html do @follow_ups = @business.follow_ups.order(created_at: :desc) render layout: false end end end def export_records respond_to do |format| format.html do render layout: false end format.json do @records = BusinessExportRecord.all.order(id: :desc).page(params[:page]).per(params[:limit]) end end end def add_export_records if params[:type] == 'csv' record = BusinessExportRecord.new(name: '商机数据表.csv', staff_id: @current_admin.id) else params[:type] == 'excel' record = BusinessExportRecord.new(name: '商机数据表.xls', staff_id: @current_admin.id) end if record.save render_success else render_failure business end end def get_export_data respond_to do |format| format.html do end format.json do if @current_admin.is_admin? @businesses = Business else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').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) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) 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.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_businesses.staff_id = #{@current_admin.id} OR educode_sales_businesses.id in (?)", school_ids, business_ids) else @businesses = Business end end if params[:signed_date].present? date = params[:signed_date].split(" - ") @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", date[0], date[1]) end if params[:date_at].present? date = params[:date_at].split(" - ") x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= 1").where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", date[0] + " 00:00:00", date[1] + " 23:59:59").distinct end if params[:bidded_date].present? date = params[:bidded_date].split(" - ") @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", date[0], date[1]) end if params[:business_year].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.year = ?", params[:business_year]) end if params[:all].present? @businesses = @businesses end if params[:mine].present? ids = AssignFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id) ids1 = FollowUp.where(id: ids).pluck(:business_id) @businesses =@businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids1, @current_admin.id) end if params[:is_follow].present? # 关注 my_ids = BusinessRelationShip.where(user_id:@current_admin.user_id).pluck(:business_id) @businesses = @businesses.where(id: my_ids) end @businesses = @businesses if params[:name].present? # && params[:name] != "(销售态势-" + @year + "-已中标商机)" && params[:name] != "(销售态势-" + @year + "-已签单商机)" && params[:name] != "(销售态势-" + @year + "-已回款商机)" && params[:name] != "(销售态势-" + @year + "-现有商机)"&& params[:name] != "(销售态势-" + @year + "-应收款商机)" @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:name]}%") end if params[:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:department]}%").pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) end if params[:staff_id].present? part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[:staff_id]).ids part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.staff_id = ?", params[:staff_id]).ids business_ids = part_a_ids + part_b_ids @businesses = EducodeSales::Business.where(id:business_ids) end if params[:business_type].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.clazz_id in (?)", params[:business_type].split(",").map(&:to_i)) end if params[:business_step].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.stage_id in (?)", params[:business_step].split(",").map(&:to_i)) end if params[:place_id].present? @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.place_id = ?", params[:place_id]) end if params[:area].present? p = EducodeSales::Common.find(params[:area]).name if @current_admin.is_admin? @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level if level == "区域" @businesses = @businesses.joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end end end if params[:date].present? date = params[:date].split(" - ") @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59') end if params[:invitation_at].present? date = params[:invitation_at].split(" - ") @businesses = @businesses.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59') end if params[:sort].present? && params[:sort][:field] @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @businesses = @businesses.order("educode_sales_businesses.created_at desc") end @business_count = @businesses.count @businesses = @businesses.select(" educode_sales_businesses.*, last_follow.invitation_at, last_follow.budget_amount, last_follow.reception_at, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.bidded_date, last_follow.signed_date, last_follow.created_at as latest_time, last_follow.actual_amount, last_follow.divide_rate, last_follow.divide_amount, (last_follow.total_amount - last_follow.actual_amount) as divide_money, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount, total_follow_ups.follow_ups_count ").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL LEFT JOIN ( SELECT COUNT(*) AS follow_ups_count, educode_sales_follow_ups.business_id FROM educode_sales_follow_ups WHERE educode_sales_follow_ups.deleted_at IS NULL GROUP BY educode_sales_follow_ups.business_id ) AS total_follow_ups ON educode_sales_businesses.id = total_follow_ups.business_id ").includes(last_follow_up: [:clazz, :assign_follow_ups]) end end end #关注接口 def following if brs=follow_business(@current_admin.user_id,params[:business_id]) if brs.delete render json: {code:200,msg:"已取消关注"} else render json: {code:300,msg:"操作失败"} end else brs=BusinessRelationShip.new(user_id:@current_admin.user_id, business_id:params[:business_id]) if brs.save render json: {code:200,msg:"关注成功"} else render json: {code:300,msg:"操作失败"} end end end #我已关注的商机 def following_businesses p=[] BusinessRelationShip.select(:business_id).where(user_id:@current_admin.user_id).to_a.each { |d| p << d.business_id} @businesses=Business.where(id:p) @businesses=@businesses.select(" educode_sales_businesses.*, last_follow.budget_amount, last_follow.invitation_at, last_follow.reception_at, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.bidded_date, last_follow.signed_date, last_follow.created_at as latest_time, last_follow.actual_amount, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id ").page(params[:page]).per(params[:limit]) end def followed if follow_business(@current_admin.user_id,params[:business_id]) render json: {msg:'确定取消关注?'} else render json: {msg:'确定关注?'} end end private def load_business @business = Business.find(params[:id]) end def follow_up_params params.permit(:clazz_id, :stage_id, :invitation_at, :reception_at, :total_amount, :actual_amount, :divide_amount, :description, :advise, :place_id) end def edu_setting name EduSetting.get(name) end def follow_business(user_id,business_id) BusinessRelationShip.where(user_id:user_id,business_id:business_id).first end end end