forked from knowsee/phpMysqlDataHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataTmpToMain.php
259 lines (235 loc) · 9.56 KB
/
dataTmpToMain.php
1
2
3
4
5
6
7
8
9
10
11
12
13
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
58
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
106
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
151
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
if (!defined('BASE_PATH'))
exit('Access Denied!');
class Game_Service_H5HomeAutoHandle {
private static $mTable = array();
private static $mOpenTmp = true;
const CACHE_SET_IN_MAIN = 1;
const CACHE_SET_IN_TMP = 0;
const DATA_TMP_NUM = 99999;
private static function getTmpTableConfig($getTableName = 'tmp') {
$_tmpTableName = array(
'game_h5_recommend_banner_tmp',
'game_h5_recommend_games_tmp',
'game_h5_recommend_hdnew_tmp',
'game_h5_recommend_imgs_tmp',
'game_h5_recommend_rank_tmp',
'game_h5_recommend_tmp',
);
$_tableMainName = array(
'game_h5_recommend' => array(
'list' => array(
'game_h5_recommend_games',
'game_h5_recommend_hdnew',
'game_h5_recommend_imgs',
'game_h5_recommend_rank',
),
'fetchKey' => 'recommend_id'
),
'game_h5_recommend_banner' => array()
);
$tableList = $getTableName == 'tmp' ? $_tmpTableName : $_tableMainName;
return $tableList;
}
public static function initID($dayid) {
return Util_Cookie::set('changeDay', $dayid, true, Common::getTime() + (2 * 3600));
}
public static function getID() {
return Util_Cookie::get('changeDay', true);
}
public static function handleTmpDataToSave() {
Common_Service_Base::beginTransaction();
$tmpTableName = self::getTmpTableConfig();
try {
foreach ($tmpTableName as $key => $table) {
$listDataTmp = self::getTableName($table)->getList('0', self::DATA_TMP_NUM, array('tmp_id' => self::getID()));
$showField = self::getTableField($table);
self::SaveToMainTable($table, $listDataTmp, $showField);
}
self::deleteTmpRowHandle();
Common_Service_Base::commit();
return true;
} catch (Exception $e) {
Common_Service_Base::rollBack();
return false;
}
}
/**
*
* @param int $fromDay
* @param int $toDay
* @return boolean
*/
public static function copyDayToDay($fromDay, $toDay, $noTmpInit = false) {
Common_Service_Base::beginTransaction();
self::$mOpenTmp = $noTmpInit == true ? false : true;
$tableNameList = self::getTmpTableConfig('main');
try {
foreach ($tableNameList as $key => $table) {
$tableName = $key;
$tmpIdList = self::saveToFirstStepData($tableName, array('day_id' => $fromDay), $toDay);
if ($table['fetchKey'] && $tmpIdList) {
self::saveToNextStepData($table['list'], $tmpIdList);
}
}
if ($noTmpInit) {
$editInfo = Game_Service_H5RecommendList::getRecommendList($toDay);
if (!$editInfo) {
$requestData = array();
$requestData['day_id'] = $toDay;
$requestData['create_time'] = Common::getTime();
Game_Service_H5RecommendList::addRecommendList($requestData);
}
}
Common_Service_Base::commit();
return true;
} catch (Exception $e) {
Common_Service_Base::rollBack();
return false;
}
}
public static function cleanAllOldTmpData($dayId) {
Common_Service_Base::beginTransaction();
$tmpTableName = self::getTmpTableConfig();
$tmpTableName[] = 'game_h5_recommend_delete_tmp';
try {
foreach ($tmpTableName as $key => $table) {
self::CleanOldTmpData($table);
}
$tableNameList = self::getTmpTableConfig('main');
foreach ($tableNameList as $key => $table) {
$tableName = $key;
$tmpIdList = self::saveToFirstStepData($tableName, array('day_id' => $dayId), '');
if ($table['fetchKey'] && ($tmpIdList || $dayId)) {
self::saveToNextStepData($table['list'], $tmpIdList, $dayId);
}
}
Common_Service_Base::commit();
return true;
} catch (Exception $e) {
Common_Service_Base::rollBack();
return false;
}
}
private static function deleteTmpRowHandle() {
$params = array('tmp_id' => Game_Service_H5HomeAutoHandle::getID(), 'maininit' => 0);
$deleteList = Game_Service_H5RecommendDelete::getsBy($params);
foreach ($deleteList as $key => $value) {
if (is_numeric($value['delete_id'])) {
self::deleteTmpRowById($value['table_name'], $value['delete_id'], $value['table_main_name']);
} else {
self::deleteTmpRowByAnyThing($value['table_name'], $value['delete_id'], $value['table_main_name']);
}
}
}
private static function deleteTmpRowById($table, $id, $field) {
$table = str_replace('_tmp', '', $table);
self::getTableName($table)->deleteBy(array($field => $id));
}
private static function deleteTmpRowByAnyThing($table, $params, $field = '') {
$params = unserialize($params);
if (!$params)
return;
if (!isset($params['where'])) {
$params = self::makeParams($params, $field);
} else {
$params = array($field => $params);
}
$table = str_replace('_tmp', '', $table);
self::getTableName($table)->deleteBy($params);
}
private static function makeParams($params, $field) {
return array($field => array('IN', $params));
}
private static function saveToFirstStepData($tableName, $param, $toDay = '') {
$listDataTmp = self::getTableName($tableName)->getList('0', self::DATA_TMP_NUM, $param);
$tmpIdList = self::handleListToTmp($tableName, $listDataTmp, $toDay);
return $tmpIdList;
}
private static function saveToNextStepData($tableList, $idList, $isCopy = '') {
$oldIdList = !$isCopy ? array_keys($idList) : $idList;
foreach ($tableList as $key => $table) {
$listDataTmp = self::getTableName($table)->getList('0', self::DATA_TMP_NUM, array('recommend_id' => array('IN', $oldIdList)));
foreach ($listDataTmp as $key => $listInfo) {
if (!$isCopy) {
unset($listInfo['id']);
$listInfo['recommend_id'] = $idList[$listInfo['recommend_id']];
} elseif (self::$mOpenTmp == true) {
$listInfo['maininit'] = self::CACHE_SET_IN_MAIN;
}
if (self::$mOpenTmp == true) {
$listInfo['tmp_id'] = self::getID();
}
$tableTmp = self::$mOpenTmp == true ? $table . '_tmp' : $table;
self::getTableName($tableTmp)->insert($listInfo);
}
}
return true;
}
private static function getTableName($name) {
if (isset(self::$mTable[$name]) && self::$mTable[$name]) {
return self::$mTable[$name];
} else {
self::$mTable[$name] = self::getDao()->changeTableName($name);
return self::$mTable[$name];
}
}
private static function handleListToTmp($tableName, $list, $toDay = '') {
$isCopy = $toDay ? FALSE : TRUE;
foreach ($list as $key => $info) {
$oldId = $info['id'];
if (!$isCopy) {
unset($info['id']);
$info['create_time'] = $info['day_id'] = $toDay;
} elseif (self::$mOpenTmp == true) {
$info['maininit'] = self::CACHE_SET_IN_MAIN;
}
if (self::$mOpenTmp == true) {
$info['tmp_id'] = self::getID();
}
$newTableName = self::$mOpenTmp == true ? $tableName . '_tmp' : $tableName;
self::getTableName($newTableName)->insert($info);
$newId = self::getTableName($newTableName)->getLastInsertId();
if (!$isCopy) {
$returnMergeList[$oldId] = $newId;
} else {
$returnMergeList[$oldId] = $oldId;
}
}
return $returnMergeList;
}
private static function getTableField($table, $autoHidden = true) {
$fieldList = Db_Adapter_Pdo::fetchAll("SHOW COLUMNS FROM `$table`");
foreach ($fieldList as $key => $value) {
if (!in_array($value['Field'], array('tmp_id', 'maininit')) || !$autoHidden) {
$field[] = $value['Field'];
}
}
return $field;
}
private static function CleanOldTmpData($table) {
self::getTableName($table)->deleteBy(array('maininit' => self::CACHE_SET_IN_TMP));
self::getTableName($table)->deleteBy(array('maininit' => self::CACHE_SET_IN_MAIN));
}
private static function SaveToMainTable($table, $list, $showField) {
$table = str_replace('_tmp', '', $table);
$list = self::addMutiInfo($list);
self::getTableName($table)->mutiReplaceInsert($list, $showField);
}
private static function addMutiInfo($list) {
if (!$list)
return false;
foreach ($list as $key => $value) {
if (isset($value['tmp_id'])) {
unset($list[$key]['tmp_id']);
}
if (isset($value['maininit'])) {
unset($list[$key]['maininit']);
}
}
return $list;
}
private static function getDao() {
return Common::getDao("Game_Dao_H5HomeAutoSave");
}
}