source: branches/dev/html/admin/batch/daily.php @ 13272

Revision 13272, 10.7 KB checked in by naka, 15 years ago (diff)
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7$daily_php_dir = realpath(dirname( __FILE__));
8require_once($daily_php_dir . "/../require.php");
9
10$term = 0;
11$start = 1; // ½¸·×´ü´Ö¤Ï¡¢$start¢·$term¤Î´Ö¤È¤Ê¤ë¡£Ä̾ïÁ°Æüʬ¤«¤é¡£
12$command = false;
13
14// ½¸·×Âоݴü´Ö¤Î¼èÆÀ¡Ê»ØÄêÆüʬ¤µ¤«¤Î¤Ü¤ë)
15if (sfIsInt($argv[1]) && $argv[1] <= 365) {
16    $term = $argv[1];
17    $command = true;
18}
19
20// ½¸·×³«»ÏÆü
21if (sfIsInt($argv[2]) && $argv[2] <= 365) {
22    $start = $argv[2];
23    $command = true;
24}
25
26if($term > 0) {
27    // ½¸·×¤Î³«»Ï
28    lfStartDailyTotal($term, $start, $command);
29}
30
31// ½¸·×¤Î³«»Ï
32function lfStartDailyTotal($term, $start, $command = false) {
33       
34    $now_time = time();
35       
36    // ¥°¥é¥Õ²èÁü¤Îºï½ü
37    $path = GRAPH_DIR . "*.png";
38    system ("rm -rf $path");
39   
40    // ºï½ü¤µ¤ì¤¿¼õÃí¥Ç¡¼¥¿¤Î¼õÃí¾ÜºÙ¾ðÊó¤Îºï½ü
41    $objQuery = new SC_Query();
42    $where = "order_id IN (SELECT order_id FROM dtb_order WHERE del_flg = 1)";
43    $objQuery->delete("dtb_order_detail", $where);
44   
45    // ºÇ¸å¤Ë¹¹¿·¤µ¤ì¤¿ÆüÉÕ¤ò¼èÆÀ
46    $ret = $objQuery->max("dtb_bat_order_daily", "create_date");
47    list($batch_last) = split("\.", $ret);
48    $pass = $now_time - strtotime($batch_last);
49       
50    // ºÇ¸å¤Î¥Ð¥Ã¥Á¼Â¹Ô¤«¤éLOAD_BATCH_PASSÉ÷вᤷ¤Æ¤¤¤Ê¤¤¤È¼Â¹Ô¤·¤Ê¤¤¡£
51    if($pass < LOAD_BATCH_PASS) {
52        gfPrintLog("LAST BATCH " . $arrRet[0]['create_date'] . " > " . $batch_pass . " -> EXIT BATCH $batch_date");
53        return;
54    }
55
56    // ½¸·×
57    for ($i = $start; $i < $term; $i++) {
58        // ´ðËÜ»þ´Ö¤«¤é$iÆüʬ¤µ¤«¤Î¤Ü¤ë
59        $tmp_time = $now_time - ($i * 24 * 3600);
60               
61        $batch_date = date("Y/m/d", $tmp_time);
62        gfPrintLog("LOADING BATCH $batch_date");
63                   
64        lfBatOrderDaily($tmp_time);
65        lfBatOrderDailyHour($tmp_time);
66        lfBatOrderAge($tmp_time);
67       
68        // ¥Ö¥é¥¦¥¶¤«¤é¤Î¼Â¹Ô¤Î¾ì¹ç
69        if(!$command) {
70            // ¥¿¥¤¥à¥¢¥¦¥È¤òËɤ°
71            sfFlush();
72        }
73    }
74}
75
76// ¥ê¥¢¥ë¥¿¥¤¥à¤Ç½¸·×¤ò¼Â»Ü¤¹¤ë¡£½¸·×¤¬½ªÎ»¤·¤Æ¤¤¤ë¥ì¥³¡¼¥É¤Ï¼Â»Ü¤·¤Ê¤¤¡£
77/*
78    $sdate:YYYY-MM-DD hh:mm:ss·Á¼°¤ÎÆüÉÕ
79    $edate:YYYY-MM-DD hh:mm:ss·Á¼°¤ÎÆüÉÕ
80*/
81function lfRealTimeDailyTotal($sdate, $edate) {
82    $pass = strtotime($edate) - strtotime($sdate);
83    $loop = intval($pass / 86400);
84   
85    for($i = 0; $i <= $loop; $i++) {
86        $tmp_time = strtotime($sdate) + ($i * 86400);
87        $batch_date = date("Y/m/d H:i:s", $tmp_time);
88        $objQuery = new SC_Query();
89        $arrRet = $objQuery->select("order_date, create_date", "dtb_bat_order_daily", "order_date = ?", array($batch_date));
90        // ¤¹¤Ç¤Ë¥Ð¥Ã¥Á½èÍý¤¬½ªÎ»¤·¤Æ¤¤¤ë¤«¥Á¥§¥Ã¥¯¤¹¤ë¡£
91        if(count($arrRet) > 0) {
92            list($create_date) = split("\.", $arrRet[0]['create_date']);
93            list($order_date) = split("\.", $arrRet[0]['order_date']);
94            $create_time = strtotime($create_date);
95            $order_time = strtotime($order_date);
96            // ½¸·×³«»ÏÆü¤è¤ê1Æü°Ê¾å¸å¤Ë½¸·×¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï½¸·×¤·¤Ê¤ª¤µ¤Ê¤¤
97            if($order_time + 86400 < $create_time || $tmp_time > time()) {
98                gfPrintLog("EXIT BATCH $batch_date $tmp_time" . " " . time());
99                continue;
100            }
101        }
102        gfPrintLog("LOADING BATCH $batch_date");
103        lfBatOrderDaily($tmp_time);
104        lfBatOrderDailyHour($tmp_time);
105        lfBatOrderAge($tmp_time);
106    }
107}
108
109// ¥Ð¥Ã¥Á½¸·×ÍѤÎSQLʸ¤ò¼èÆÀ¤¹¤ë¡£
110function lfGetOrderDailySQL($start, $end) {
111    $from = " FROM dtb_order AS T1 LEFT JOIN dtb_customer AS T2 USING ( customer_id ) ";
112    $where = " WHERE T1.del_flg = 0 AND T1.status <> " . ORDER_CANCEL . " AND T1.create_date BETWEEN '$start' AND '$end' ";
113
114/*  mysql¤Ç¤âÌäÂê¤Ê¤¤¤è¤¦¤Ë½¤Àµ
115    $sql = "SELECT ";
116    $sql.= "COUNT(*) AS total_order, ";
117    $sql.= "SUM((SELECT COUNT(*) WHERE customer_id = 0)) AS nonmember, ";
118    $sql.= "SUM((SELECT COUNT(*) WHERE customer_id <> 0 GROUP BY customer_id)) AS member, ";
119    $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1)) AS men, ";
120    $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2)) AS women, ";
121    $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1 AND customer_id <> 0)) AS men_member, ";
122    $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1 AND customer_id = 0)) AS men_nonmember, ";
123    $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2 AND customer_id <> 0)) AS women_member, ";
124    $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2 AND customer_id = 0)) AS women_nonmember, ";
125    $sql.= "SUM(total) AS total, ";
126    $sql.= "int8(AVG(total)) AS total_average ";
127    $sql.= $from;
128    $sql.= $where;      // ¼õÃíºîÀ®Æü¤Ç¸¡º÷¤¹¤ë
129*/
130    $sql = "SELECT ";
131    $sql.= "COUNT(*) AS total_order, ";
132    $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.customer_id = 0) AS nonmember ) AS nonmember, ";
133    $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.customer_id <> 0) AS member ) AS member, ";
134    $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 1) AS men ) AS men, ";
135    $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 2) AS women ) AS women, ";
136    $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, ";
137    $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, ";
138    $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, ";
139    $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, ";
140    $sql.= "SUM(total) AS total, ";
141    $sql.= "(AVG(total)) AS total_average ";
142    $sql.= $from;
143    $sql.= $where;      // ¼õÃíºîÀ®Æü¤Ç¸¡º÷¤¹¤ë
144
145    return $sql;
146}
147
148// Çä¾å¤²½¸·×¥Ð¥Ã¥Á½èÍý(ÆüÊÌ)
149function lfBatOrderDaily($time) {
150    global $arrWDAY;
151   
152    // ½¸·×ÂоÝÆü¤ò¼èÆÀ¤¹¤ë
153    $date = date("Y-m-d", $time);
154   
155    $start = $date . " 00:00:00";
156    $end = $date . " 23:59:59";
157   
158    $sql = lfGetOrderDailySQL($start,$end);
159   
160    $objQuery = new SC_Query();
161    $arrRet = $objQuery->getall($sql); 
162   
163    $sqlval = $arrRet[0];
164   
165    // ¶õʸ»ú¤ò"0"¤ËÊÑ´¹
166    foreach($sqlval as $key => $val) {
167        if ($val == "") {
168            $sqlval[$key] = "0";
169        }
170    }
171   
172    $sqlval['create_date'] = 'now()';
173    $sqlval['order_date'] = $start;
174    $sqlval['year'] = date("Y", $time);
175    $sqlval['month'] = date("m", $time);
176    $sqlval['day'] = date("d", $time);
177    $sqlval['wday'] = date("w", $time);
178    $sqlval['key_day'] = sprintf("%02d/%02d/%02d", substr($sqlval['year'],2), $sqlval['month'], $sqlval['day']);
179    $sqlval['key_month'] = sprintf("%02d/%02d", substr($sqlval['year'],2), $sqlval['month']);
180    $sqlval['key_year'] = sprintf("%d", $sqlval['year']);
181    $sqlval['key_wday'] = sprintf("%s", $arrWDAY[$sqlval['wday']]);
182   
183    $objQuery->delete("dtb_bat_order_daily", "order_date = ?", array($start));
184    $objQuery->insert("dtb_bat_order_daily", $sqlval);
185}
186
187// Çä¾å¤²½¸·×¥Ð¥Ã¥Á½èÍý(»þ´ÖÊÌ)
188function lfBatOrderDailyHour($time) {
189   
190    // ½¸·×ÂоÝÆü¤ò¼èÆÀ¤¹¤ë
191    $date = date("Y-m-d", $time);
192    $objQuery = new SC_Query();
193   
194    $start = $date . " 00:00:00";
195    $objQuery->delete("dtb_bat_order_daily_hour", "order_date = ?", array($start));
196   
197    // 1»þ´ÖËè¤Ë½¸·×¤¹¤ë¡£
198    for($i = 0; $i < 24; $i++) {
199        $sdate = sprintf("%s %02d:00:00", $date, $i);
200        $edate = sprintf("%s %02d:59:59", $date, $i);
201        $sql = lfGetOrderDailySQL($sdate, $edate);
202        $arrRet = $objQuery->getall($sql);
203        $sqlval = $arrRet[0];
204        // ¶õʸ»ú¤ò"0"¤ËÊÑ´¹
205        foreach($sqlval as $key => $val) {
206            if ($val == "") {
207                $sqlval[$key] = "0";
208            }
209        }
210        $sqlval['create_date'] = "now()";
211        $sqlval['order_date'] = $start;
212        $sqlval['hour'] = "$i";
213        $objQuery->insert("dtb_bat_order_daily_hour", $sqlval);
214    }   
215}
216
217// Çä¾å¤²½¸·×¥Ð¥Ã¥Á½èÍý(ǯÎðÊÌ)
218function lfBatOrderAge($time) {
219   
220    $age_loop = intval(BAT_ORDER_AGE / 10);
221   
222    // ǯÎð¤ÎÈϰϤò»ØÄꤷ¤Æ¥Ç¡¼¥¿Ãê½Ð
223    $sql.= "SELECT COUNT(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average ";
224    $sql.= "FROM dtb_order ";
225   
226    // ½¸·×ÂоÝÆü¤ò¼èÆÀ¤¹¤ë
227    $date = date("Y-m-d", $time);
228   
229    $start = $date . " 00:00:00";
230    $end = $date . " 23:59:59";
231   
232    $objQuery = new SC_Query();
233    $objQuery->delete("dtb_bat_order_daily_age", "order_date = ?", array($start));
234
235    /* ²ñ°÷½¸·× */
236
237    $base_where = "WHERE (create_date BETWEEN ? AND ?) AND customer_id <> 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
238
239    $end_date = date("Y/m/d", time());
240    $start_date = date("Y/m/d",strtotime("-10 year" ,strtotime($end_date)));
241    $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($end_date)));
242    // ǯÎðËè¤Ë½¸·×¤¹¤ë¡£
243    for($i = 0; $i <= $age_loop; $i++) {
244        $where = $base_where . " AND order_birth >= cast('$start_date' as date)";
245        $start_age = $i * 10;
246        if($i < $age_loop) {
247            $end_age = $start_age+9;
248            $where = $where . " AND order_birth < cast('$end_date' as date)";
249        }else{
250            $where = $base_where . " AND order_birth < cast('$end_date' as date)";
251            $end_age = 999;
252        }
253        lfBatOrderAgeSub($sql . $where, $start, $end, $start_age, $end_age, 1);
254        $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($start_date)));
255        $start_date = date("Y/m/d",strtotime("-10 year",strtotime($start_date)));
256    }
257   
258    // ÃÂÀ¸ÆüÆþÎϤʤ·
259    $where = $base_where . " AND order_birth IS NULL ";
260    lfBatOrderAgeSub($sql . $where, $start, $end, NULL, NULL, 1);
261
262    /* Èó²ñ°÷½¸·× */
263   
264    $base_where = "WHERE (create_date BETWEEN ? AND ?) AND customer_id = 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
265    $where = $base_where . " AND (to_number(to_char(age(current_timestamp, order_birth), 'YYY'), 999) BETWEEN ? AND ?) ";
266
267    $end_date = date("Y/m/d", time());
268    $start_date = date("Y/m/d",strtotime("-10 year" ,strtotime($end_date)));
269    $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($end_date)));
270    // ǯÎðËè¤Ë½¸·×¤¹¤ë¡£
271    for($i = 0; $i <= $age_loop; $i++) {
272        $where = $base_where . " AND order_birth >= cast('$start_date' as date)";
273        $start_age = $i * 10;
274        if($i < $age_loop) {
275            $end_age = $start_age+9;
276            $where = $where . " AND order_birth < cast('$end_date' as date)";
277        }else{
278            $where = $base_where . " AND order_birth < cast('$end_date' as date)";
279            $end_age = 999;
280        }
281        lfBatOrderAgeSub($sql . $where, $start, $end, $start_age, $end_age, 0);
282        $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($start_date)));
283        $start_date = date("Y/m/d",strtotime("-10 year",strtotime($start_date)));
284    }
285   
286    // ÃÂÀ¸ÆüÆþÎϤʤ·
287    $where = $base_where . " AND order_birth IS NULL AND del_flg = 0";
288    lfBatOrderAgeSub($sql . $where, $start, $end, NULL, NULL, 0);   
289}
290
291// Çä¾å¤²½¸·×¥Ð¥Ã¥Á½èÍý(ǯÎðÊÌ) ÅÐÏ¿Éôʬ
292function lfBatOrderAgeSub($sql, $start, $end, $start_age, $end_age, $member) {
293    $objQuery = new SC_Query();
294   
295    $arrRet = $objQuery->getall($sql, array($start, $end));
296    $sqlval = $arrRet[0];
297   
298    // ¶õʸ»ú¤ò"0"¤ËÊÑ´¹
299    foreach($sqlval as $key => $val) {
300        if ($val == "") {
301            $sqlval[$key] = "0";
302        }
303    }
304
305    $sqlval['create_date'] = "now()";
306    $sqlval['order_date'] = $start;
307    $sqlval['start_age'] = "$start_age";
308    $sqlval['end_age'] = "$end_age";
309    $sqlval['member'] = "$member";
310
311    $objQuery->insert("dtb_bat_order_daily_age", $sqlval);
312}
313
314// ʸ»úÎó¤ËSingleQuotation¤òÉÕÍ¿¤¹¤ë
315function lfSingleQuot($value){
316    $ret = "";
317    if (DB_TYPE == "mysql") {
318        $ret = $value;
319    }else{
320        $ret = "'" . $value . "'";
321    }
322   
323    return $ret;
324}
325
326?>
Note: See TracBrowser for help on using the repository browser.