class RulingTimerTask include Mongoid::Document include Mongoid::Timestamps include OrbitTag::Taggable field :ticket_no, type: String, default: "" field :task_name, type: String, default: "" has_many :ruling_timer_sub_tasks, :autosave => true, :dependent => :destroy field :user_ids, type: Array, default: [] #負責⼈、協助人 field :is_finished, type: Boolean, default: false field :time_offset, type: String, default: "+8" field :privacy, type: Integer, default: 0 #私密等級 0 => 所有團隊成員Open, 1 => 專案成員Project, 2 => 優先, 3 => 任務成員 Task field :priority, type: Integer, default: 0 #優先度 0 => 無, 1 => 普通, 2 => 優先, 3 => 緊急 belongs_to :creator , :class_name=>"User", :foreign_key => :creator_id #建立者 belongs_to :owner , :class_name=>"User", :foreign_key => :owner_id #負責⼈ belongs_to :ruling_timer_project field :helper_ids, type: Array, default: [] #協助人 field :observer_ids, type: Array, default: [] #觀察者 field :details, type: String, default: "" field :progress, type: Integer, default: 0 #進度 0 ~ 100 field :update_user_id has_many :ruling_timer_files, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :ruling_timer_files, :allow_destroy => true has_many :ruling_timer_comments, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :ruling_timer_comments, :allow_destroy => true belongs_to :ruling_timer_section has_many :ruling_timer_notifies, :autosave => true, :dependent => :destroy after_initialize do unless self.new_record? save_flag = false if self.owner.nil? && self.user_ids.present? self.owner_id = self.user_ids.first save_flag = true end if self.creator.nil? && self.user_ids.present? self.creator_id = self.user_ids.first save_flag = true end if self.ruling_timer_project_id.nil? project = RulingTimerProject.where(:creator=>self.creator).first if project.nil? project = RulingTimerProject.create(:created_at=>self.created_at,:creator=>self.creator,:time_offset=>self.time_offset) end self.ruling_timer_project = project save_flag = true end self.save if save_flag end end before_create do |record| time_now = record.created_at || DateTime.now.utc.new_offset(record.time_offset) today_start = DateTime.parse("#{time_now.strftime("%Y/%m/%d")} 00:00:00#{record.time_offset}").utc today_end = DateTime.parse("#{(time_now + 1.day).strftime("%Y/%m/%d")} 00:00:00#{record.time_offset}").utc today_task_count = record.class.where(:created_at.gte=>today_start,:created_at.lt=>today_end).count record.ticket_no = "T"+time_now.strftime("%Y%m%d") + ("0000" + (today_task_count + 1).to_s)[-4..-1] end before_save do |record| unless @already_save record.user_ids = [record.owner_id.to_s] + record.helper_ids record.user_ids = record.user_ids.select{|id| id.present?} if self.ruling_timer_project_id == "private_task" time_now = DateTime.now.utc project = RulingTimerProject.where(:creator=>self.creator).first if project.nil? project = RulingTimerProject.create(:created_at=>time_now,:creator=>self.creator,:time_offset=>self.time_offset) end self.ruling_timer_project = project end end end after_save do if self.ruling_timer_project_id_change && !@already_save if self.ruling_timer_project_id_change[0] old_project = RulingTimerProject.find(self.ruling_timer_project_id_change[0]) rescue nil if old_project old_project.task_count -= 1 @already_save = true old_project.save(:validate=>false) end end new_project = self.ruling_timer_project new_project.task_count += 1 new_project.all_user_ids += self.get_all_user_ids new_project.all_user_ids.uniq @already_save = true new_project.save(:validate=>false) end if self.ruling_timer_section_id_change && !@already_save need_create_notify = true if self.ruling_timer_section_id_change[0] if self.ruling_timer_notifies.where(:type=>2,:data=>self.ruling_timer_section_id_change.reverse,:updated_at.lte=>DateTime.now - 1.hour).destroy != 0 need_create_notify = false end old_section = RulingTimerSection.find(self.ruling_timer_section_id_change[0]) rescue nil if old_section old_section.task_count -= 1 if old_section.task_count > 0 @already_save = true old_section.save(:validate=>false) end end new_section = self.ruling_timer_section new_section.task_count += 1 @already_save = true new_section.save(:validate=>false) if need_create_notify all_user_ids = self.get_all_user_ids rescue [] ruling_timer_temps = RulingTimerTemp.where(:user_id.in=>all_user_ids).to_a ruling_timer_temps.each do |temp| notify = self.ruling_timer_notifies.new(:type=>2,:user=>temp.user,:data=>self.ruling_timer_section_id_change) notify.is_hidden = false notify.ruling_timer_task = self.ruling_timer_task notify.update_user_id = self.uploader_id notify.content = self.content notify.save end end end end after_destroy do @already_save = true if self.ruling_timer_project project = self.ruling_timer_project project.task_count -= 1 project.save(:validate=>false) end end def get_all_user_ids all_user_ids = [] all_user_ids << self.owner_id.to_s if self.owner_id.present? all_user_ids += (self.helper_ids + self.observer_ids) all_user_ids.uniq end def helpers User.where(:id.in=>self.helper_ids) end def observers User.where(:id.in=>self.observer_ids) end def convert_datetime(time) if time.class == Time return time.to_datetime elsif time.class == DateTime return time elsif time.class == String return DateTime.parse(time) else return Time.at(time).to_datetime #time is seconds end end def create_time convert_datetime(self.created_at.utc).new_offset(self.time_offset).strftime("%Y/%m/%d %H:%m") end def users User.where(:id.in=>self.user_ids) end def get_priority I18n.t("ruling_timer.priority_opts.#{self.priority}") end after_save do |record| if record.user_ids_changed? delete_user_ids = record.user_ids_was.to_a - record.user_ids.to_a add_user_ids = record.user_ids.to_a - record.user_ids_was.to_a delete_user_ids.each do |user| self.ruling_timer_sub_tasks.where(:user=>user).destroy end add_user_ids.each do |user| self.ruling_timer_sub_tasks.create(:user=>user,:task_name=>task_name) end end if record.task_name_changed? self.ruling_timer_sub_tasks.each do |sub_task| sub_task.update(:task_name=>task_name) end end end end