Class: EducodeSales::ReturnForecastService

Inherits:
Object
  • Object
show all
Defined in:
app/services/return_forecast_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, request, params) ⇒ ReturnForecastService

ReturnForecastService.new(current_user, request, params).call



5
6
7
8
9
# File 'app/services/return_forecast_service.rb', line 5

def initialize(user, request, params)
  @user = user
  @request = request
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'app/services/return_forecast_service.rb', line 3

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'app/services/return_forecast_service.rb', line 3

def request
  @request
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'app/services/return_forecast_service.rb', line 3

def user
  @user
end

Instance Method Details

#callObject



11
12
# File 'app/services/return_forecast_service.rb', line 11

def call
end

#return_forecast_month(labels, selects, staff_id = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/services/return_forecast_service.rb', line 107

def return_forecast_month(labels, selects, staff_id = nil)
  plan_get = plan_get(staff_id, labels, "month")
  actual_get = actual_get(staff_id, labels, "month")
  datasets = selects.map do |select|
    if select == "计划投标额"
      {
        label: select,
        data: labels.map do |d|
          month = d.split("-").last.to_i
          year = d.split("-").first.to_i
          plan_get[[year, month]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f
        end,
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
        ],
        borderColor: [
          'rgba(255,99,132,1)',
        ],
        borderWidth: 1
      }
    else
      {
        label: select,
        data: labels.map do |d|
          month = d.split("-").last.to_i
          year = d.split("-").first.to_i
          actual_get[[year, month]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f
        end,
        backgroundColor: [
          'rgba(54, 162, 235, 0.2)',
        ],
        borderColor: [
          'rgba(54, 162, 235, 1)',
        ],
        borderWidth: 1
      }
    end
  end

  {
    labels: labels,
    datasets: datasets
  }
end

#return_forecast_quarter(labels, selects, staff_id = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/return_forecast_service.rb', line 14

def return_forecast_quarter(labels, selects, staff_id = nil)
  plan_get = plan_get(staff_id, labels, "quarter")
  actual_get = actual_get(staff_id, labels, "quarter")
  datasets = selects.map do |select|
    if select == "计划投标额"
      {
        label: select,
        data: labels.map do |d|
          quarter = d.split("-").last.to_i
          year = d.split("-").first.to_i
          plan_get[[year, quarter]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f
        end,
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
        ],
        borderColor: [
          'rgba(255,99,132,1)',
        ],
        borderWidth: 1
      }
    else
      {
        label: select,
        data: labels.map do |d|
          quarter = d.split("-").last.to_i
          year = d.split("-").first.to_i
          actual_get[[year, quarter]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f
        end,
        backgroundColor: [
          'rgba(54, 162, 235, 0.2)',
        ],
        borderColor: [
          'rgba(54, 162, 235, 1)',
        ],
        borderWidth: 1
      }
    end
  end

  {
    labels: labels,
    datasets: datasets
  }
end

#return_forecast_week(labels, selects, staff_id = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
# File 'app/services/return_forecast_service.rb', line 59

def return_forecast_week(labels, selects, staff_id = nil)

  plan_get = plan_get(staff_id, labels, "week")
  actual_get = actual_get(staff_id, labels, "week")
  datasets = selects.map do |select|
    if select == "计划投标额"
      {
        label: select,
        data: labels.map do |d|
          week = d.split("-").last
          year = d.split("-").first
          yearweek = "#{year}#{week}".to_i
          plan_get[yearweek].to_f
        end,
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
        ],
        borderColor: [
          'rgba(255,99,132,1)',
        ],
        borderWidth: 1
      }
    else
      {
        label: select,
        data: labels.map do |d|
          week = d.split("-").last
          year = d.split("-").first
          yearweek = "#{year}#{week}".to_i
          actual_get[yearweek].to_f
        end,
        backgroundColor: [
          'rgba(54, 162, 235, 0.2)',
        ],
        borderColor: [
          'rgba(54, 162, 235, 1)',
        ],
        borderWidth: 1
      }
    end
  end

  {
    labels: labels,
    datasets: datasets
  }
end

#return_forecast_year(labels, selects, staff_id = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/services/return_forecast_service.rb', line 152

def return_forecast_year(labels, selects, staff_id = nil)
  plan_get = plan_get(staff_id, labels, "year")
  actual_get = actual_get(staff_id, labels, "year")
  datasets = selects.map do |select|
    if select == "计划投标额"
      {
        label: select,
        data: labels.map { |d| plan_get[d.to_i] || 0 },
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
        ],
        borderColor: [
          'rgba(255,99,132,1)',
        ],
        borderWidth: 1
      }
    else
      {
        label: select,
        data: labels.map { |d| actual_get[d.to_i] || 0 },
        backgroundColor: [
          'rgba(54, 162, 235, 0.2)',
        ],
        borderColor: [
          'rgba(54, 162, 235, 1)',
        ],
        borderWidth: 1
      }
    end
  end

  {
    labels: labels,
    datasets: datasets
  }
end