Changeset 19691
- Timestamp:
- 2010/11/30 03:00:48 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_5-dev/data/class/helper/SC_Helper_DB.php
r19688 r19691 923 923 * 924 924 * @param SC_Query $objQuery SC_Query インスタンス 925 * @param boolean $is_force_all_count 全カテゴリの集計を強制する場合 true 925 926 * @return void 926 927 */ 927 function sfCategory_Count($objQuery = NULL ){928 function sfCategory_Count($objQuery = NULL, $is_force_all_count = false){ 928 929 $objProduct = new SC_Product(); 929 930 930 931 if($objQuery == NULL) { 931 932 } 933 $is_out_tarns = false;934 //トランザクションの開始確認。開始していない場合は932 $objQuery =& SC_Query::getSingletonInstance(); 933 } 934 935 $is_out_trans = false; 935 936 if(!$objQuery->inTransaction()){ 936 //TODO: トランザクション制御を足す? (重い!) 937 //$objQuery->begin(); 938 $is_out_trans = false; 939 } 940 937 $objQuery->begin(); 938 $is_out_trans = true; 939 } 940 941 941 //共通のfrom/where文の構築 942 942 $sql_where = 'alldtl.del_flg = 0 AND alldtl.status = 1'; … … 951 951 //dtb_category_countの構成 952 952 // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを含まない。 953 // 2.5で消える予定だったが復活させます。 953 // 2.5で消える予定だったが復活させます。DELETE処理は無くしました。 954 954 955 955 //まずテーブル内容の元を取得 956 $arrCategoryCountOld = $objQuery->select('category_id,product_count','dtb_category_count');957 958 //テーブル内容の削除959 $objQuery->query("DELETE FROM dtb_category_count");960 961 //各カテゴリ内の商品数を数えて格納 962 // これは単純集計なので比較的軽い956 if(!$is_force_all_count) { 957 $arrCategoryCountOld = $objQuery->select('category_id,product_count','dtb_category_count'); 958 }else{ 959 $arrCategoryCountOld = array(); 960 } 961 962 //各カテゴリ内の商品数を数えて取得 963 963 $sql = <<< __EOS__ 964 INSERT INTO dtb_category_count(category_id, product_count, create_date) 965 SELECT T1.category_id, count(T2.category_id), now() 964 SELECT T1.category_id, count(T2.category_id) as product_count 966 965 FROM dtb_category AS T1 967 966 LEFT JOIN dtb_product_categories AS T2 … … 972 971 GROUP BY T1.category_id, T2.category_id 973 972 __EOS__; 974 $objQuery->query($sql); 975 976 //dtb_category_total_countの構成 973 974 $arrCategoryCountNew = $objQuery->getAll($sql); 977 975 // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを「含む」。 978 976 //差分を取得して、更新対象カテゴリだけを確認する。 979 980 //各カテゴリ毎のデータ値において以前との差を見る 981 $arrCategoryCountNew = $objQuery->select('category_id, product_count','dtb_category_count'); 982 977 978 //各カテゴリ毎のデータ値において以前との差を見る 983 979 //古いデータの構造入れ替え 984 980 $arrOld = array(); 985 981 foreach($arrCategoryCountOld as $item){ 986 $arrOld[$item['category_id']] = $item ;982 $arrOld[$item['category_id']] = $item['product_count']; 987 983 } 988 984 //新しいデータの構造入れ替え 989 985 $arrNew = array(); 990 986 foreach($arrCategoryCountNew as $item){ 991 $arrNew[$item['category_id']] = $item; 992 } 987 $arrNew[$item['category_id']] = $item['product_count']; 988 } 989 993 990 $arrDiffCategory_id = array(); 994 991 //新しいカテゴリ一覧から見て商品数が異なるデータが無いか確認 995 foreach($arrCategoryCountNew as $item){ 996 $category_id = $item['category_id']; 997 if($arrOld[$category_id]['product_count'] != $item['product_count']){ 998 $arrDiffCategory_id[] = $category_id; 992 foreach($arrNew as $cid => $count){ 993 if($arrOld[$cid] != $count){ 994 $arrDiffCategory_id[] = $cid; 999 995 } 1000 996 } 1001 997 //削除カテゴリを想定して、古いカテゴリ一覧から見て商品数が異なるデータが無いか確認。 1002 foreach($arrCategoryCountOld as $item){ 1003 $category_id = $item['category_id']; 1004 if($arrNew[$category_id]['product_count'] != $item['product_count']){ 1005 $arrDiffCategory_id[] = $category_id; 998 foreach($arrOld as $cid => $count){ 999 if($arrNew[$cid] != $count){ 1000 $arrDiffCategory_id[] = $cid; 1006 1001 } 1007 1002 } … … 1009 1004 //対象IDが無ければ終了 1010 1005 if(count($arrDiffCategory_id) == 0){ 1011 if($is_out_t arns){1006 if($is_out_trans) { 1012 1007 $objQuery->commit(); 1013 1008 } … … 1017 1012 //差分対象カテゴリIDの重複を除去 1018 1013 $arrDiffCategory_id = array_unique($arrDiffCategory_id); 1014 1015 //dtb_category_countの更新 差分のあったカテゴリだけ更新する。 1016 foreach($arrDiffCategory_id as $cid) { 1017 $sqlval = array(); 1018 $sqlval['create_date'] = 'Now()'; 1019 $sqlval['product_count'] = (string)$arrNew[$cid]; 1020 if($sqlval['product_count'] =="") { 1021 $sqlval['product_count'] = (string)'0'; 1022 } 1023 if(isset($arrOld[$cid])) { 1024 $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid)); 1025 }else{ 1026 $sqlval['category_id'] = $cid; 1027 $objQuery->insert('dtb_category_count', $sqlval); 1028 } 1029 } 1030 1019 1031 //差分があったIDとその親カテゴリIDのリストを取得する 1020 1032 $arrTgtCategory_id = array(); … … 1030 1042 $arrTgtCategory_id = array_unique($arrTgtCategory_id); 1031 1043 1032 //dtb_cateogry_total_count 集計処理開始 1033 //旧データの削除 1034 $objQuery->query("DELETE FROM dtb_category_total_count WHERE category_id in (" . implode(',',$arrTgtCategory_id) . ")"); 1035 1036 //カテゴリ毎に処理 1044 //dtb_category_total_count 集計処理開始 1045 //更新対象カテゴリIDだけ集計しなおす。 1046 $arrUpdateData = array(); 1037 1047 foreach ($arrTgtCategory_id as $category_id) { 1038 1048 $arrval = array(); … … 1048 1058 1049 1059 $from = $objProduct->alldtlSQL($sql_where_product_ids); 1050 $sql = <<< __EOS__ 1051 INSERT INTO dtb_category_total_count (category_id, product_count, create_date) 1052 SELECT 1053 ? 1054 ,count(*) 1055 ,now() 1056 FROM $from 1057 WHERE $where 1058 __EOS__; 1059 $objQuery->query($sql, $arrval); 1060 } 1061 //トランザクションの終了処理。 1062 if($is_out_tarns){ 1060 $sql = "SELECT count(*) FROM $from WHERE $where "; 1061 $arrUpdateData[ $category_id ] = $objQuery->getOne($sql, $arrval); 1062 } 1063 // 更新対象だけを更新。 1064 foreach($arrUpdateData as $cid => $count) { 1065 $sqlval = array(); 1066 $sqlval['create_date'] = 'Now()'; 1067 $sqlval['product_count'] = $count; 1068 if($sqlval['product_count'] =="") { 1069 $sqlval['product_count'] = (string)'0'; 1070 } 1071 $ret = $objQuery->update('dtb_category_total_count', $sqlval, 'category_id = ?', array($cid)); 1072 if(!$ret) { 1073 $sqlval['category_id'] = $cid; 1074 $ret = $objQuery->insert('dtb_category_total_count', $sqlval); 1075 } 1076 } 1077 // トランザクション音終了処理 1078 if($is_out_trans) { 1063 1079 $objQuery->commit(); 1064 1080 }
Note: See TracChangeset
for help on using the changeset viewer.