source: branches/feature-module-update/data/class/batch/SC_Batch_Daily.php @ 15896

Revision 15896, 14.0 KB checked in by nanasess, 15 years ago (diff)

require_once 追加

  • Property svn:keywords set to "Id Revision Date"
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8// {{{ requires
9require_once(CLASS_PATH . "batch/SC_Batch.php");
10
11/**
12 * 日次バッチクラス.
13 *
14 * XXX Singleton にするべき...
15 *
16 * @package Batch
17 * @author LOCKON CO.,LTD.
18 * @version $Id$
19 */
20class SC_Batch_Daily extends SC_Batch {
21
22
23    /**
24     * コンストラクタ.
25     *
26     * @param array $argv コマンドライン用引数.
27     *                    指定しない場合は, 内部関数が実行されない.
28     */
29    function SC_Batch_Daily($argv = "") {
30
31        if (!empty($argv)) {
32            $this->execute($argv);
33        }
34    }
35
36    /**
37     * バッチ処理を実行する.
38     *
39     * @param mixed $argv コマンドライン引数
40     * @return void
41     */
42    function execute($argv = "") {
43        $term = 0;
44        $start = 1; // 集計期間は、$start~$termの間となる。通常前日分から。
45        $command = false;
46
47        // 集計対象期間の取得(指定日分さかのぼる)
48        if (SC_Utils_Ex::sfIsInt($argv[1]) && $argv[1] <= 365) {
49            $term = $argv[1];
50            $command = true;
51        }
52
53        // 集計開始日
54        if (SC_Utils_Ex::sfIsInt($argv[2]) && $argv[2] <= 365) {
55            $start = $argv[2];
56            $command = true;
57        }
58
59        if($term > 0) {
60            // 集計の開始
61            $this->lfStartDailyTotal($term, $start, $command);
62        }
63    }
64
65    // 集計の開始
66    function lfStartDailyTotal($term, $start, $command = false) {
67
68        $now_time = time();
69
70        // グラフ画像の削除
71        $path = GRAPH_DIR . "*.png";
72        system ("rm -rf $path");
73
74        // 削除された受注データの受注詳細情報の削除
75        $objQuery = new SC_Query();
76        $where = "order_id IN (SELECT order_id FROM dtb_order WHERE del_flg = 1)";
77        $objQuery->delete("dtb_order_detail", $where);
78
79        // 最後に更新された日付を取得
80        $ret = $objQuery->max("dtb_bat_order_daily", "create_date");
81        list($batch_last) = split("\.", $ret);
82        $pass = $now_time - strtotime($batch_last);
83
84        // 最後のバッチ実行からLOAD_BATCH_PASS秒経過していないと実行しない。
85        if($pass < LOAD_BATCH_PASS) {
86            GC_Utils_Ex::gfPrintLog("LAST BATCH " . $arrRet[0]['create_date'] . " > " . $batch_pass . " -> EXIT BATCH $batch_date");
87            return;
88        }
89
90        // 集計
91        for ($i = $start; $i < $term; $i++) {
92            // 基本時間から$i日分さかのぼる
93            $tmp_time = $now_time - ($i * 24 * 3600);
94
95            $batch_date = date("Y/m/d", $tmp_time);
96            GC_Utils_Ex::gfPrintLog("LOADING BATCH $batch_date");
97
98            $this->lfBatOrderDaily($tmp_time);
99            $this->lfBatOrderDailyHour($tmp_time);
100            $this->lfBatOrderAge($tmp_time);
101
102            // タイムアウトを防ぐ
103            SC_Utils_Ex::sfFlush();
104        }
105    }
106
107    // リアルタイムで集計を実施する。集計が終了しているレコードは実施しない。
108    /*
109     $sdate:YYYY-MM-DD hh:mm:ss形式の日付
110     $edate:YYYY-MM-DD hh:mm:ss形式の日付
111    */
112    function lfRealTimeDailyTotal($sdate, $edate) {
113        $pass = strtotime($edate) - strtotime($sdate);
114        $loop = intval($pass / 86400);
115
116        for($i = 0; $i <= $loop; $i++) {
117            $tmp_time = strtotime($sdate) + ($i * 86400);
118            $batch_date = date("Y/m/d H:i:s", $tmp_time);
119            $objQuery = new SC_Query();
120            $arrRet = $objQuery->select("order_date, create_date", "dtb_bat_order_daily", "order_date = ?", array($batch_date));
121            // すでにバッチ処理が終了しているかチェックする。
122            if(count($arrRet) > 0) {
123                list($create_date) = split("\.", $arrRet[0]['create_date']);
124                list($order_date) = split("\.", $arrRet[0]['order_date']);
125                $create_time = strtotime($create_date);
126                $order_time = strtotime($order_date);
127                // オーダー開始日より一日以上後に集計されている場合は集計しなおさない
128                if($order_time + 86400 < $create_time || $tmp_time > time()) {
129                    GC_Utils_Ex::gfPrintLog("EXIT BATCH $batch_date $tmp_time" . " " . time());
130                    continue;
131                }
132            }
133            GC_Utils_Ex::gfPrintLog("LOADING BATCH $batch_date");
134            $this->lfBatOrderDaily($tmp_time);
135            $this->lfBatOrderDailyHour($tmp_time);
136            $this->lfBatOrderAge($tmp_time);
137        }
138    }
139
140    // バッチ集計用のSQL文を取得する。
141    function lfGetOrderDailySQL($start, $end) {
142        $from = " FROM dtb_order AS T1 LEFT JOIN dtb_customer AS T2 USING ( customer_id ) ";
143        $where = " WHERE T1.del_flg = 0 AND T1.status <> " . ORDER_CANCEL . " AND T1.create_date BETWEEN '$start' AND '$end' ";
144
145        /*  mysqlでも問題ないように修正
146         $sql = "SELECT ";
147         $sql.= "COUNT(*) AS total_order, ";
148         $sql.= "SUM((SELECT COUNT(*) WHERE customer_id = 0)) AS nonmember, ";
149         $sql.= "SUM((SELECT COUNT(*) WHERE customer_id <> 0 GROUP BY customer_id)) AS member, ";
150         $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1)) AS men, ";
151         $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2)) AS women, ";
152         $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1 AND customer_id <> 0)) AS men_member, ";
153         $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1 AND customer_id = 0)) AS men_nonmember, ";
154         $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2 AND customer_id <> 0)) AS women_member, ";
155         $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2 AND customer_id = 0)) AS women_nonmember, ";
156         $sql.= "SUM(total) AS total, ";
157         $sql.= "int8(AVG(total)) AS total_average ";
158         $sql.= $from;
159         $sql.= $where;     // 受注作成日で検索する
160        */
161        $sql = "SELECT ";
162        $sql.= "COUNT(*) AS total_order, ";
163        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.customer_id = 0) AS nonmember ) AS nonmember, ";
164        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.customer_id <> 0) AS member ) AS member, ";
165        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 1) AS men ) AS men, ";
166        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 2) AS women ) AS women, ";
167        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 1 AND T2.customer_id <> 0) AS men_member ) AS men_member, ";
168        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 1 AND T1.customer_id = 0) AS men_nonmember ) AS men_nonmember, ";
169        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 2 AND T2.customer_id <> 0) AS women_member ) AS women_member, ";
170        $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 2 AND T1.customer_id = 0) AS women_nonmember ) AS women_nonmember, ";
171        $sql.= "SUM(total) AS total, ";
172        $sql.= "(AVG(total)) AS total_average ";
173        $sql.= $from;
174        $sql.= $where;      // 受注作成日で検索する
175
176        return $sql;
177    }
178
179    // 売上げ集計バッチ処理(日別)
180    function lfBatOrderDaily($time) {
181        $masterData = new SC_DB_MasterData_Ex();
182        $arrWDAY = $masterData->getMasterData("mtb_wday");
183        // 集計対象日を取得する
184        $date = date("Y-m-d", $time);
185
186        $start = $date . " 00:00:00";
187        $end = $date . " 23:59:59";
188
189        $sql = $this->lfGetOrderDailySQL($start,$end);
190
191        $objQuery = new SC_Query();
192        $arrRet = $objQuery->getall($sql);
193
194        $sqlval = $arrRet[0];
195
196        // 空文字を"0"に変換
197        foreach($sqlval as $key => $val) {
198            if ($val == "") {
199                $sqlval[$key] = "0";
200            }
201        }
202
203        $sqlval['create_date'] = 'now()';
204        $sqlval['order_date'] = $start;
205        $sqlval['year'] = date("Y", $time);
206        $sqlval['month'] = date("m", $time);
207        $sqlval['day'] = date("d", $time);
208        $sqlval['wday'] = date("w", $time);
209        $sqlval['key_day'] = sprintf("%02d/%02d/%02d", substr($sqlval['year'],2), $sqlval['month'], $sqlval['day']);
210        $sqlval['key_month'] = sprintf("%02d/%02d", substr($sqlval['year'],2), $sqlval['month']);
211        $sqlval['key_year'] = sprintf("%d", $sqlval['year']);
212        $sqlval['key_wday'] = sprintf("%s", $arrWDAY[$sqlval['wday']]);
213
214        $objQuery->delete("dtb_bat_order_daily", "order_date = ?", array($start));
215        $objQuery->insert("dtb_bat_order_daily", $sqlval);
216    }
217
218    // 売上げ集計バッチ処理(時間別)
219    function lfBatOrderDailyHour($time) {
220
221        // 集計対象日を取得する
222        $date = date("Y-m-d", $time);
223        $objQuery = new SC_Query();
224
225        $start = $date . " 00:00:00";
226        $objQuery->delete("dtb_bat_order_daily_hour", "order_date = ?", array($start));
227
228        // 1時間毎に集計する。
229        for($i = 0; $i < 24; $i++) {
230            $sdate = sprintf("%s %02d:00:00", $date, $i);
231            $edate = sprintf("%s %02d:59:59", $date, $i);
232            $sql = $this->lfGetOrderDailySQL($sdate, $edate);
233            $arrRet = $objQuery->getall($sql);
234            $sqlval = $arrRet[0];
235            // 空文字を"0"に変換
236            foreach($sqlval as $key => $val) {
237                if ($val == "") {
238                    $sqlval[$key] = "0";
239                }
240            }
241            $sqlval['create_date'] = "now()";
242            $sqlval['order_date'] = $start;
243            $sqlval['hour'] = "$i";
244            $objQuery->insert("dtb_bat_order_daily_hour", $sqlval);
245        }
246    }
247
248    // 売上げ集計バッチ処理(年齢別)
249    function lfBatOrderAge($time) {
250
251        $age_loop = intval(BAT_ORDER_AGE / 10);
252
253        // 年齢の範囲を指定してデータ抽出
254        $sql = "SELECT COUNT(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average ";
255        $sql.= "FROM dtb_order ";
256
257        // 集計対象日を取得する
258        $date = date("Y-m-d", $time);
259
260        $start = $date . " 00:00:00";
261        $end = $date . " 23:59:59";
262
263        $objQuery = new SC_Query();
264        $objQuery->delete("dtb_bat_order_daily_age", "order_date = ?", array($start));
265
266        /* 会員集計 */
267
268        $base_where = "WHERE (create_date BETWEEN ? AND ?) AND customer_id <> 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
269
270        $end_date = date("Y/m/d", time());
271        $start_date = date("Y/m/d",strtotime("-10 year" ,strtotime($end_date)));
272        $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($end_date)));
273        // 年齢毎に集計する。
274        for($i = 0; $i <= $age_loop; $i++) {
275            $where = $base_where . " AND order_birth >= cast('$start_date' as date)";
276            $start_age = $i * 10;
277            if($i < $age_loop) {
278                $end_age = $start_age+9;
279                $where = $where . " AND order_birth < cast('$end_date' as date)";
280            }else{
281                $where = $base_where . " AND order_birth < cast('$end_date' as date)";
282                $end_age = 999;
283            }
284            $this->lfBatOrderAgeSub($sql . $where, $start, $end, $start_age, $end_age, 1);
285            $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($start_date)));
286            $start_date = date("Y/m/d",strtotime("-10 year",strtotime($start_date)));
287        }
288
289        // 誕生日入力なし
290        $where = $base_where . " AND order_birth IS NULL ";
291        $this->lfBatOrderAgeSub($sql . $where, $start, $end, NULL, NULL, 1);
292
293        /* 非会員集計 */
294
295        $base_where = "WHERE (create_date BETWEEN ? AND ?) AND customer_id = 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
296        $where = $base_where . " AND (to_number(to_char(age(current_timestamp, order_birth), 'YYY'), 999) BETWEEN ? AND ?) ";
297
298        $end_date = date("Y/m/d", time());
299        $start_date = date("Y/m/d",strtotime("-10 year" ,strtotime($end_date)));
300        $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($end_date)));
301        // 年齢毎に集計する。
302        for($i = 0; $i <= $age_loop; $i++) {
303            $where = $base_where . " AND order_birth >= cast('$start_date' as date)";
304            $start_age = $i * 10;
305            if($i < $age_loop) {
306                $end_age = $start_age+9;
307                $where = $where . " AND order_birth < cast('$end_date' as date)";
308            }else{
309                $where = $base_where . " AND order_birth < cast('$end_date' as date)";
310                $end_age = 999;
311            }
312            $this->lfBatOrderAgeSub($sql . $where, $start, $end, $start_age, $end_age, 0);
313            $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($start_date)));
314            $start_date = date("Y/m/d",strtotime("-10 year",strtotime($start_date)));
315        }
316
317        // 誕生日入力なし
318        $where = $base_where . " AND order_birth IS NULL AND del_flg = 0";
319        $this->lfBatOrderAgeSub($sql . $where, $start, $end, NULL, NULL, 0);
320    }
321
322    // 売上げ集計バッチ処理(年齢別) 登録部分
323    function lfBatOrderAgeSub($sql, $start, $end, $start_age, $end_age, $member) {
324        $objQuery = new SC_Query();
325
326        $arrRet = $objQuery->getall($sql, array($start, $end));
327        $sqlval = $arrRet[0];
328
329        // 空文字を"0"に変換
330        foreach($sqlval as $key => $val) {
331            if ($val == "") {
332                $sqlval[$key] = "0";
333            }
334        }
335
336        $sqlval['create_date'] = "now()";
337        $sqlval['order_date'] = $start;
338        $sqlval['start_age'] = "$start_age";
339        $sqlval['end_age'] = "$end_age";
340        $sqlval['member'] = "$member";
341
342        $objQuery->insert("dtb_bat_order_daily_age", $sqlval);
343    }
344
345    // 文字列にSingleQuotationを付与する
346    function lfSingleQuot($value){
347        $ret = "";
348        if (DB_TYPE == "mysql") {
349            $ret = $value;
350        }else{
351            $ret = "'" . $value . "'";
352        }
353        return $ret;
354    }
355}
356?>
Note: See TracBrowser for help on using the repository browser.