Class: EducodeSales::Idea

Inherits:
ApplicationRecord show all
Defined in:
app/models/educode_sales/idea.rb

Instance Method Summary collapse

Instance Method Details

#assist_staffsObject



50
51
52
# File 'app/models/educode_sales/idea.rb', line 50

def assist_staffs
  Staff.where(id: self.assist_staff_ids)
end

#attachmentsObject



58
59
60
# File 'app/models/educode_sales/idea.rb', line 58

def attachments
  Attachment.where(id: self.attachment_ids)
end

#other_staffsObject



54
55
56
# File 'app/models/educode_sales/idea.rb', line 54

def other_staffs
  Staff.where(id: self.other_staff_ids)
end

#recycleObject

before_save :check_changes



34
35
36
# File 'app/models/educode_sales/idea.rb', line 34

def recycle
  self.update(deleted_at: nil, deleter_id: nil, is_deleted: false)
end

#save_history(attr, old_value, new_value) ⇒ Object

def check_changes

unless self.new_record?
  history = []
  arr = self.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 self.send("#{attr}_changed?")
      old_value, new_value = self.send("#{attr}_was"), self.send(attr)
      res = save_history(attr, old_value, new_value)
      history << res if res
    end
  end
  self.idea_histories.create(content: history, staff: current_user)
end

end



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/educode_sales/idea.rb', line 81

def save_history(attr, old_value, new_value)
  case attr
  when "school_id"
    old_value = School.find_by(id: old_value)&.name
    new_value = School.find_by(id: new_value)&.name
    "学校/单位由“#{old_value}”变更为“#{new_value}"
  when "name"
    "项目名称由“#{old_value}”变更为“#{new_value}"
  when "level"
    "优先级由“#{old_value}”变更为“#{new_value}"
  when "staff_id"
    old_value = Staff.find_by(id: old_value)&.user&.real_name
    new_value = Staff.find_by(id: new_value)&.user&.real_name
    "方案经理由“#{old_value}”变更为“#{new_value}"
  when "status"
    "状态由“#{old_value}”变更为“#{new_value}"
  when "types"
    "类型由“#{old_value}”变更为“#{new_value}"
  when "model"
    "部署模式由“#{old_value}”变更为“#{new_value}"
  when "hardware"
    "硬件规模(万)由“#{old_value}”变更为“#{new_value}"
  when "project"
    "并发规模(人)由“#{old_value}”变更为“#{new_value}"
  when "money"
    "项目预算(万)由“#{old_value}”变更为“#{new_value}"
  when "end_time"
    "截止时间由“#{old_value}”变更为“#{new_value}"
  when "content"
    "需求说明由“#{old_value}”变更为“#{new_value}"
  when "department_id"
    old_value = Department.find_by(id: old_value)&.name
    new_value = Department.find_by(id: new_value)&.name
    "部门由“#{old_value}”变更为“#{new_value}"
  when "manager_name"
    "单位联系人由“#{old_value}”变更为“#{new_value}"
  when "manager_phone"
    "负责人电话由“#{old_value}”变更为“#{new_value}"
  else
    nil
  end
end

#soft_destroy(staff_id) ⇒ Object



62
63
64
# File 'app/models/educode_sales/idea.rb', line 62

def soft_destroy(staff_id)
  self.update(deleted_at: Time.now, deleter_id: staff_id, is_deleted: true)
end

#sync_dataObject



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/educode_sales/idea.rb', line 38

def sync_data
  # {"content"=>"123", "idea_id"=>15, "school_id"=>nil, "money"=>123.0,  "advise"=>"123", "sale_staff_id"=>8, "idea_staff_id"=>12, "staff_id"=>2, "comment"=>nil, "created_at"=>Mon, 01 May 2023 11:12:48 CST +08:00, "updated_at"=>Mon, 01 Ma023 11:14:06 CST +08:00}
  # {"school_id"=>38739, "name"=>"java项目", "level"=>"高", "staff_id"=>24,"types"=>"低定制", "model"=>"本地版", "hardware"=>111.011, "project"=>333.011, "money"=>222.01, "creator_id"=>2, "end_time"=>Sat, 08 Apr 2023, "history_type"=>n"=>"4441111", "deleter_id"=>nil, "is_deleted"=>false, "deleted_at"=>nil, "department_id"=>35794, "manager_name"=>"王旭11111", "manager_phone"=>"131011111111111", "history_record"=>nil, "created_at"=>Thu, 23 Mar 2023 11:27:22 CST +08:00, "updated_at"=>Tue, 04 Jul 20217:57:32 CST +08:00, "school_name"=>"湖南科技大学1111", "department_name"=>"计算机科学与技术11111", "sale_staff_id"=>53, "idea_staff_id"=>nil, "assist_staff_ids"=>["10"], "attachment_id"=>nil, "attachment_ids"=>["4390068"], "idea_type"=>"建设方案", "other_staff_ids" "12", "13", "14", "16", "17", "18"], "idea_follows_count"=>2, "last_idea_follow_id"=>11, "advise"=>"123123", "business_id"=>nil}

  last_follow = self.idea_follows.order(created_at: :desc).first
  if last_follow
    self.update(status: last_follow.status, last_idea_follow_id: last_follow.id, staff_id: last_follow.idea_staff_id, sale_staff_id: last_follow.sale_staff_id)
  else
    self.update(last_idea_follow_id: nil)
  end
end