]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store_tariffs.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Tariffs manipulation methods
23  *
24  *  $Revision: 1.5 $
25  *  $Date: 2007/12/23 13:39:59 $
26  *
27  */
28
29 #include "firebird_store.h"
30
31 #include "stg/ibpp.h"
32 #include "stg/tariff.h"
33 #include "stg/tariff_conf.h"
34 #include "stg/common.h"
35
36 #include <cmath>
37
38 namespace
39 {
40
41 const int pt_mega = 1024 * 1024;
42
43 }
44
45 //-----------------------------------------------------------------------------
46 int FIREBIRD_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
47 {
48 std::lock_guard lock(m_mutex);
49
50 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
51 IBPP::Statement st = IBPP::StatementFactory(db, tr);
52
53 try
54     {
55     tr->Start();
56     st->Execute("select name from tb_tariffs");
57     while (st->Fetch())
58         {
59         std::string name;
60         st->Get(1, name);
61         tariffsList->push_back(name);
62         }
63     tr->Commit();
64     }
65
66 catch (IBPP::Exception & ex)
67     {
68     tr->Rollback();
69     strError = "IBPP exception";
70     printfd(__FILE__, ex.what());
71     return -1;
72     }
73
74 return 0;
75 }
76 //-----------------------------------------------------------------------------
77 int FIREBIRD_STORE::AddTariff(const std::string & name) const
78 {
79 std::lock_guard lock(m_mutex);
80
81 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
82 IBPP::Statement st = IBPP::StatementFactory(db, tr);
83
84 try
85     {
86     tr->Start();
87     st->Prepare("execute procedure sp_add_tariff(?, ?)");
88     st->Set(1, name);
89     st->Set(2, DIR_NUM);
90     st->Execute();
91     tr->Commit();
92     }
93
94 catch (IBPP::Exception & ex)
95     {
96     tr->Rollback();
97     strError = "IBPP exception";
98     printfd(__FILE__, ex.what());
99     return -1;
100     }
101
102 return 0;
103 }
104 //-----------------------------------------------------------------------------
105 int FIREBIRD_STORE::DelTariff(const std::string & name) const
106 {
107 std::lock_guard lock(m_mutex);
108
109 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
110 IBPP::Statement st = IBPP::StatementFactory(db, tr);
111
112 try
113     {
114     tr->Start();
115     st->Prepare("execute procedure sp_delete_tariff(?)");
116     st->Set(1, name);
117     st->Execute();
118     tr->Commit();
119     }
120
121 catch (IBPP::Exception & ex)
122     {
123     tr->Rollback();
124     strError = "IBPP exception";
125     printfd(__FILE__, ex.what());
126     return -1;
127     }
128
129 return 0;
130 }
131 //-----------------------------------------------------------------------------
132 int FIREBIRD_STORE::SaveTariff(const STG::TariffData & td,
133                                const std::string & tariffName) const
134 {
135 std::lock_guard lock(m_mutex);
136
137 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
138 IBPP::Statement st = IBPP::StatementFactory(db, tr);
139
140 try
141     {
142     tr->Start();
143     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
144     st->Set(1, tariffName);
145     st->Execute();
146     if (!st->Fetch())
147     {
148     tr->Rollback();
149     strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
150     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
151     return -1;
152     }
153     int32_t id;
154     st->Get(1, id);
155     st->Close();
156
157     std::string query = "update tb_tariffs set \
158                             fee = ?, \
159                             free = ?, \
160                             passive_cost = ?, \
161                             traff_type = ?";
162
163     if (schemaVersion > 0)
164         query += ", period = ?";
165     if (schemaVersion > 1)
166         query += ", change_policy = ?, \
167                     change_policy_timeout = ?";
168
169     query += " where pk_tariff = ?";
170
171     unsigned num = 5;
172     st->Prepare(query);
173     st->Set(1, td.tariffConf.fee);
174     st->Set(2, td.tariffConf.free);
175     st->Set(3, td.tariffConf.passiveCost);
176     st->Set(4, td.tariffConf.traffType);
177
178     if (schemaVersion > 0)
179         {
180         st->Set(5, STG::Tariff::toString(td.tariffConf.period));
181         ++num;
182         }
183
184     if (schemaVersion > 1)
185         {
186         st->Set(6, STG::Tariff::toString(td.tariffConf.changePolicy));
187         IBPP::Timestamp policyTimeout;
188         time_t2ts(td.tariffConf.changePolicyTimeout, &policyTimeout);
189         st->Set(7, policyTimeout);
190         num += 2;
191         }
192
193     st->Set(num, id);
194     st->Execute();
195     st->Close();
196
197     IBPP::Time tb;
198     IBPP::Time te;
199
200     for(int i = 0; i < DIR_NUM; i++)
201         {
202
203         tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
204         te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
205
206         double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
207         double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
208         double pna = 0;
209         double pnb = 0;
210
211         if (td.dirPrice[i].singlePrice)
212             {
213             pna = pda;
214             pnb = pdb;
215             }
216         else
217             {
218             pna = td.dirPrice[i].priceNightA;
219             pnb = td.dirPrice[i].priceNightB;
220             }
221
222         int threshold = 0;
223         if (td.dirPrice[i].noDiscount)
224             {
225             threshold = 0xffFFffFF;
226             }
227         else
228             {
229             threshold = td.dirPrice[i].threshold;
230             }
231
232         st->Prepare("update tb_tariffs_params set \
233                 price_day_a = ?, \
234                 price_day_b = ?, \
235                 price_night_a = ?, \
236                 price_night_b = ?, \
237                 threshold = ?, \
238                 time_day_begins = ?, \
239                 time_day_ends = ? \
240                 where fk_tariff = ? and dir_num = ?");
241         st->Set(1, pda);
242         st->Set(2, pdb);
243         st->Set(3, pna);
244         st->Set(4, pnb);
245         st->Set(5, threshold);
246         st->Set(6, tb);
247         st->Set(7, te);
248         st->Set(8, id);
249         st->Set(9, i);
250         st->Execute();
251         st->Close();
252         }
253     tr->Commit();
254     }
255
256 catch (IBPP::Exception & ex)
257     {
258     tr->Rollback();
259     strError = "IBPP exception";
260     printfd(__FILE__, ex.what());
261     return -1;
262     }
263
264 return 0;
265 }
266 //-----------------------------------------------------------------------------
267 int FIREBIRD_STORE::RestoreTariff(STG::TariffData * td,
268                                   const std::string & tariffName) const
269 {
270 std::lock_guard lock(m_mutex);
271
272 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
273 IBPP::Statement st = IBPP::StatementFactory(db, tr);
274
275
276 td->tariffConf.name = tariffName;
277
278 try
279     {
280     tr->Start();
281     st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
282     st->Set(1, tariffName);
283     st->Execute();
284     if (!st->Fetch())
285         {
286         strError = "Tariff \"" + tariffName + "\" not found in database";
287     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
288         tr->Rollback();
289         return -1;
290         }
291     int32_t id;
292     st->Get(1, id);
293     st->Get(3, td->tariffConf.fee);
294     st->Get(4, td->tariffConf.free);
295     st->Get(5, td->tariffConf.passiveCost);
296     td->tariffConf.traffType = STG::Tariff::fromInt(Get<int>(st, 6));
297     if (schemaVersion > 0)
298         td->tariffConf.period = STG::Tariff::parsePeriod(Get<std::string>(st, 7));
299     if (schemaVersion > 1)
300         {
301         td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(Get<std::string>(st, 8));
302         td->tariffConf.changePolicyTimeout = ts2time_t(Get<IBPP::Timestamp>(st, 9));
303         }
304     st->Close();
305     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
306     st->Set(1, id);
307     st->Execute();
308     int i = 0;
309     while (st->Fetch())
310     {
311     i++;
312     if (i > DIR_NUM)
313         {
314         strError = "Too mach params for tariff \"" + tariffName + "\"";
315         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
316         tr->Rollback();
317         return -1;
318         }
319     int16_t dir;
320     st->Get(3, dir);
321     st->Get(4, td->dirPrice[dir].priceDayA);
322     td->dirPrice[dir].priceDayA /= 1024*1024;
323     st->Get(5, td->dirPrice[dir].priceDayB);
324     td->dirPrice[dir].priceDayB /= 1024*1024;
325     st->Get(6, td->dirPrice[dir].priceNightA);
326     td->dirPrice[dir].priceNightA /= 1024*1024;
327     st->Get(7, td->dirPrice[dir].priceNightB);
328     td->dirPrice[dir].priceNightB /= 1024*1024;
329     st->Get(8, td->dirPrice[dir].threshold);
330     if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
331         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
332         {
333         td->dirPrice[dir].singlePrice = true;
334         }
335     else
336         {
337         td->dirPrice[dir].singlePrice = false;
338         }
339     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
340         {
341         td->dirPrice[dir].noDiscount = true;
342         }
343     else
344         {
345
346         td->dirPrice[dir].noDiscount = false;
347         }
348     IBPP::Time tb;
349     st->Get(9, tb);
350     IBPP::Time te;
351     st->Get(10, te);
352     int h, m, s;
353     tb.GetTime(h, m, s);
354     td->dirPrice[dir].hDay = h;
355     td->dirPrice[dir].mDay = m;
356     te.GetTime(h, m, s);
357     td->dirPrice[dir].hNight = h;
358     td->dirPrice[dir].mNight = m;
359     }
360     tr->Commit();
361     }
362
363 catch (IBPP::Exception & ex)
364     {
365     tr->Rollback();
366     strError = "IBPP exception";
367     printfd(__FILE__, ex.what());
368     return -1;
369     }
370
371 return 0;
372 }
373 //-----------------------------------------------------------------------------
374