module EducodeSales class BusinessSubjectShixun < ApplicationRecord belongs_to :business_subject, optional: true belongs_to :school, optional: true belongs_to :shixun, optional: true has_many :staffs, as: :container, dependent: :destroy, class_name: "EducodeSales::BusinessSubjectStaff" has_many :shixun_members, as: :container, dependent: :destroy, class_name: "EducodeSales::BusinessSubjectStaff" has_many :shixun_producer, ->{ shixun_producer }, as: :container, dependent: :destroy, class_name: "EducodeSales::BusinessSubjectStaff" belongs_to :last_dectect, foreign_key: :shixun_dectect_id, class_name: "EducodeSales::ShixunDectect", optional: true module CATEGORY_TYPE TRAINING = 1 # 管培 WORKER = 2 # 全职 PLURALIST = 3 # 兼职 end scope :search_type, ->(type) {where(shixun_type: type.to_i) if type.to_i != 0} scope :search_level, ->(level) {where(level: level.to_i) if level.to_i != 0} scope :search_status, ->(status) {where(shixun_status: status.to_i) if status.to_i != 0} scope :search_category, ->(category) {where(category: category.to_i) if category.to_i != 0} scope :training, -> {where(category: CATEGORY_TYPE::TRAINING)} scope :worker, -> {where(category: CATEGORY_TYPE::WORKER)} scope :pluralist, -> {where(category: CATEGORY_TYPE::PLURALIST)} scope :completed, -> {where(shixun_status: [6,7,8])} scope :un_completed, -> {where.not(shixun_status: [6,7,8])} # 项目状态: 待建设(constructed) 已签协议(signed) 建设中(construction) 审核中(review),返修中(repair) 已内部公开(public), 已公开发布(published) 已经付费(paid) # enum shixun_status: { constructed: 1, signed: 2, construction: 3, review: 4, repair: 5, public: 6, published: 7,paid: 8} def shixun_manages staffs.joins(staff: :user).where(category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE).pluck("CONCAT(users.lastname, users.firstname) ").join(",") end def shixun_producer staffs.joins(staff: :user).where(category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER).pluck("CONCAT(users.lastname, users.firstname) ").join(",") end def shixun_staff staffs.joins(staff: :user).where(category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_STAFF).pluck("CONCAT(users.lastname, users.firstname) ").join(",") end def all_staff staffs.joins(staff: :user).distinct.select("users.id") end def shixun_school name = "" shixuns = BusinessSubjectShixun.find(id) if shixuns.school.present? name = shixuns.school.name end if shixuns.business_subject.present? if shixuns.business_subject.business.present? name = shixuns.business_subject.business.school.name end end p name end # 查询审核历史和审核人 def audit_history ShixunDectect.joins("join educode_sales_staffs on educode_sales_staffs.id = educode_sales_shixun_dectects.reviewed_id join users on users.id = educode_sales_staffs.user_id") .where(business_subject_shixun_id: id) .where("dectect_type != 0") .select("educode_sales_shixun_dectects.*, CONCAT(users.lastname, users.firstname) as examine_name") .order("created_at desc") .all end # def school_name # business_subject ? business_subject.school_name : school.name # end end end