module EducodeSales class BusinessSubject < ApplicationRecord belongs_to :business, optional: true belongs_to :business_deliver_subject, optional: true belongs_to :school, optional: true belongs_to :subject, optional: true has_many :business_subject_shixuns has_many :staffs, ->{subject_staffs}, as: :container, dependent: :destroy, class_name: "EducodeSales::BusinessSubjectStaff" has_many :manages, ->{subject_manges}, as: :container, dependent: :destroy, class_name: "EducodeSales::BusinessSubjectStaff" # 销售名称 def staffs_name staff_names = BusinessSubjectStaff.joins(staff: :user) .where("educode_sales_business_subject_staffs.category = #{ BusinessSubjectStaff::CATEGORY_TYPES::SUBJECT_STAFF } and educode_sales_business_subject_staffs.container_id = #{self.id} and educode_sales_business_subject_staffs.container_type = #{BusinessSubjectStaff::CONTAINER_TYPES::SUBJECT} ") .pluck("DISTINCT CONCAT(users.lastname, users.firstname) ") .join(",") # staffs.joins(staff: :user).pluck("CONCAT(users.lastname, users.firstname) ").join(",") staff_names end # 课程经理名称 def manges_name manages.joins(staff: :user).pluck("CONCAT(users.lastname, users.firstname) ").join(",") end # 实训完成统计 def shixun_compeled_count " #{business_subject_shixuns.where("educode_sales_business_subject_shixuns.shixun_status in (6,7,8)").count} / #{business_subject_shixuns.count}" end def school_name school.try(:name) end # def deliver_date # business ? business.last_follow_up.try(:reception_at)&.strftime("%Y-%m-%d") || "--" : deliver_date.strftime("%Y-%m-%d") # end # def b_name # business ? business.name : '--' # end def staffs business ? business.staff&.name : staffs_name end def manges business_deliver_subject ? business_deliver_subject.subject_manages : manges_name end def add_subject_shixuns(shixun_ids=[]) # 1先创建实践项目下,通过实践课程无法选该实践项目 shixuns = Shixun.where.not(id: BusinessSubjectShixun.joins(:business_subject).pluck(:shixun_id)) # 1先创建实践项目下,通过实践课程无法选该实践项目 shixun_ids.each do |item| now_shixun = shixuns.find_by(id: item) if now_shixun.present? add_item = BusinessSubjectShixun.create_with(name: now_shixun.name).find_or_create_by(shixun_id: item) add_item.update(business_subject_id: self.id ) end end end end end